2010-04-12 62 views
2

我希望它是动画,所以我使用[UIView beginAnimations]动画移动和图像的比例的UIButton

我试着用button.imageView.frame动画(含图片设置为ImageView的),但它只是动画的位置,图像没有按根本不会缩小。

使用hateButton.frame(当图像设置为backgroundImage时),backgroundImage会缩小但不是动画效果。

如何动画缩放UIButton图像?

+0

请把代码一样... – 2010-04-12 07:09:07

回答

3

我可以做到这一点使用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]; 

确保导入Quartz2D太:

#import <QuartzCore/QuartzCore.h> 
+0

你可以申请通过button.imageView.transform设置为您CGAffineTransform直接转换到button.imageView。 – Kekoa 2011-03-23 15:53:23

+0

我决定使用UIImageView,因此可以使用普通的动画框架。然后在上面我放了一个无形的UIButton。 – Jonny 2011-04-12 10:17:28

5

你可以调用和[UIView commintAnimations]layoutIfNeeded之间[UIView beginAnimations][UIView animateWithDuration]

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