簡體   English   中英

繪制視圖,但未檢測到手勢識別器

[英]View is drawn but gesture recognizer are not detected

我有一個UIScrollView ,其中有一個UIView作為子視圖,而FooView包含我的所有FooView ,也就是UIView 每個FooView都有一個手勢識別器。 對於我的滾動視圖中的第一個FooView ,檢測到的手勢就很好(這意味着選擇器已正確觸發),但是所有后續FooView不會觸發該手勢。

我想這個問題的答案不被稱為Subview Gesture Recognizer解釋了我所面臨的問題。 引用:“問題是帶有手勢識別器的子視圖在超級視圖的框架之外。這意味着即使繪制了視圖,也沒有檢測到手勢”。

我了解手頭的問題,但不確定如何解決。

順便說一句:如果我將FooView直接添加到滾動視圖,而不是將它們添加到contentView ,它會完美地工作。 但是,對於我的用例來說,這不是一個選擇,因為我使用的是自動布局,否則約束會變得混亂。

隨即附上我的MWE,您可以在其中看到在第一個視圖上點擊將打印出“已輕按”,但其他視圖將沒有輸出。 該代碼只是一個游樂場示例,因此可以將其復制到此處以運行。

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)
        }
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM