如何在Swift 5的UIScrollView内的程序生成的UIView上实现点击手势

问题描述 投票:0回答:1

我正在尝试在均通过Swift生成的UIScrollView内创建一个UIView位置。在编译时,我能够按预期看到它们,但是我无法在UIView上调用Tap Gesture。我经历了相同的各种示例,它们都有与我相似的东西。我的代码如下。注意:我有一个从自动布局添加为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")
}
ios swift xcode uiscrollview
1个回答
0
投票

对于可能有相同问题的任何人。问题在于手势上下文“自我”,如此处详细说明的https://stackoverflow.com/a/53717065/2448688通过初始化手势来解决它,如下所示

var mediaTapRecognizer : UITapGestureRecognizer {
    let t = UITapGestureRecognizer(target: self, action: #selector(openMedia))
    return t
}
© www.soinside.com 2019 - 2024. All rights reserved.