gpt4 book ai didi

iOS 在@selector 处获取 UIButton 当前的 UIControlEventType

转载 作者:行者123 更新时间:2023-11-28 10:23:11 24 4
gpt4 key购买 nike

let aButton = UIButton()
a.addTarget(self, action: "handler:", forControlEvents: .TouchUpInside)

func handler(btn: UIButton) {
//Is there anyway can I know the controlEvent is "TouchUpInside" in this method?
}

如上例,我想知道@selector 的ControlEventType,找不到正确的API 或者不可能?

最佳答案

您可以为不同的控件事件提供不同的方法:

a.addTarget(self, action: "touchDownHandler:", forControlEvents: .TouchDown)
a.addTarget(self, action: "touchUpHandler:", forControlEvents: .TouchUpInside)

func touchDownHandler(btn: UIButton) {
}

func touchUpHandler(btn: UIButton) {
}

another answer 中所述您还可以在处理程序中获取事件并检查触摸类型,这里是 Swift 中的:

a.addTarget(self, action: "handleTouch:event:", forControlEvents: .AllTouchEvents)

// ...

func handleTouch(button: UIButton, event: UIEvent) {
if let touch = event.touchesForView(button)?.first as? UITouch {
switch touch.phase {
case .Began:
println("touch began")

case .Ended:
println("touch ended")

case .Moved:
println("touch moved")

case .Cancelled:
println("touch cancelled")

case .Stationary:
println("touch stationary")

}
}
}

关于iOS 在@selector 处获取 UIButton 当前的 UIControlEventType,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31405771/

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