pressGestureRecognizer和touchesBegan

c2p*_*ing 3 cocoa cocoa-touch touchesbegan ios

我有以下问题.我正在使用一个UILongPressGestureRecognizerUIView进入"切换模式".如果UIView处于"切换模式",则用户可以在屏幕上拖动UIView.为了在屏幕上拖动UIView我正在使用方法touchesBegan,touchesMovedtouchesEnded.

它工作,但是:我必须举起手指才能拖动它,因为该touchesBegan方法已被调用,因此不会再次调用,因此我无法拖动UIView屏幕.

触发touchesBegan后是否有任何方法可以手动调用UILongPressGestureRecognizer(UILongPressGestureRecognizer更改BOOL值,touchesBegan仅当此BOOL设置为YES时才有效).

Rob*_*Rob 10

UILongPressGestureRecognizer是一个连续的手势识别器,所以,而不是诉诸touchesMovedUIPanGestureRecognizer,只是检查UIGestureRecognizerStateChanged,例如:

- (void)viewDidLoad
{
    [super viewDidLoad];

    UILongPressGestureRecognizer *gesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(handleGesture:)];
    [self.view addGestureRecognizer:gesture];
}

- (void)handleGesture:(UILongPressGestureRecognizer *)gesture
{
    CGPoint location = [gesture locationInView:gesture.view];

    if (gesture.state == UIGestureRecognizerStateBegan)
    {
        // user held down their finger on the screen

        // gesture started, entering the "toggle mode"
    }
    else if (gesture.state == UIGestureRecognizerStateChanged)
    {
        // user did not lift finger, but now proceeded to move finger

        // do here whatever you wanted to do in the touchesMoved
    }
    else if (gesture.state == UIGestureRecognizerStateEnded)
    {
        // user lifted their finger

        // all done, leaving the "toggle mode"
    }
}
Run Code Online (Sandbox Code Playgroud)