swift滑动识别器在第一次滑动时不起作用

时间:2016-09-13 10:33:23

标签: swift

这是我的代码 我希望左滑动和右滑动有两个功能 两种功能都有效,但只有当我刷两次......第一次没有任何反应 谢谢!

override func viewWillAppear(animated: Bool) {

    SwipeGesture.addTarget(self, action: "addSwipe")
}

func addSwipe() {
    let directions: [UISwipeGestureRecognizerDirection] = [.Right, .Left]
    for direction in directions {
        let gesture = UISwipeGestureRecognizer(target: self, action: Selector("handleSwipe:"))
        gesture.direction = direction
        self.view.addGestureRecognizer(gesture)
    }
}

func handleSwipe(sender: UISwipeGestureRecognizer) {
    switch sender.direction {
    case UISwipeGestureRecognizerDirection.Right:
        print("swipe to close")
        self.dismissViewControllerAnimated(true, completion: nil)

    case UISwipeGestureRecognizerDirection.Left:
        print("Swiped left")

        let storyBoard : UIStoryboard = UIStoryboard(name: "Main", bundle:nil)
            let resultViewController = storyBoard.instantiateViewControllerWithIdentifier("chatAlbumViewController") as UIViewController
            self.presentViewController(resultViewController, animated:true, completion:nil)
    default:
        break
    }
}

1 个答案:

答案 0 :(得分:0)

你需要刷两次的原因是因为你有一个添加手势识别器的手势识别器,所以第一次滑动注册它们然后第二次实际做你想要的。

要修复,请更改此代码:

override func viewWillAppear(animated: Bool) {

    SwipeGesture.addTarget(self, action: "addSwipe")
}

override func viewWillAppear(animated: Bool) {

    self.addSwipe()
}

并删除另一个的插座,不需要