Lukas Lukas - 3 months ago 16
iOS Question

button press animation

So I've searched a lot but I didn't find an answer for my problem.

I want to make a button that would have two images, first image that is when button is in normal state, the other one when it is in highlighting state. The problem is that i want these images to change in animated manner, for example the first image fades out, when the second fades in. And they change in such a way that the animation time remains constant.

The only way I thought is, to make a custom button, add couple of

ImageViews
and on button touch down event, one
ImageView
fades in, other fades out, on button touch up event, I could do the opposite. But this method doesn't seem the most suitable. Is there a better option that I am not seeing?

Answer

I've done it the following way, similarly as Ilker Baltaci has described it: I subclassed UIButton (although it's not advised because it's a class cluster) and added a function to it:

- (void) blinkAndFlipButtonAnimationWithText: (NSString*) buttonText {
    [UIView animateWithDuration: 0.2 delay: 0 options: UIViewAnimationOptionCurveLinear 
                     animations: ^{ 
                         self.alpha = 0.1;
                     } 
                     completion: ^(BOOL finished) {
                         [UIView animateWithDuration: 0.2 delay: 0 options: UIViewAnimationOptionCurveLinear 
                                          animations: ^{ 
                                              self.alpha = 1;
                                          } 
                                          completion: ^(BOOL finished) {
                                              [UIView transitionWithView: self duration: 0.25 options: (UIViewAnimationOptionTransitionFlipFromBottom | UIViewAnimationOptionCurveEaseInOut)
                                                              animations: ^{ 
                                                                  [self setTitle: buttonText forState: UIControlStateNormal];
                                                              } 
                                                              completion: ^(BOOL finished) { 
                                                                  if ([self.delegate respondsToSelector: @selector(primaCustomButtonDidFinishFlipAnimation:)]) {
                                                                      [self.delegate primaCustomButtonDidFinishFlipAnimation: self];
                                                                  } 
                                                              }
                                               ];
                                          }
                          ];
                     }
     ];
}

a lot of block-animation cascades, ugly i know! But anyway, you can adapt the animation block to your needs and exclude the delegate callback.

then in your IBAction/target-action method for the button press just call the method, like:

- (IBAction) facebookButtonPresse: (id) sender {
    [self.facebookButton blinkAndFlipButtonAnimationWithText: @"+3"];
}
Comments