在UIView之间拖动UIView [英] Drag UIView between UIViews
问题描述
我有一个UIView对象X包含在一个UIView对象A.我想能够触摸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 drag 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];
//触摸时设置正确的中心
touchPoint.x - = deltaX;
touchPoint.y - = deltaY;
self.center = touchPoint;
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
//发现查看事件结束了,子视图。
}
@end
调用 [[touches anyObject] locationInView:self.superview]
获取容器视图中手指下的点。然后发送 self.superview -hitTest:withEvent:
找出视图X在里面。注意,它总是返回X,所以你必须重写 -pointIsInside:withEvent:
或 -hitTest:withEvent:
在拖动时返回nil。这种kludge是我在容器视图中实现这种跟踪的原因,而不是拖动视图。
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
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.
这篇关于在UIView之间拖动UIView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!