gpt4 book ai didi

ios - Swift 4 - 将 UITapGestureRecognizer 添加到 subview 图像 - 未调用该方法

转载 作者:IT王子 更新时间:2023-10-29 05:28:37 24 4
gpt4 key购买 nike

我有以下 UITapGestureRecognizer 设置,但没有调用该方法?

注意:UITapGestureRecognizer 被添加到 subview 项。

此外,它在 SUStepView 本身中添加 UIGestureRecognizerDelegate 时也有效 - 唯一的问题是我需要在容器中使用它。

class StepViewContainer: NSObject, UIGestureRecognizerDelegate {
var view: SUStepView?

@objc func tapAction(recognizer: UITapGestureRecognizer) {

}

override init(){
super.init()
// View
self.view = Bundle.main.loadNibNamed("SignupV3Views", owner: self, options: nil)![0] as? SUStepView

let mytapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(tapAction))
mytapGestureRecognizer.numberOfTapsRequired = 1
self.view?.imageView.addGestureRecognizer(mytapGestureRecognizer)
}
}

StepViewContainer 中的 View :

class SUStepView: UIView {
@IBOutlet weak var imageView: UIImageView!

@objc public func nextStepTap(sender: UITapGestureRecognizer) {
}

override func awakeFromNib() {
let tapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(nextStepTap))
tapGestureRecognizer.numberOfTapsRequired = 1
self.imageView.addGestureRecognizer(tapGestureRecognizer)

self.imageView.isUserInteractionEnabled = true
self.imageView.layer.masksToBounds = true
self.imageView.layer.cornerRadius = self.imageView.frame.size.width / 2
self.imageView.clipsToBounds = true

}

最佳答案

Swift 4 代码:

点击手势:

 tapGesture = UITapGestureRecognizer(target: self, action: #selector(ViewController.myviewTapped(_:)))
tapGesture.numberOfTapsRequired = 1
tapGesture.numberOfTouchesRequired = 1

ImageView 点击:

    self.imageView = UIImageView(frame: CGRect(x: 0, y: 0, width: self.view.frame.size.width/2.0, height: self.view.frame.size.height/2.0))
self.imageView.isUserInteractionEnabled = true
self.imageView.backgroundColor = UIColor.red
self.imageView.layer.masksToBounds = true
self.imageView.layer.cornerRadius = self.imageView.frame.size.width / 2
self.imageView.clipsToBounds = true
self.imageView.isUserInteractionEnabled = true
self.imageView.addGestureRecognizer(tapGesture)
self.view.addSubview(self.imageView)

点击调用

@objc func myviewTapped(_ sender: UITapGestureRecognizer) {
if self.imageView.backgroundColor == UIColor.yellow {
self.imageView.backgroundColor = UIColor.green
}else{
self.imageView.backgroundColor = UIColor.yellow
}
}

关于ios - Swift 4 - 将 UITapGestureRecognizer 添加到 subview 图像 - 未调用该方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46583964/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com