I have a touch operation that is a trigger for resize kind of scenario, where the operation starts on touches move, and needs to end on touchesEnded. The thing is, that the touchesEnd could be on a different UI View, since the user is dragging the finger. How do I register a *global" touchesEnded listener?
问问题
271 次
1 回答
5
您应该使用UIPanGestureRecognizer
. 每次触发时,检查state
手势识别器。如果UIGestureRecognizerStateBegan
是,则首先触摸视图,如果是,UIGestureRecognizerStateChanged
则用户正在移动手指,如果是,UIGestureRecognizerStateEnded
则用户已抬起手指。
只要手势在您的视图上开始,即使在离开视图边界后它也会继续(除非取消),因此您无需担心“使其成为全局”。
初始化手势识别器:
UIPanGestureRecognizer *dragViewGestureRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handleDragView:)];
dragViewGestureRecognizer.maximumNumberOfTouches = 1;
[yourView addGestureRecognizer:dragViewGestureRecognizer];
然后用你需要的代码填写下面的方法:
- (void)handleDragView:(UIPanGestureRecognizer*)panGestureRecognizer {
switch (panGestureRecognizer.state) {
case UIGestureRecognizerStateBegan: {
//Code when dragging starts
break;
} case UIGestureRecognizerStateChanged: {
//Code while dragging is happening (if needed)
break;
} case UIGestureRecognizerStateEnded: {
//Code when dragging ends
break;
}
default:
break;
}
}
于 2013-07-16T04:42:21.497 回答