UITapGestureRecognizer not attaching action

吃可爱长大的小学妹 提交于 2021-01-28 06:15:02

问题


I created a separate class for View. I left all the functions in the Controller. But when I add a click on the picture, it doesn't work for some reason.

import UIKit

class APOTDView: UIView {

    var imageView: UIImageView = {
        let imageView = UIImageView()
        imageView.translatesAutoresizingMaskIntoConstraints = false
        imageView.isUserInteractionEnabled = true
        let tap = UITapGestureRecognizer(target: self, action: #selector(APOTDViewController.imageTapped(_:)))
        imageView.addGestureRecognizer(tap)

        return imageView
    }()
}
import UIKit

class APOTDViewController: UIViewController {

    let av = APOTDView()

    override func viewDidLoad() {
        super.viewDidLoad()
        // ... add subview and constraint
    }

    @objc func imageTapped(_ sender: UITapGestureRecognizer) {
        print("good job")
    }
}

What's the matter? Please help me figure it out


回答1:


Your selector in the UITapGestureRecognizer is wrong. You can not call the APOTDViewController directly.
APOTDViewController.imageTapped would be a static function, which is not available.

You can use a delegate instead.

Delegate Protocol and View.

protocol APOTDViewDelegate: AnyObject {
    func viewDidTapImage()
}

class APOTDView: UIView {
    weak var delegate: APOTDViewDelegate?

    var imageView: UIImageView = {
        let imageView = UIImageView()
        imageView.translatesAutoresizingMaskIntoConstraints = false
        imageView.isUserInteractionEnabled = true
        let tap = UITapGestureRecognizer(target: self, action: #selector(imageTapped))
        imageView.addGestureRecognizer(tap)

        return imageView
    }()

    @objc func imageTapped() {
        delegate?.viewDidTapImage()
    }
}

ViewController:

class APOTDViewController: UIViewController, APOTDViewDelegate {
    let av = APOTDView()

    override func viewDidLoad() {
        super.viewDidLoad()
        av.delegate = self
        // ... add subview and constraint
    }

    @objc func viewDidTapImage() {
        print("good job")
    }
}



回答2:


This will not work, because you are calling the UIViewController method directly without any class reference or object. The solution is to use protocol or clouser to get action from view to class.

class

class APOTDView: UIView {
    @objc var imageViewAction: ((UITapGestureRecognizer) -> Void)? = nil
    
    lazy var imageView: UIImageView = {
        let imageView = UIImageView()
        imageView.backgroundColor = .blue
        imageView.translatesAutoresizingMaskIntoConstraints = true
        imageView.isUserInteractionEnabled = true
        let tap = UITapGestureRecognizer(target: self, action: #selector((imageTapped(_:))))
        imageView.addGestureRecognizer(tap)
        return imageView
    }()
    
    @objc private func imageTapped(_ sender: UITapGestureRecognizer) {
        self.imageViewAction?(sender)
    }
}

ViewController

class APOTDViewController: UIViewController {
    let av = APOTDView()

    override func viewDidLoad() {
        super.viewDidLoad()
        av.imageViewAction = { sender in
            print("good job")
        }
    }
}



来源:https://stackoverflow.com/questions/65735936/uitapgesturerecognizer-not-attaching-action

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!