简体   繁体   中英

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. On compilation, I am able to see them both as expected but I am unable to invoke Tap Gesture on the UIView. 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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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