简体   繁体   English

swift UITapGestureRecognizer 在视图上不起作用

[英]swift UITapGestureRecognizer not working on view

I had to create a new thread bcoz it's driving me crazy and all the other answers online are exactly the same.我不得不创建一个新线程,因为它让我发疯了,而网上的所有其他答案都完全相同。 I have done this countless of times but I cannot see what I am missing for the life of me.我已经这样做了无数次,但我看不到我一生中缺少的东西。 I am using a "test" view controller just to get the tap gesture working but it isn't working at all... I am fairly certain that I am setting this up correctly, as this is how I've always implemented it in the past: (yes, I have checked the box for isUserInteractionEnabled).我正在使用“测试”视图控制器只是为了让点击手势工作,但它根本不起作用......我很确定我正确设置了它,因为这是我一直在实现它的方式过去:(是的,我已经选中了 isUserInteractionEnabled 框)。 I am even implementing this on a different viewcontroller this exact way and it is working...我什至以这种确切的方式在不同的视图控制器上实现它并且它正在工作......

class TestViewController: UIViewController {

  override func viewDidLoad() {
    super.viewDidLoad()
    view.addGestureRecognizer(tap)
  }

  let tap = UITapGestureRecognizer(target: self, action: #selector(wasTapped))

    
  @objc func wasTapped() {
    print("tapped")
  }
}

I have also tried adding the parameters to wasTapped:我还尝试将参数添加到 wasTapped:

@objc func wasTapped(gestureRecognizer: UITapGestureRecognizer) {
  print("tapped")
}

You are saying:你在说:

override func viewDidLoad() {
    super.viewDidLoad()

    view.addGestureRecognizer(tap)
}

let tap = UITapGestureRecognizer(target: self, action: #selector(wasTapped))

The problem is the last line:问题是最后一行:

let tap = UITapGestureRecognizer(target: self, action: #selector(wasTapped))

You cannot just say let tap like that in the middle of nowhere.你不能只是说let tap那样let tap You are implicitly making an instance property.您正在隐式地创建一个实例属性。 But you cannot initialize an instance property with a target of self , because self does not exist at the time an instance property is initialized.但是您不能以self为目标初始化实例属性,因为在初始化实例属性时self不存在。 (I regard the fact that that code even compiles as a bug, and have reported it as such.) (我认为该代码甚至可以编译为错误,并已将其报告为错误。)

Move that line to the start of viewDidLoad , like this:将该行移动到viewDidLoad的开头,如下所示:

override func viewDidLoad() {
    super.viewDidLoad()

    let tap = UITapGestureRecognizer(target: self, action: #selector(wasTapped))
    view.addGestureRecognizer(tap)
}

Try something like this尝试这样的事情

override func viewDidLoad() {
    super.viewDidLoad()

     let tap = UITapGestureRecognizer(target: self, action: #selector(wasTapped(sender:)))
     tap.numberOfTapsRequired = 1 // Default value
     view.isUserInteractionEnabled = true
     view.addGestureRecognizer(tap)

}

@objc func wasTapped(sender: UITapGestureRecognizer) {
    print("tapped")
}

You have to enable interaction if you want to use gesture recognizers for standard UIView 's如果您想对标准UIView使用手势识别器,则必须启用交互

Add view.isUserInteractionEnabled = true in your viewDidLoad .viewDidLoad添加view.isUserInteractionEnabled = true 。

var tapGesture = UITapGestureRecognizer()

take a view and set IBOutlet like:查看并设置 IBOutlet 如下:

@IBOutlet weak var viewTap: UIView!     

Write pretty code on viewDidLoad() like:viewDidLoad()上编写漂亮的代码,例如:

tapGesture = UITapGestureRecognizer(target: self, action: #selector(self.myviewTapped(_:)))
tapGesture.numberOfTapsRequired = 1
tapGesture.numberOfTouchesRequired = 1
viewTap.addGestureRecognizer(tapGesture)
viewTap.isUserInteractionEnabled = true

this method is calling when tap gesture recognized:此方法在识别点击手势时调用:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM