How to detect swiping left / right on the iPhone?

后端 未结 3 766
梦毁少年i
梦毁少年i 2021-02-04 07:50

Is there any easy way to detect these kinds of gestures for iPhone? I can use touchesBegan, touchesMoved, touchesEnded. But how can I implement the gestures? thz u.

3条回答
  •  南方客
    南方客 (楼主)
    2021-02-04 08:38

    You will using UISwipeGestureRecognizer Object to detect the Touch and direction of Swipe In

    UIView or anywhere in iphone sdk.

     UISwipeGestureRecognizer *rightRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(rightSwipeHandle:)];
    rightRecognizer.direction = UISwipeGestureRecognizerDirectionRight;
    [rightRecognizer setNumberOfTouchesRequired:1];
    
    //add the your gestureRecognizer , where to detect the touch..
    [view1 addGestureRecognizer:rightRecognizer];
    [rightRecognizer release];
    
    UISwipeGestureRecognizer *leftRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(leftSwipeHandle:)];
    leftRecognizer.direction = UISwipeGestureRecognizerDirectionLeft;
    [leftRecognizer setNumberOfTouchesRequired:1];
    
    [view1 addGestureRecognizer:leftRecognizer];
    [leftRecognizer release];
    
     - (void)rightSwipeHandle:(UISwipeGestureRecognizer*)gestureRecognizer
     {
         NSLog(@"rightSwipeHandle");
     }
    
     - (void)leftSwipeHandle:(UISwipeGestureRecognizer*)gestureRecognizer 
     {
    NSLog(@"leftSwipeHandle");
     }
    

    I think this the Better solution for your problem

提交回复
热议问题