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) {
}
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")
}
}
}