在选项卡式视图应用程序中滑动手势

时间:2012-07-12 19:10:09

标签: iphone objective-c ios

所以这非常非常奇怪。我已经将右舷的滑动手势识别器添加到我的6标签标签栏中第一个标签的视图中。我将其设置为向左,并将其作为Action连接到firstTabViewController.h。而且效果很好。

现在,如果我尝试以相同的方式将“正确方向”滑动手势识别器添加到第一个选项卡,则操作甚至不会注册。

此外,如果我尝试在另一个选项卡(而不是索引0处的选项卡)中执行相同的操作,或者将工作选项卡移动到选项卡栏中的其他位置,则应用程序崩溃且访问错误我刷的时候。

firstTabViewController.h

- (IBAction)swipeLeft:(id)sender;   // Works fine
- (IBAction)swipeRight:(id)sender;  // Doesn't even register

firstTabViewController.m

- (IBAction)swipeLeft:(id)sender {
    int nextIndex = CURRENT_INDEX + 1;  // I did modify this accordingly when the tab was moved

    [self.tabBarController setSelectedIndex:nextIndex];
    NSLog(@"Swipe left");

}

- (IBAction)swipeRight:(id)sender {
    NSLog(@"Swiped Right");
}

1 个答案:

答案 0 :(得分:3)

刚试过这个并且它有效:

UISwipeGestureRecognizer *swipeLeft = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeLeft:)];
swipeLeft.direction = UISwipeGestureRecognizerDirectionLeft;
[self.view addGestureRecognizer:swipeLeft];
swipeLeft.delegate = self;
[swipeLeft release];

UISwipeGestureRecognizer *swipeRight = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeRight:)];
swipeRight.direction = UISwipeGestureRecognizerDirectionRight;
[self.view addGestureRecognizer:swipeRight];
swipeRight.delegate = self;
[swipeRight release];

-(void) swipeRight:(UISwipeGestureRecognizer *) recognizer {
    if (recognizer.direction == UISwipeGestureRecognizerDirectionRight)
        NSLog(@"swipe right");
}

-(void) swipeLeft:(UISwipeGestureRecognizer *) recognizer {
    if (recognizer.direction == UISwipeGestureRecognizerDirectionLeft)
        NSLog(@"swipe left");

}
相关问题