iOS翻译和缩放animation

我正在研究UIButtonanimation,其中:

UIButton被设置在屏幕的底部中心,缩小到一个小的尺寸

 _menuBtn.transform = CGAffineTransformMakeScale(0.1f, 0.1f); 

当应用程序启动时,它应该移动到屏幕的左下angular,因为它会缩放或增长到原始大小。

 - (void)viewDidLoad { [super viewDidLoad]; _menuBtn.frame = CGRectMake(160, 513, 30, 30); _menuBtn.superview.frame = CGRectMake(160, 513, 30, 30); _menuBtn.transform = CGAffineTransformMakeScale(0.1f, 0.1f); NSLog(@"_menuBtn: %@ ; _menuBtn.superview: %@", _menuBtn, _menuBtn.superview); [UIView beginAnimations:nil context:nil]; [UIView setAnimationDelegate:self]; [UIView setAnimationDuration:5]; [UIView setAnimationCurve:UIViewAnimationCurveEaseOut]; CGAffineTransform scaleTrans = CGAffineTransformMakeScale(1.0f, 1.0f); CGAffineTransform lefttorightTrans = CGAffineTransformMakeTranslation(-200.0f,0.0f); _menuBtn.transform = CGAffineTransformConcat(scaleTrans, lefttorightTrans); [UIView commitAnimations]; } 

问题

当animation开始时,button开始从屏幕的右下方移动,而不是在底部的中心,它应该是。 任何帮助?

logging结果

 NSLog(@"%@", _myBtn); 2013-08-14 09:22:38.913 GJCoolNavi[339:c07] <UIButton: 0x813ea30; frame = (0 0; 0 0); opaque = NO; autoresize = TM+BM; layer = <CALayer: 0x813eaf0>> 

那在做animation之前…和animation之后的结果是:

 2013-08-14 09:30:25.719 GJCoolNavi[612:c07] <UIButton: 0x71206d0; frame = (160 294; 0 0); opaque = NO; autoresize = TM+BM; animations = { transform=<CABasicAnimation: 0x7536a80>; position=<CABasicAnimation: 0x7537dd0>; }; layer = <CALayer: 0x7120790>> 

你为什么不这样做…

 _menuBtn.transform = CGAffineTransformMakeScale(0.1, 0.1); [UIView animateWithDuration:5.0 options:UIViewAnimationOptionCurveEaseOut animations:^(){ _menuBtn.transform = CGAffineTransformMakeScale(1.0, 1.0); _menuBtn.center = self.view.center; } completion:nil]; 

我会避免使用转换移动的东西。 改变框架。

编辑

 - (void)viewDidLoad { [super viewDidLoad]; } - (void)viewDidAppear:(BOOL)animated { [super viewDidAppear:animated]; // for convenience I'm pulling these values out in to variables. float buttonWidth = _menuBtn.frame.size.width; float buttonHeight = _menuBtn.frame.size.height; float viewWidth = self.view.frame.size.width; float viewHeight = self.view.frame.size.height; // set the button frame to be the bottom center // note you shouldn't have to do this as Interface Builder should already place it there. // place the button in the horizontal center and 20 points from the bottom of the view. _menuBtn.frame = CGRectMake((viewWidth - buttonWidth) * 0.5, viewHeight - buttonHeight - 20, buttonWidth, buttonHeight); // scale the button down before the animation... _menuBtn.transform = CGAffineTransformMakeScale(0.1, 0.1); // now animate the view... [UIView animateWithDuration:5.0 delay:0.0 options:UIViewAnimationOptionCurveEaseOut animations:^{ _menuBtn.transform = CGAffineTransformIdentity; _menuBtn.frame = CGRectMake(viewWidth - buttonWidth - 20, viewHeight - buttonHeight - 20, buttonWidth, buttonHeight); } completion:nil]; } 

试试这个,让我知道发生了什么。

这解决了类似的问题。

应用(UIButton).imageViewanimation。

 [UIView beginAnimations:nil context:nil]; [UIView setAnimationDelegate:self]; [UIView setAnimationDuration:5]; [UIView setAnimationCurve:UIViewAnimationCurveEaseOut]; CGAffineTransform scaleTrans = CGAffineTransformMakeScale(1.0f, 1.0f); CGAffineTransform lefttorightTrans = CGAffineTransformMakeTranslation(-200.0f,0.0f); _menuBtn.imageView.transform = CGAffineTransformConcat(scaleTrans, lefttorightTrans); [UIView commitAnimations]; 

这种现象表明你的button的原始框架是错误的,可能是因为它的自动resize。 尝试在开始animation之前将其框架设置到底部中央。

 - (void)viewWillAppear:(BOOL)animated { [super viewWillAppear:animated]; _menuBtn.superview.frame = CGRectMake(0, 513, 320, 30); // This is a quick and dirty solution to make sure your button's superview is in the right place. You probably don't want to do this and are more likely to review your view hierarchy. _menuBtn.frame = CGRectMake(145, 0, 30, 30); // Set the frame to the bottom center, please ensure that _menuBtn's transform hasn't been changed before this step _menuBtn.transform = CGAffineTransformMakeScale(0.1f, 0.1f); [UIView beginAnimations:nil context:nil]; [UIView setAnimationDelegate:self]; [UIView setAnimationDuration:5]; [UIView setAnimationCurve:UIViewAnimationCurveEaseOut]; CGAffineTransform scaleTrans = CGAffineTransformMakeScale(1.0f, 1.0f); CGAffineTransform lefttorightTrans = CGAffineTransformMakeTranslation(-200.0f,0.0f); _menuBtn.transform = CGAffineTransformConcat(scaleTrans, lefttorightTrans); [UIView commitAnimations]; } 

我已经使用你的代码,并从底部中心向下移动完美。 可能是你的superview是不正确的。 请检查。