Detect 'Up' UIGesture in UIScrollView

时间:2015-05-24 22:18:23

标签: ios swift uiscrollview uigesturerecognizer

I have a custom segue in my app connected to a button, which when tapped scrolls my view down to a different view. I need to implement the unwind for the segue, and would like it to occur when the user swipes up when at the top of the UIScrollView on the second view.

This is my code for the segue:

override func perform() {

    var MainVCView = self.sourceViewController.view as UIView!
    var CreditVCView = self.destinationViewController.view as UIView!

    let screenWidth = UIScreen.mainScreen().bounds.size.width
    let screenHeight = UIScreen.mainScreen().bounds.size.height

    CreditVCView.frame = CGRectMake(0.0, screenHeight, screenWidth, screenHeight)

    let window = UIApplication.sharedApplication().keyWindow
    window?.insertSubview(CreditVCView, aboveSubview: MainVCView)


    // Transition.
    UIView.animateWithDuration(1.0, animations: { () -> Void in
        MainVCView.frame = CGRectOffset(MainVCView.frame, 0.0, -screenHeight)
        CreditVCView.frame = CGRectOffset(CreditVCView.frame, 0.0, -screenHeight)

        }) { (Finished) -> Void in
            self.sourceViewController.presentViewController(self.destinationViewController as! UIViewController, animated: false, completion: nil)
    }
}

I have implemented the gesture recogniser, however I can't get it to detect the swipe when it's set to 'Up' or 'Down'. It only works when it's set to 'Left' or 'Right'.

This is the code:

@IBOutlet weak var scrollView: UIScrollView!

override func viewDidLoad() {
    super.viewDidLoad()

    let unwindSwipe = UISwipeGestureRecognizer(target: self, action: "unwind")
    unwindSwipe.direction = UISwipeGestureRecognizerDirection.Down
    view.addGestureRecognizer(unwindSwipe)
    view.userInteractionEnabled = true
    scrollView.canCancelContentTouches = true

    // Do any additional setup after loading the view.
}

func gestureRecognizer(UIGestureRecognizer, shouldRecognizeSimultaneouslyWithGestureRecognizer:UIGestureRecognizer) -> Bool {
        return true
}

func unwind() {
    println("Test")
}

As you can see, I've tried a few things but I just can't get it to detect the gesture.

Any ideas?

1 个答案:

答案 0 :(得分:0)

添加滑动手势

override func viewDidLoad() {
    super.viewDidLoad()

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

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

func respondToSwipeGesture(gesture: UIGestureRecognizer) {

    if let swipeGesture = gesture as? UISwipeGestureRecognizer {

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

并添加一行来处理碰撞 scrollView.panGestureRecognizer.requireGestureRecognizerToFail(swipeGesture)