How can I register UIControlEvents?

I am trying to figure out how to register UIControlEvents so that I can see exactly which ones I want to add to my UIButton.

I can’t figure it out. Here is what I have tried so far:

[button addTarget:self action:@selector(buttonTouched:forEvent:) forControlEvents:UIControlEventAllEvents];


-(void)buttonTouched:(UIButton *)button forEvent:(UIControlEvents)event{

NSLog(@"%s", __PRETTY_FUNCTION__);

switch (event) {
    case UIControlEventTouchDown:
        NSLog(@"UIControlEventTouchDown");
        break;
    case UIControlEventTouchDownRepeat:
        NSLog(@"UIControlEventTouchDownRepeat");
        break;
    case UIControlEventTouchDragInside:
        NSLog(@"UIControlEventTouchDragInside");
        break;
    case UIControlEventTouchDragOutside:
        NSLog(@"UIControlEventTouchDragOutside");
        break;
    case UIControlEventTouchDragEnter:
        NSLog(@"UIControlEventTouchDragEnter");
        break;
    case UIControlEventTouchDragExit:
        NSLog(@"UIControlEventTouchDragExit");
        break;
    case UIControlEventTouchUpInside:
        NSLog(@"UIControlEventTouchUpInside");
        break;
    case UIControlEventTouchUpOutside:
        NSLog(@"UIControlEventTouchUpOutside");
        break;
    case UIControlEventTouchCancel:
        NSLog(@"UIControlEventTouchCancel");
        break;
    case UIControlEventValueChanged:
        NSLog(@"UIControlEventValueChanged");
    default:
        break;
}
}

The only print magazine is a magazine __PRETTY_FUNCTION__. I also tried this, just in case the switch issue was a problem:

if (event == UIControlEventTouchDown) {
    NSLog(@"UIControlEventTouchDown");
}
if (event == UIControlEventTouchDownRepeat){
    NSLog(@"UIControlEventTouchDownRepeat");
}
etc...

Still nothing is being recorded. What am I doing wrong?

Edit: I experimented further by changing which event I am adding to the button like this:

[button addTarget:self action:@selector(buttonTouched:forEvent:) forControlEvents:UIControlEventTouchDown];

And then I registered the input very specifically:

-(void)buttonTouched:(UIButton *)button forEvent:(UIControlEvents)event{

    NSLog(@"UIControlEventTouchDown = %i", UIControlEventTouchDown);
    NSLog(@"event = %i", event);
}

Here is the result:

UIControlEventTouchDown = 1
event = 350486320

Does anyone know what is going on?

2: TAKeanice , . , , UIEvent (UIControlEvents), . , , , UIControlEvents, , . Apple UIControl:

, UIControl :

, sendAction: to: forEvent:, , UIControlEvents , .

(, ). , : beginTrackingWithTouch: withEvent:, continueTrackingWithTouch: withEvent:, endTrackingWithTouch: withEvent:

UIControlEvents, , sendAction:to:forEvent:, UIEvent? ?

+3
2

:

// somewhere
[self.btn addTarget:self action:@selector(buttonTouched:forEvent:) forControlEvents:UIControlEventAllEvents];


- (void)buttonTouched:(UIButton*)sender forEvent:(id)event {
    NSSet *touches = [event allTouches];
    UITouch *touch = [touches anyObject];
    UITouchPhase phase = touch.phase;
}

UPDATE:
, , ,

:

:

  • UITouchPhaseEnded + = > UITouchUpInside
  • UITouchPhaseEnded + = > UITouchUpOutside
  • ...
+2

"UIControlEvent", "UIEvent" , .

: UIEvent - . UIEventType,

  • UIEventTypeTouches
  • UIEventTypeMotion
  • UIEventTypeRemoteControl

, , , remoteControl.

https://developer.apple.com/library/ios/documentation/uikit/reference/UIEvent_Class/Reference/Reference.html

, ControlEventType , "" , .

, UIControlEvents.

---- ----
UIControl , , , ..

+1

All Articles