我正在尝试在UIScrollView上左右滑动。然而,即使向右滑动,看起来向左滑动在iPhone模拟器中也不起作用。我错过了任何一步吗?
- (void)viewWillAppear:(BOOL)animated { [super viewWillAppear:animated]; self.scrollView.multipleTouchEnabled = YES; UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipe:)]; swipe.delaysTouchesBegan = YES; swipe.numberOfTouchesRequired = 2; [self.scrollView addGestureRecognizer:swipe]; [swipe release]; } - (void)handleSwipe:(UISwipeGestureRecognizer *)recognizer { if (recognizer.direction == UISwipeGestureRecognizerDirectionLeft) { } else if (recognizer.direction == UISwipeGestureRecognizerDirectionRight) { } }
答案 0 :(得分:19)
使用以下:
UISwipeGestureRecognizer *rightRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(rightSwipeHandle:)];
rightRecognizer.direction = UISwipeGestureRecognizerDirectionRight;
[rightRecognizer setNumberOfTouchesRequired:1];
[mainSlideShowImageScrollView addGestureRecognizer:rightRecognizer];
[rightRecognizer release];
UISwipeGestureRecognizer *leftRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(leftSwipeHandle:)];
leftRecognizer.direction = UISwipeGestureRecognizerDirectionLeft;
[leftRecognizer setNumberOfTouchesRequired:1];
[mainSlideShowImageScrollView addGestureRecognizer:leftRecognizer];
[leftRecognizer release];
- (void)rightSwipeHandle:(UISwipeGestureRecognizer*)gestureRecognizer
{
//Do moving
}
- (void)leftSwipeHandle:(UISwipeGestureRecognizer*)gestureRecognizer
{
// do moving
}
答案 1 :(得分:4)
您的'handleSwipe'代码实际上是正确的。您需要两个UISwipeGestureRecognizers,但您可以将它们指向同一个处理程序,包含您的“IF”语句。
答案 2 :(得分:0)
你可以创建一个手势识别器来处理左右滑动(甚至是所有方向 - !):
UISwipeGestureRecognizer *swipeGesture = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipe:)];
swipeGesture.direction = UISwipeGestureRecognizerDirectionLeft | UISwipeGestureRecognizerDirectionRight;
所有方向:
swipeGesture.direction = UISwipeGestureRecognizerDirectionLeft | UISwipeGestureRecognizerDirectionRight | UISwipeGestureRecognizerDirectionUp | UISwipeGestureRecognizerDirectionDown;