Ken*_*ins 8 cocoa-touch drag-and-drop objective-c uikit uiview
我有一个包含在UIView对象A中的UIView对象.我希望能够触摸X并将其从对象A中移除并将其移动到对象B(另一个UIView)中.对象A和B都在同一个超级UIView中.
A B
_____ _____
| | | |
| X | -> | |
|___| |___|
Run Code Online (Sandbox Code Playgroud)
这就是我到目前为止所拥有的.
@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
Run Code Online (Sandbox Code Playgroud)
Cos*_*que 10
调用[[touches anyObject] locationInView: self.superview]在容器视图中获取手指下的点.然后发送self.superview -hitTest:withEvent:以找出X在里面的视图.请注意,它将始终返回X,因此您必须覆盖其中一个-pointIsInside:withEvent:或-hitTest:withEvent:在拖动时返回nil.这种kludge是我在容器视图中实现这种跟踪的原因,而不是在拖动视图中.