iOS在@selector处获取UIButton当前的UIControlEventType [英] iOS fetch UIButton current UIControlEventType at @selector

查看:50
本文介绍了iOS在@selector处获取UIButton当前的UIControlEventType的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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还是不可能?

as the example above, I wanna know the ControlEventType at @selector, could not found the correct API or it's impossible?

推荐答案

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

You can either provide different methods for the different control events:

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

func touchDownHandler(btn: UIButton) {
}

func touchUpHandler(btn: UIButton) {
}

另一个答案所述,您还可以在处理程序中获取事件并检查触摸的类型,在Swift中:

As noted in another answer you can also get the event in your handler and inspect that for the type of touch, here it is in 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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆