ios - View 已绘制但未检测到手势识别器

标签 ios swift uiscrollview uigesturerecognizer

我有一个 UIScrollView,它有一个 UIView 作为 subview ,它反过来包含我所有的 FooView,这是一个 UIView 也是如此。每个 FooView 都有一个手势识别器。对于我的 ScrollView 中的第一个 FooView,可以很好地检测到手势(意味着选择器被正确触发),但所有后续 FooView 都不会触发该手势.

这个问题的答案Subview Gesture Recognizer not being called我假设解释了我面临的问题。引用:“[...] 问题是带有手势识别器的 subview 在父 View 的框架之外。这意味着即使正在绘制 View ,也没有检测到手势”。

我了解手头的问题,但不确定如何解决。

顺便说一句:如果我将 FooView 直接添加到 ScrollView ,而不是将它们添加到 contentView,它就可以完美地工作。但是,对于我的用例,这不是一个选项,因为我使用的是自动布局,否则约束会被弄乱。

附件是我的 MWE,您可以在其中看到点击第一个 View 将打印出“Tapped”,但其他 View 将没有输出。该代码只是一个 Playground 示例,因此可以复制到那里运行。

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

最佳答案

我认为您需要正确设置 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)
        }
    }

关于ios - View 已绘制但未检测到手势识别器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44101656/

相关文章:

iOS将containerview中的item链接到viewcontroller

ios - 是否有分配 UITabBarController 管理的 Controller 属性的最佳实践?

ios - 如何在 iOS 中找到当前 View Controller 之前的所有 View Controller ?

ios - ScrollView 无法快速放大或缩小

ios - 启用内容插入的 UIScrollView 分页工作异常

ios - 无法选择内部测试版本

arrays - JSON解析全局变量Swift 4

ios - swift iOS : control incoming calls

ios - 在 subview 上未检测到 UIGestureRecognizer

ios - AutoLayout UIScrollView 内容在旋转时不调整大小(包括示例项目)