在主窗口的根视图控制器类中,我设置了UITapGestureRecognizer属性。在调用覆盖的viewDidLoad函数的过程中,我已通过addGestureRecognizer方法将此属性添加到图像属性。我也将image.isUserInteractionEnabled设置为true。
override func viewDidLoad() {
image.addGestureRecognizer(tap)
image.isUserInteractionEnabled = true
self.view.addSubview(image)
}
var tap = UITapGestureRecognizer(target: self, action: #selector(imagePressed(_ :)))
Run Code Online (Sandbox Code Playgroud)
可悲的是,这不会导致在运行时调用imagePressed()。令我感到困惑的是,以下代码产生了预期的结果。
override func viewDidLoad() {
var tap = UITapGestureRecognizer(target: self, action:#selector(imagePressed(_ :)))
image.addGestureRecognizer(tap)
image.isUserInteractionEnabled = true
self.view.addSubview(image)
}
Run Code Online (Sandbox Code Playgroud)
所以我的问题是,为什么在第一种情况下未调用UITapGestureRecognizer的选择器?
使用Swift 3和Xcode 8.2.1
我遇到了以下代码片段:
(defstructure object
"An object is anything that occupies space. Some objects are 'alive'."
(name "?") ; Used to print the object on the map
(alive? nil) ; Is the object alive?
(loc (@ 1 1)) ; The square that the object is in
(bump nil) ; Has the object bumped into something?
(size 0.5) ; Size of object as proportion of loc
(color 'black) ; Some objects have a color
(shape 'rectangle) ; Some objects have a shape
(sound …
Run Code Online (Sandbox Code Playgroud)