你在哪里声明 UITapGestureRecognizer() 与 Swift 中的 addGestureRecognizer() 相关? [英] Where do you declare UITapGestureRecognizer() in relation to addGestureRecognizer() in Swift?

查看:23
本文介绍了你在哪里声明 UITapGestureRecognizer() 与 Swift 中的 addGestureRecognizer() 相关?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

centerLabelGesture 是在 viewDidLoad() 范围之外定义的,所以在调用 .addGestureRecognizer(centerLabelGesture) 时,centerLabelGesture 尚未定义.

centerLabelGesture is being defined outside of the scope of viewDidLoad(), so at the time that .addGestureRecognizer(centerLabelGesture) is called, centerLabelGesture is not defined yet.

import UIKit
import SnapKit

class ViewController: UIViewController {

    var screen: UIView!
    var centerLabel: UILabel!

    override func viewDidLoad() {
        super.viewDidLoad()
        screen = UIView()
        centerLabel = UILabel()

        view.addSubview(screen)
        screen.addSubview(centerLabel)

        screen.backgroundColor = .white
        screen.snp.makeConstraints { (make) in
            make.top.equalTo(view)
            make.right.equalTo(view)
            make.left.equalTo(view)
            make.bottom.equalTo(view)
        }

        centerLabel.text = "I hope I'm centered."
        centerLabel.snp.makeConstraints { (make) in
            make.center.equalTo(screen)
        }
        centerLabel.isUserInteractionEnabled = true
        centerLabel.addGestureRecognizer(centerLabelGesture)
    }

    let centerLabelGesture = UITapGestureRecognizer(target: self, action: #selector(centerLabelTapped))

    @objc func centerLabelTapped() {
        centerLabel.text = "Ouch, you tapped me!"
    }

}

2019 年 1 月 19 日更新

matt 指出centerLabelGesture 需要在centerLabel.addGestureRecognizer(centerLabelGesture) 之前声明,在viewDidLoad()

Update 1/19/2019

matt pointed out that centerLabelGesture needs to be declared prior to centerLabel.addGestureRecognizer(centerLabelGesture), inside viewDidLoad()

推荐答案

这是一个微妙的错误.问题在于您放置此行的位置:

This is a subtle mistake. The problem is the place you've put this line:

let centerLabelGesture = UITapGestureRecognizer(target: self, action: #selector(centerLabelTapped))

将该行移动到 viewDidLoad 代码中:

Move that line into the viewDidLoad code:

centerLabel.isUserInteractionEnabled = true
let centerLabelGesture = UITapGestureRecognizer(target: self, action: #selector(centerLabelTapped))
centerLabel.addGestureRecognizer(centerLabelGesture)

原因是你得到那条线的地方,它是一个实例属性,当你说self作为实例属性初始值设定项中的目标时,它并不意味着你的想法确实(这意味着 class,而不是 instance),所以当您点击时的消息被误导并且什么也不做.

The reason is that where you've got that line, it's an instance property, and when you say self as the target in an instance property initializer, it doesn't mean what you think it does (it means the class, not the instance), so the message when you tap is misdirected and does nothing.

我已经提交了关于此问题的错误;在我看来,编译器至少应该警告你你正在犯一个潜在的错误.

I have filed a bug on this issue; in my opinion the compiler should at least warn you that you're making a potential mistake.

这篇关于你在哪里声明 UITapGestureRecognizer() 与 Swift 中的 addGestureRecognizer() 相关?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆