绘制视图但未检测到手势识别器

时间:2017-05-21 20:50:52

标签: ios swift uiscrollview uigesturerecognizer

我有一个UIScrollView,其UIView作为子视图,作为回报,它还包含我FooView的所有内容,这也是UIView。这些FooView中的每一个都有一个手势识别器。我的滚动视图中的第一个FooView检测到手势很好(意味着选择器正在被正确触发),但所有后续FooView都不会触发该手势。

这个问题的回答Subview Gesture Recognizer not being called我假设解释了我所面临的问题。引用:" [...]问题是带有手势识别器的子视图超出了超视图的框架。这意味着即使正在绘制视图,也未检测到手势"。

我理解手头的问题,但不知道如何解决。

顺便说一下:如果我将FooView直接添加到滚动视图,而不是将它们添加到contentView,它的效果非常好。但是,对于我的用例,这不是一个选项,因为我使用的是自动布局,否则约束会被弄乱。

随附您的MWE,您可以看到点击第一个视图将打印出来" Tapped",但其他视图将没有输出。代码只是一个操场示例,因此可以在那里复制以运行。

import UIKit
import PlaygroundSupport

class AwesomeView: UIView {
    override init(frame: CGRect) {
        super.init(frame: frame)

        backgroundColor = UIColor.getRandomColor()

        let tapGesture = UITapGestureRecognizer(target: self, action: #selector(tapHandler))
        self.addGestureRecognizer(tapGesture)
    }

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }

    override func layoutSubviews() {
        guard let superview = superview else {
            return
        }

        self.frame = superview.frame
    }

    func tapHandler(_ sender: AnyObject) {
        print("Tapped")
    }
}

extension UIColor {
    static func getRandomColor(_ hash: UInt32 = arc4random()) -> UIColor{

        let randomRed:CGFloat = CGFloat(hash) / CGFloat(UInt32.max)
        let randomGreen:CGFloat = CGFloat(hash << 4) / CGFloat(UInt32.max)
        let randomBlue:CGFloat = CGFloat(hash << 2) / CGFloat(UInt32.max)

        return UIColor(red: randomRed, green: randomGreen, blue: randomBlue, alpha: 1.0)
    }
}

class DummyVC: UIViewController, UIScrollViewDelegate {

    let scrollView = UIScrollView(frame: CGRect(x:0, y:0, width:320,height: 300))
    var contentView: UIView!
    var frame: CGRect = CGRect(x:0, y:0, width:0, height:0)

    override func viewDidLoad() {
        super.viewDidLoad()

        contentView = UIView(frame: scrollView.frame)
        scrollView.addSubview(contentView)

        self.view.addSubview(scrollView)

        for index in 0..<4 {

            frame.origin.x = self.scrollView.frame.size.width * CGFloat(index)
            frame.size = self.scrollView.frame.size
            self.scrollView.isPagingEnabled = true

            let subView = AwesomeView(frame: frame)
            self.contentView.addSubview(subView)
        }

        self.scrollView.contentSize = CGSize(width:self.scrollView.frame.size.width * 4, height: self.scrollView.frame.size.height)
    }
}

PlaygroundPage.current.liveView = DummyVC().view
PlaygroundPage.current.needsIndefiniteExecution = true

1 个答案:

答案 0 :(得分:0)

我认为你需要正确设置contentView的大小。现在,您将contentView的帧设置为scrollView框架的大小,即总大小的1/4。注意viewDidLoad中的最后一行,我们将contentView的框架设置为scrollView的内容大小。

    class DummyVC: UIViewController, UIScrollViewDelegate {

        let scrollView = UIScrollView(frame: CGRect(x:0, y:0, width:320,height: 300))
        var contentView: UIView!
        var frame: CGRect = CGRect(x:0, y:0, width:0, height:0)

        override func viewDidLoad() {
            super.viewDidLoad()

            var contentFrame = scrollView.frame
            contentFrame.size.width *= 4
            contentView = UIView(frame: .zero)
            scrollView.addSubview(contentView)

            self.view.addSubview(scrollView)

            for index in 0..<4 {

                frame.origin.x = self.scrollView.frame.size.width * CGFloat(index)
                frame.size = self.scrollView.frame.size
                self.scrollView.isPagingEnabled = true

                let subView = AwesomeView(frame: frame)
                self.contentView.addSubview(subView)
            }

            self.scrollView.contentSize = CGSize(width:self.scrollView.frame.size.width * 4, height: self.scrollView.frame.size.height)
            //new line to set contentView's frame
            contentView.frame = CGRect(origin: .zero, size: scrollView.contentSize)
        }
    }