我能夠使用手勢識別器委託方法requireGestureRecognizerToFail計算出來。我需要兩個手指在各個方向上滑動。我首先創建了一個平移手勢識別器,並設置適當的最小和最大觸摸量。您必須將平移手勢的代表設置爲self,並且確保您將requireGestureRecognizerToFail設置爲每個滑動手勢。不要忘記在shouldRecognizeSimultaneouslyWithGestureRecognizer中傳遞滑動手勢集合。
- (void)viewDidLoad
{
[super viewDidLoad];
scrollView.contentSize = CGSizeMake(image.frame.size.width, image.frame.size.height);
scrollView.maximumZoomScale = 4;
scrollView.minimumZoomScale = 1;
scrollView.delegate = self;
UIPanGestureRecognizer *TwoFingerPanGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(HandlePanGesture:)];
TwoFingerPanGesture.minimumNumberOfTouches=2;
TwoFingerPanGesture.maximumNumberOfTouches=2;
[TwoFingerPanGesture setDelegate:self];
[scrollView addGestureRecognizer:TwoFingerPanGesture];
UISwipeGestureRecognizer *TwoFingerSwipeDownGesture = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(HandleSwipeGesture:)];
TwoFingerSwipeDownGesture.direction = UISwipeGestureRecognizerDirectionDown;
TwoFingerSwipeDownGesture.numberOfTouchesRequired = 2 ;
[TwoFingerPanGesture requireGestureRecognizerToFail:TwoFingerSwipeDownGesture];
[scrollView addGestureRecognizer:TwoFingerSwipeDownGesture];
UISwipeGestureRecognizer *TwoFingerSwipeUpGesture = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(HandleSwipeGesture:)];
TwoFingerSwipeUpGesture.direction = UISwipeGestureRecognizerDirectionUp;
TwoFingerSwipeUpGesture.numberOfTouchesRequired = 2;
[TwoFingerPanGesture requireGestureRecognizerToFail:TwoFingerSwipeUpGesture];
[scrollView addGestureRecognizer:TwoFingerSwipeUpGesture];
UISwipeGestureRecognizer *TwoFingerSwipeLeftGesture = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(HandleSwipeGesture:)];
TwoFingerSwipeLeftGesture.direction = UISwipeGestureRecognizerDirectionLeft;
TwoFingerSwipeLeftGesture.numberOfTouchesRequired = 2;
[TwoFingerPanGesture requireGestureRecognizerToFail:TwoFingerSwipeLeftGesture];
[scrollView addGestureRecognizer:TwoFingerSwipeLeftGesture];
UISwipeGestureRecognizer *TwoFingerSwipeRightGesture = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(HandleSwipeGesture:)];
TwoFingerSwipeRightGesture.direction = UISwipeGestureRecognizerDirectionRight;
TwoFingerSwipeRightGesture.numberOfTouchesRequired = 2;
[TwoFingerPanGesture requireGestureRecognizerToFail:TwoFingerSwipeRightGesture];
[scrollView addGestureRecognizer:TwoFingerSwipeRightGesture];
}
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
// Either just return YES to allow your cell's gesture recognizer
// to work simultaneously with all other recognizers:
//return YES;
// Or you can decide whether your cell's pan gesture recognizer should
// recognize simultaneously with otherGestureRecognizer. For example,
// you could get a reference to your collection view's panGestureRecognizer
// and only return YES if otherGestureRecognizer is equal to that recognizer:
return otherGestureRecognizer == scrollView.gestureRecognizers;
}
你能分享你已經試過的代碼嗎? –