【问题标题】:How to Implement Tap Gesture on programmatically generated UIView inside UIScrollView in Swift 5如何在 Swift 5 中的 UIScrollView 内以编程方式生成的 UIView 上实现 Tap Gesture
【发布时间】:2020-03-25 17:56:26
【问题描述】:

我正在尝试在 Swift 中以编程方式生成的 UIScrollView 内创建一个 UIView 位置。在编译时,我能够按预期看到它们,但我无法在 UIView 上调用 Tap Gesture。我经历了各种相同的例子,它们都有与我相似的东西。我的代码如下。注意:我从 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")
}

【问题讨论】:

    标签: ios swift xcode uiscrollview


    【解决方案1】:

    对于任何可能有同样问题的人。问题出在手势上下文“self”上,这里有详细解释https://stackoverflow.com/a/53717065/2448688 通过初始化Gesture来解决它,如下所示

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

    【讨论】:

      猜你喜欢
      • 2019-08-05
      • 2013-12-11
      • 2012-02-13
      • 1970-01-01
      • 1970-01-01
      • 2023-03-18
      • 2013-04-08
      • 2020-08-02
      • 2021-04-24
      相关资源
      最近更新 更多