开发者

Animate move and scale of image UIButton

开发者 https://www.devze.com 2022-12-26 17:47 出处:网络
I want it to be animated so I\'m using [UIView beginAnimations]开发者_StackOverflow中文版 I\'ve tried animating using button.imageView.frame (with image set as imageView) but it only animates the po

I want it to be animated so I'm using [UIView beginAnimations]开发者_StackOverflow中文版

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?


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];
 }];


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>
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号