1

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?

4

1 回答 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 回答