使用UITableView的UISwipeGestureRecognizer自定义操作

时间:2015-03-17 06:57:51

标签: ios uitableview swift uigesturerecognizer uiswipegesturerecognizer

我试图添加滑动手势(左/右)以隐藏/显示我的侧边菜单。 我在UIView上完美运行,但是,我在使用UITableView时遇到了麻烦。

我的代码是将我的滑动手势添加到我的TableView:

// Add right swipe gesture recognizer

    let rightSwipeGestureRecognizer = UISwipeGestureRecognizer(target: self, action: "toggleSideMenu")
    rightSwipeGestureRecognizer.direction =  UISwipeGestureRecognizerDirection.Right
    self.timelineTableView.addGestureRecognizer(rightSwipeGestureRecognizer)

    // Add left swipe gesture recognizer
    let leftSwipeGestureRecognizer = UISwipeGestureRecognizer(target: self, action: "toggleSideMenu")
    leftSwipeGestureRecognizer.direction = UISwipeGestureRecognizerDirection.Left
    //sideMenuContainerView.addGestureRecognizer(rightSwipeGestureRecognizer)
    self.timelineTableView.addGestureRecognizer(leftSwipeGestureRecognizer)

这是我的选择方法:

func toggleSideMenu() {
    println("ENTER SWIPE")
    toggleSideMenuView()
}

我也做到了这一点:

func tableView(tableView: UITableView, editingStyleForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCellEditingStyle {
    return UITableViewCellEditingStyle.None
}

func tableView(tableView: UITableView, canEditRowAtIndexPath indexPath: NSIndexPath) -> Bool {
    return false
}

然而,我的选择器菜单" toggleSideMenu()"我向左或向右滑动时从未调用过。

P.S:我还尝试直接在我的UITableViewCell上添加滑动手势,但它也不起作用。

有人有想法吗?非常感谢你的时间!

2 个答案:

答案 0 :(得分:7)

感谢Kirit Modi。这是解决我问题的解决方案

添加:

leftSwipeGestureRecognizer.delegate = self    
rightSwipeGestureRecognizer.delegate = self

然后添加 UIGestureRecognizerDelegate 委托方法:

func gestureRecognizer(gestureRecognizer: UIGestureRecognizer, shouldReceiveTouch touch: UITouch) -> Bool {
    return true
}

答案 1 :(得分:0)

For me helped

(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
return YES;
}

because i had two gestures. One from slide menu and one from current VC. So that one from slide menu killed VC's gestures.