gpt4 book ai didi

ios - touchesBegan 在 Swift 中的用法

转载 作者:行者123 更新时间:2023-11-29 01:55:05 25 4
gpt4 key购买 nike

想知道touchesBegan在Swift中的使用函数。我可以使用它来了解是否按下了按钮吗?你能举一个这个功能的例子吗?

最佳答案

touchesBegan() 的主要目的不是用于按钮。无需任何其他声明或代码即可使用该方法来显示手指在屏幕上的触摸。有配套函数 touchesMoved() 显示手指移动,touchesEnded() 显示手指松开。还有一个函数 touchesCancelled() 表示触摸被取消。

这是一个例子:

override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
print("finger touched the screen...")
}

要检测按钮是否被按下,您可以使用它的目标和选择器:

myButton.addTarget(self, action: "buttonPressed:", forControlEvents: UIControlEvents.TouchUpInside)

在下面的函数中你的按钮被触发:

func buttonPressed(button: UIButton!) {
print("button was pressed")
}

根据 UIControlEvent,您可以通过按下并松开、触摸它、触摸并移开手指等方式触发按钮。

希望对您有所帮助:)

关于ios - touchesBegan 在 Swift 中的用法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30959297/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com