Search code examples
objective-cuiimageviewtouchesbegan

Fully Executing Action With touchesBegan


I'm using - (void)touchesBegan... to tell if the user has tapped anywhere on the screen. Once the user taps on the screen, an action is executed. However, whenever that person lifts up their finger, the action is halted. Specifically I am running through imageView.animateImages once when the user taps. I want to make it so that if they tap and let go, the animation will continue to run through (I have the repeatCount set to 1). Does anyone know how to do this? Thanks in advance!


Solution

  • You should probably use NSNotifications. Try something like this:

    ...
    
    NSNotificationCenter *nc = [NSNotificationCenter defaultCenter]; 
    [nc addObserver:self selector:@selector(nameOfAnimationMethod) name:@"touched" object:nil];
    
    -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
        [nc postNotificationName:@"touched" object:self]; 
    } 
    

    Let me run you through what this does. It creates a notification center, and adds the current object as an observer to the notification center, listening for the notification "touched". When the notification center posts the said notification, the object runs the method (selector) "nameOfAnimationMethod". If you look at the touchesBegan method override, you'll see that I posted a notification by the name "touched". This will trigger the object to run the method that you wanted it to.