I'm trying to create a subclass of UIControl and track touches to change control appearance.
I don't know why but if I add the action (for .TouchUpInside) from IB or code, when I touch the control the register action method get called twice.
The stack trace tells me that me that the first call comes from _sendActionsForEvents:withEvent:
, the second is not clear.
Here there is how I overridden track methods:
override func continueTrackingWithTouch(touch: UITouch, withEvent event: UIEvent?) -> Bool {
let touchPoint = touch.locationInView(self)
if CGRectContainsPoint(bounds, touchPoint) {
sendActionsForControlEvents(.TouchDragInside)
}
else {
sendActionsForControlEvents(.TouchDragOutside)
}
return true
}
override func beginTrackingWithTouch(touch: UITouch, withEvent event: UIEvent?) -> Bool {
sendActionsForControlEvents(.TouchDown)
return true
}
override func endTrackingWithTouch(touch: UITouch?, withEvent event: UIEvent?) {
guard let tou = touch else { return }
let touchPoint = tou.locationInView(self)
if CGRectContainsPoint(bounds, touchPoint) {
sendActionsForControlEvents(.TouchUpInside)
}
else {
sendActionsForControlEvents(.TouchUpOutside)
}
}
override func cancelTrackingWithEvent(event: UIEvent?) {
sendActionsForControlEvents(.TouchCancel)
}
I've found also this answer but it doesn't seems to fit my issue, because when I add the target for .TouchUpInside event I don't get any action automatically from the event dispatcher as stated in that answer.