UISwipeGestureRecognizerDirection仅识别正确的手势

时间:2015-06-17 16:57:13

标签: ios objective-c uitableview uiswipegesturerecognizer

我正在尝试向我的UITableView中的每个单元格添加左右滑动手势。然而,无论我在哪里,似乎没有人回答这个问题。使用当前推荐的解决方案,我没有输入正确的手势处理程序。

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


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

我为我的解决方案创建了两个不同的手势识别器。但是,我永远无法看到正确的UIGestureRecognizer的响应。我不明白为什么我从来没有看到右手势识别器的响应只剩下一个。

-(void)swipeLeft:(UISwipeGestureRecognizer*)gestureRecognizer
{ NSLog("You have swiped Left");
}
-(void)swipeRight:(UISwipeGestureRecognizer*)gestureRecognizer
{ NSLog("You have swiped right"); //Never Enter this handler
}

1 个答案:

答案 0 :(得分:1)

使用此代码...

我希望你不要太介意它。

override func viewDidLoad() {
    super.viewDidLoad()

    var swipeRight = UISwipeGestureRecognizer(target: self, action: "respondToSwipeGesture:")
    swipeRight.direction = UISwipeGestureRecognizerDirection.Right
    self.view.addGestureRecognizer(swipeRight)

    var swipeDown = UISwipeGestureRecognizer(target: self, action: "respondToSwipeGesture:")
    swipeDown.direction = UISwipeGestureRecognizerDirection.Down
    self.view.addGestureRecognizer(swipeDown)
}

func respondToSwipeGesture(gesture: UIGestureRecognizer) {

    if let swipeGesture = gesture as? UISwipeGestureRecognizer {

        switch swipeGesture.direction {
        case UISwipeGestureRecognizerDirection.Right:
            println("Swiped right")
        case UISwipeGestureRecognizerDirection.Down:
            println("Swiped down")
        default:
            break
        }
    }
}