Search code examples
ios5uibuttoncore-animation

How to attach a click event to an animated button in ios


I am having a moveable button and would like to execute a method when the user click on that button - is that possible in ios?

I have write the following code but the animation needs to be finished in order to be able to trigger that function:

[arrowButton addTarget:self action:@selector(presentMainWindow) forControlEvents:UIControlEventTouchUpInside];


[UIView animateWithDuration:kAnimationDuration
                          delay:.2f
                        options:UIViewAnimationOptionCurveEaseInOut 
                     animations:
     ^{
         [arrowButton setTransform:CGAffineTransformMakeTranslation(kButtonShift, 0)];
         [arrowButton setFrame:CGRectMake(arrowButton.frame.origin.x + kButtonShift, arrowButton.frame.origin.y, arrowButton.frame.size.width, arrowButton.frame.size.height);
     } 
                     completion:nil]; 

Solution

  • I found out that you can simply add the option UIViewAnimationOptionAllowUserInteraction to the animation to handle both the animation and the interaction:

     [UIView animateWithDuration:kAnimationDuration
                              delay:.2f
                            options:UIViewAnimationOptionCurveEaseInOut | UIViewAnimationOptionAutoreverse | UIViewAnimationOptionAllowUserInteraction
                         animations:
         ^{
             [arrowButton setTransform:CGAffineTransformMakeTranslation(kButtonShift, 0)];
        } 
                         completion:nil];