Simultaneous gesture recognizers in Iphone SDK

前端 未结 5 1280
灰色年华
灰色年华 2020-11-27 05:25

I need to catch two different swipping gestures using UISwipeGestureRecognizer(for example, UISwipeGestureRecognizerDirectionRight and UISwip

5条回答
  •  抹茶落季
    2020-11-27 05:47

    It was really easy:

    At first we should create class, that implements UIGestureRecognizerDelegate protocol:

    @interface MyGestureDelegate : NSObject 
    
    
    @implementation MyGestureDelegate
    
    - (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer{
        return YES;
    }
    
    - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer{
        return YES;
    }
    - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch{
        return YES;
    }
    
    

    And use it like this:

    
        UISwipeGestureRecognizer *swipeGestureLeft = [[UISwipeGestureRecognizer alloc]
                                                  initWithTarget:self action:@selector(handleSwipeGestureLeft:)];
        [self.view addGestureRecognizer:swipeGestureLeft];
        swipeGestureLeft.direction = UISwipeGestureRecognizerDirectionLeft;
        [swipeGestureLeft release];
    
        UISwipeGestureRecognizer *swipeGesture = [[UISwipeGestureRecognizer alloc]
                                              initWithTarget:self action:@selector(handleSwipeGesture:)];
        swipeGesture.direction = UISwipeGestureRecognizerDirectionRight;
        [self.view addGestureRecognizer:swipeGesture];
    
        MyGestureDelegate *deleg = [[MyGestureDelegate alloc] init];
    
        [swipeGesture setDelegate:deleg];
        [swipeGesture release];
    
    

提交回复
热议问题