简体   繁体   English

如何在 Swift 5 中的 UIScrollView 中以编程方式生成的 UIView 上实现点击手势

[英]How to Implement Tap Gesture on programmatically generated UIView inside UIScrollView in Swift 5

I am trying to create a UIView place inside a UIScrollView both generated programmatically in Swift.我正在尝试在 UIScrollView 中创建一个 UIView 位置,这两个位置都是在 Swift 中以编程方式生成的。 On compilation, I am able to see them both as expected but I am unable to invoke Tap Gesture on the UIView.在编译时,我能够按预期看到它们,但我无法在 UIView 上调用 Tap Gesture。 I have gone through various examples of the same and they all have something similar to what I have.我经历过各种相同的例子,它们都有与我所拥有的相似的东西。 My code is as below.我的代码如下。 NOTE: I have a wrapper view added from Autolayout as Superview注意:我从 Autolayout 添加了一个包装器视图作为 Superview

override func viewDidLoad() {
    super.viewDidLoad()
    let mediaTapRecognizer = UITapGestureRecognizer(target: self, action: #selector( self.openMedia ) )
    let slideShowWidth = self.wrapperView.bounds.width
    let slideShowHeight = 225

    let slideShowScrollView: UIScrollView = {
        let scroll = UIScrollView()
        scroll.isPagingEnabled = false
        scroll.showsHorizontalScrollIndicator = false
        scroll.showsVerticalScrollIndicator = false
        scroll.backgroundColor = .white

        scroll.frame = CGRect (x: 0,  y: 0, width: self.wrapperView.bounds.width , height: CGFloat(slideShowHeight) )

        scroll.contentSize = CGSize(width:  slideShowWidth , height: CGFloat(slideShowHeight) )

        return scroll
    }()

    self.wrapperView.addSubview(slideShowScrollView)

    slideShowScrollView.widthAnchor.constraint(equalToConstant: self.wrapperView.bounds.width ).isActive = true
    slideShowScrollView.heightAnchor.constraint(equalToConstant: CGFloat(slideShowHeight) ).isActive = true
    slideShowScrollView.leadingAnchor.constraint(equalTo: self.wrapperView.leadingAnchor).isActive = true
    slideShowScrollView.topAnchor.constraint(equalTo: self.wrapperView.topAnchor).isActive = true
    slideShowScrollView.trailingAnchor.constraint(equalTo: self.wrapperView.trailingAnchor).isActive = true
    //slideShowScrollView.bottomAnchor.constraint(equalTo: self.wrapperView.bottomAnchor).isActive = true

    let placeholder:UIView = {
        let view = UIView()
        view.frame = CGRect (x: 0,  y: 0, width: self.view.bounds.width , height: CGFloat(slideShowHeight) )
        view.backgroundColor = .brown

        return view
    }()

    //slideShowScrollView.clipsToBounds = true
    slideShowScrollView.addSubview(placeholder)

    placeholder.isUserInteractionEnabled = true
    placeholder.addGestureRecognizer(mediaTapRecognizer)

    placeholder.translatesAutoresizingMaskIntoConstraints = false
    placeholder.widthAnchor.constraint(equalToConstant: self.wrapperView.bounds.width ).isActive = true
    placeholder.heightAnchor.constraint(equalToConstant: CGFloat(slideShowHeight) ).isActive = true
    placeholder.leadingAnchor.constraint(equalTo: slideShowScrollView.leadingAnchor).isActive = true
    placeholder.topAnchor.constraint(equalTo: slideShowScrollView.topAnchor).isActive = true
    placeholder.trailingAnchor.constraint(equalTo: slideShowScrollView.trailingAnchor).isActive = true
    placeholder.bottomAnchor.constraint(equalTo: slideShowScrollView.bottomAnchor).isActive = true
}

@objc func openMedia(){
    //print("The clicked media is \(sender)")
    print("The clicked media is me")
}

For anyone who might have same issue.对于可能有同样问题的任何人。 Problem was with the gestures Context "self" as explained in detail here https://stackoverflow.com/a/53717065/2448688 Solved it by initializing Gesture as follows问题出在手势上下文“self”上,如这里详细解释的那样https://stackoverflow.com/a/53717065/2448688通过初始化 Gesture 解决了它,如下所示

var mediaTapRecognizer : UITapGestureRecognizer {
    let t = UITapGestureRecognizer(target: self, action: #selector(openMedia))
    return t
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM