在 UIScrollView 中检测“向上”UIGesture

Posted

技术标签:

【中文标题】在 UIScrollView 中检测“向上”UIGesture【英文标题】:Detect 'Up' UIGesture in UIScrollView 【发布时间】:2015-05-24 22:18:22 【问题描述】:

我的应用中有一个连接到按钮的自定义 segue,当点击该按钮时,我的视图会向下滚动到另一个视图。我需要为 segue 实现展开,并希望当用户在第二个视图的 UIScrollView 顶部向上滑动时发生这种情况。

这是我的转场代码:

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)
    

我已经实现了手势识别器,但是当它设置为“向上”或“向下”时,我无法让它检测到滑动。它仅在设置为“左”或“右”时有效。

这是代码:

@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")

如您所见,我尝试了一些方法,但无法检测到手势。

有什么想法吗?

【问题讨论】:

我在你的代码中看到你有一个滚动视图,你是否试图让手势识别在它之上工作?因为它会“静止”滚动视图的动作 是的,我正在努力做到这一点,一旦用户滚动到滚动视图的顶部,他们可以再次向上滑动,它会继续回到另一个视图。 你需要实现一个为什么来监控滚动视图的位置,当它到达顶部时你可以看到另一个视图。另一个想法是,如果速度大于某个值,则拦截手势并执行 segue,并滚动其较小的视图。 【参考方案1】:

添加滑动手势

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)

【讨论】:

以上是关于在 UIScrollView 中检测“向上”UIGesture的主要内容,如果未能解决你的问题,请参考以下文章

UIScrollView 检测滚动

在 uiscrollview 中检测 toucesbegin

如何检测 UIScrollView 何时变为可滚动?

UISwipeGestureRecognizer 和 UIScrollView

UIScrollView 不会一直向上/向下滚动

UIScrollview 在向下滚动并推送新的视图控制器后向上移动内容