在UIViews之间拖动UIView(Drag UIView between UIViews)

编程入门 行业动态 更新时间:2024-10-28 04:28:17
在UIViews之间拖动UIView(Drag UIView between UIViews)

我有一个包含在UIView对象A中的UIView对象X.我想要能够触摸X并从对象A中移除它并将它移动到对象B(另一个UIView)中。 对象A和B都在同一个超级UIView中。

A B _____ _____ | | | | | X | -> | | |___| |___|

这是我迄今为止所拥有的。

@implementation X_UIView float deltaX; float deltaY; - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { [self.superview.superview addSubview:self]; //pop dragged view outside of container view CGPoint beginCenter = self.center; UITouch * touch = [touches anyObject]; CGPoint touchPoint = [touch locationInView:self.superview]; deltaX = touchPoint.x - beginCenter.x; deltaY = touchPoint.y - beginCenter.y; } - (void) touchesMoved:(NSSet*)touches withEvent:(UIEvent*)event { UITouch * touch = [touches anyObject]; CGPoint touchPoint = [touch locationInView:self.superview]; // Set the correct center when touched touchPoint.x -= deltaX; touchPoint.y -= deltaY; self.center = touchPoint; } - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event { //discover view that event ended was over and add self as a subview. } @end

I have a UIView object X that is contained in an UIView object A. I want to be able to touch X and remove it from object A and move it into object B (another UIView). Both Object A & B are inside of the same super UIView.

A B _____ _____ | | | | | X | -> | | |___| |___|

This is what I have so far.

@implementation X_UIView float deltaX; float deltaY; - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { [self.superview.superview addSubview:self]; //pop dragged view outside of container view CGPoint beginCenter = self.center; UITouch * touch = [touches anyObject]; CGPoint touchPoint = [touch locationInView:self.superview]; deltaX = touchPoint.x - beginCenter.x; deltaY = touchPoint.y - beginCenter.y; } - (void) touchesMoved:(NSSet*)touches withEvent:(UIEvent*)event { UITouch * touch = [touches anyObject]; CGPoint touchPoint = [touch locationInView:self.superview]; // Set the correct center when touched touchPoint.x -= deltaX; touchPoint.y -= deltaY; self.center = touchPoint; } - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event { //discover view that event ended was over and add self as a subview. } @end

最满意答案

调用[[touches anyObject] locationInView: self.superview]获取容器视图中手指下的点。 然后发送self.superview -hitTest:withEvent:找出视图X在里面。 请注意,它将始终返回X,因此您必须在拖动时重写-pointIsInside:withEvent:或-hitTest:withEvent:以返回nil。 这种kludge是我在容器视图中实现这种跟踪的原因,而不是在拖动视图中。

Call [[touches anyObject] locationInView: self.superview] to get the point under the finger in the container view. Then send self.superview -hitTest:withEvent: to find out the view X is inside. Note that it will always return X, so you will have to override either -pointIsInside:withEvent: or -hitTest:withEvent: to return nil while you're dragging. This kind of kludge is the reason I would implement such tracking in the container view, rather than in a dragged view.

更多推荐

本文发布于:2023-07-05 14:35:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1038500.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:拖动   UIViews   Drag   UIView

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!