【问题标题】:Swift - UISwipeGestureRecognizer Within UIScrollViewSwift - UIScrollView 中的 UISwipeGestureRecognizer
【发布时间】:2016-08-24 20:56:38
【问题描述】:

我相信我找到了解决方案,但它在 Obj-C 中,我对将其解释为 Swift 感到完全陌生和困惑:Horizontal UISwipeGestureRecognizer in subview of UIScrollView ? (UIScrollView only needs to recognize vertical scrolling)

我有我的 Main.storyboard,它添加了两个子视图,我可以在它们之间水平滚动。目前,由于 UIScrollView,检测到上下滑动,但未检测到左右滑动。这种干扰有什么解决方法吗?

Main.storyboard:

// Global
let vc0 = ViewController0(nibName: "ViewController0", bundle: nil)
let vc1 = ViewController1(nibName: "ViewController1", bundle: nil)

class ViewController: UIViewController, UIScrollViewDelegate, UIGestureRecognizerDelegate {

@IBOutlet weak var scrollView: UIScrollView!

override func viewDidLoad() {
    super.viewDidLoad()

    self.addChildViewController(vc0)
    self.scrollView.addSubview(vc0.view)
    vc0.didMoveToParentViewController(self)

    var frame1 = vc1.view.frame
    frame1.origin.x = self.view.frame.size.width
    vc1.view.frame = frame1

    self.addChildViewController(vc1)
    self.scrollView.addSubview(vc1.view)
    vc1.didMoveToParentViewController(self)

    self.scrollView.contentSize = CGSizeMake(self.view.frame.size.width * 2, self.view.frame.size.height - 66)
    self.scrollView.delegate = self

    // Swipe Gesture Recognizers
    let swipeRight = UISwipeGestureRecognizer(target: self, action: #selector(ViewController.respondToSwipeGesture(_:)))
    swipeRight.direction = UISwipeGestureRecognizerDirection.Right
    let swipeLeft = UISwipeGestureRecognizer(target: self, action: #selector(ViewController.respondToSwipeGesture(_:)))
    swipeLeft.direction = UISwipeGestureRecognizerDirection.Left
    let swipeUp = UISwipeGestureRecognizer(target: self, action: #selector(ViewController.respondToSwipeGesture(_:)))
    swipeUp.direction = UISwipeGestureRecognizerDirection.Up
    let swipeDown = UISwipeGestureRecognizer(target: self, action: #selector(ViewController.respondToSwipeGesture(_:)))
    swipeDown.direction = UISwipeGestureRecognizerDirection.Down

    swipeRight.delegate = self
    swipeLeft.delegate = self

    self.view.addGestureRecognizer(swipeRight)
    self.view.addGestureRecognizer(swipeLeft)
    self.view.addGestureRecognizer(swipeUp)
    self.view.addGestureRecognizer(swipeDown)
}

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

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

// Debugging - Only Up & Down Swipes Are Detected
func respondToSwipeGesture(gesture: UIGestureRecognizer) {
    if let swipeGesture = gesture as? UISwipeGestureRecognizer {
        switch swipeGesture.direction {
        case UISwipeGestureRecognizerDirection.Right:
            print("Swiped right")
        case UISwipeGestureRecognizerDirection.Down:
            print("Swiped down")
        case UISwipeGestureRecognizerDirection.Left:
            print("Swiped left")
        case UISwipeGestureRecognizerDirection.Up:
            print("Swiped up")
        default:
            break
        }
    }
}
}

【问题讨论】:

    标签: ios swift uiscrollview swipe uiswipegesturerecognizer


    【解决方案1】:

    我认为你需要实现UIGestureRecognizerDelegate 协议

    这是否达到了您的要求?

    import UIKit
    
    class ViewController: UIViewController, UIScrollViewDelegate, UIGestureRecognizerDelegate {
    
        @IBOutlet weak var scrollView: UIScrollView!
    
        override func viewDidLoad() {
            super.viewDidLoad()
    
            // these were made in a storyboard I whipped up for this example.
            let vc0 = self.storyboard!.instantiateViewControllerWithIdentifier("vc0")
            let vc1 = self.storyboard!.instantiateViewControllerWithIdentifier("vc1")
    
            self.addChildViewController(vc0)
            self.scrollView.addSubview(vc0.view)
            vc0.didMoveToParentViewController(self)
    
            var frame1 = vc1.view.frame
            frame1.origin.x = self.view.frame.size.width
            vc1.view.frame = frame1
    
            self.addChildViewController(vc1)
            self.scrollView.addSubview(vc1.view)
            vc1.didMoveToParentViewController(self)
    
            self.scrollView.contentSize = CGSizeMake(self.view.frame.size.width * 2, self.view.frame.size.height - 66)
            self.scrollView.delegate = self
    
            // Swipe Gesture Recognizers
            // These can be lets because they aren't mutated and I'm using the latest Selector syntax
            let swipeRight = UISwipeGestureRecognizer(target: self, action: #selector(ViewController.respondToSwipeGesture(_:)))
            swipeRight.direction = UISwipeGestureRecognizerDirection.Right
            let swipeLeft = UISwipeGestureRecognizer(target: self, action: #selector(ViewController.respondToSwipeGesture(_:)))
            swipeLeft.direction = UISwipeGestureRecognizerDirection.Left
            let swipeUp = UISwipeGestureRecognizer(target: self, action: #selector(ViewController.respondToSwipeGesture(_:)))
            swipeUp.direction = UISwipeGestureRecognizerDirection.Up
            let swipeDown = UISwipeGestureRecognizer(target: self, action: #selector(ViewController.respondToSwipeGesture(_:)))
            swipeDown.direction = UISwipeGestureRecognizerDirection.Down
    
            // ViewController will be the delegate for the left and right swipes
            swipeRight.delegate = self
            swipeLeft.delegate = self
    
            self.view.addGestureRecognizer(swipeRight)
            self.view.addGestureRecognizer(swipeLeft)
            self.view.addGestureRecognizer(swipeUp)
            self.view.addGestureRecognizer(swipeDown)
        }
    
        // here are those protocol methods with Swift syntax
        func gestureRecognizer(gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWithGestureRecognizer otherGestureRecognizer: UIGestureRecognizer) -> Bool {
            return true
        }
    
        func gestureRecognizer(gestureRecognizer: UIGestureRecognizer, shouldReceiveTouch touch: UITouch) -> Bool {
            return true
        }
    
        // Debugging - All Swipes Are Detected Now
        func respondToSwipeGesture(gesture: UIGestureRecognizer) {
            if let swipeGesture = gesture as? UISwipeGestureRecognizer {
                switch swipeGesture.direction {
                case UISwipeGestureRecognizerDirection.Right:
                    print("Swiped right")
                case UISwipeGestureRecognizerDirection.Down:
                    print("Swiped down")
                case UISwipeGestureRecognizerDirection.Left:
                    print("Swiped left")
                case UISwipeGestureRecognizerDirection.Up:
                    print("Swiped up")
                default:
                    break
                }
            }
        }
    }
    

    【讨论】:

    • 啊该死!我忘记在我的代码中包含我的变量 vc0 和 vc1。它们被全局放置。现在更新了。我在没有初始化 vc0 和 vc1 的情况下尝试了你的代码,它崩溃了。嗯...
    • 您使用的是什么版本的 Swift 和 Xcode?还有什么是崩溃?
    • 我正在使用最新版本的 Swift 和 xCode。抱歉没有具体说明。当我开始滑动到 vc0 时它就崩溃了:Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[Project.ViewController respondToSwipeGesture:]: unrecognized selector sent to instance 0x7be75360'
    • 你确定你符合UIGestureRecognizerDelegate吗?粘贴您已修改的内容。
    • 我是。我已经更新了我的问题中的代码,并让我的全局变量保持不变,因为我认为这就是问题所在。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-29
    • 2015-10-02
    • 2012-12-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多