samvermette
samvermette

Reputation: 40437

Animate move and scale of image UIButton

I want it to be animated so I'm using [UIView beginAnimations]

I've tried animating using button.imageView.frame (with image set as imageView) but it only animates the position, the image doesn't get scaled down at all.

using hateButton.frame (when image is set as backgroundImage), the backgroundImage gets scaled down but it's not animated.

How do I animate-scale a UIButton image?

Upvotes: 2

Views: 3567

Answers (2)

Mérion
Mérion

Reputation: 51

You can call layoutIfNeeded between [UIView beginAnimations] and [UIView commintAnimations] or with [UIView animateWithDuration].

[UIView animateWithDuration:0.3f animations:^{
    self.myButton.frame = aFrame;    
    [self.myButton layoutIfNeeded];
 }];

Upvotes: 5

samvermette
samvermette

Reputation: 40437

I could achieve this using Quartz 2D:

// move anchor point without moving frame
CGRect oldFrame = hateButton.frame;
hateButton.layer.anchorPoint = CGPointMake(0, 1); // bottom left
hateButton.frame = oldFrame;

[UIView beginAnimations:nil context:NULL];  
CGAffineTransform newTransform;
newTransform = CGAffineTransformMakeScale(0.8, 0.8);
hateButton.transform = CGAffineTransformTranslate(newTransform, 0, -160);
[UIView commitAnimations];

Make sure you import Quartz2D too:

#import <QuartzCore/QuartzCore.h>

Upvotes: 3

Related Questions