我想知道swift中touchesBegan的使用功能。我可以使用它来知道是否按下按钮?你能举一个这个函数的例子吗?
答案 0 :(得分:1)
touchesBegan()
的主要目的不是按钮。该方法可以在没有任何其他声明或代码的情况下使用,以显示手指触摸屏幕。伴随功能touchesMoved()
显示手指移动,touchesEnded()
显示手指放开。此外还有功能touchesCancelled()
,表示触摸已被取消。
以下是一个例子:
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
print("finger touched the screen...")
}
要检测按钮是否被按下,请使用其目标&amp;选择器:
myButton.addTarget(self, action: "buttonPressed:", forControlEvents: UIControlEvents.TouchUpInside)
在以下功能中,您的按钮会被触发:
func buttonPressed(button: UIButton!) {
print("button was pressed")
}
根据UIControlEvent
,您可以通过按下并放开,触摸,触摸和移动手指等来触发按钮。
希望有所帮助:)