开发者

Animation not work when second run

I´m running this code to scale and move an image in animation. It works perfectly the first run, but the next time I run this code the image does not animate. I´ve put NSLog statements before and after the transforms to see if the image is affected. See the results below the code

-(void)animateShowImage:(UIButton *)button
{
     //set the starting animate position
     CGRect frameRect = imageView.frame;

     frameRect.size.width = button.frame.size.width;
     frameRect.size.height = button.frame.size.height;
     frameRect.origin = button.frame.origi开发者_如何学编程n;
     imageView.frame = frameRect;


     NSLog(@"imageview width : %f", imageView.frame.size.width);
     NSLog(@"imageview height : %f", imageView.frame.size.height);
     NSLog(@"imageview x : %f", imageView.frame.origin.x);
     NSLog(@"imageview y : %f", imageView.frame.origin.y);

     [UIView beginAnimations:nil context:NULL];
     [UIView setAnimationDuration:1.2];

     CGAffineTransform move = CGAffineTransformMakeTranslation(30, 20);
     CGAffineTransform scale = CGAffineTransformMakeScale(3,3);

     CGAffineTransform finalTransform = CGAffineTransformConcat(move, scale);
     imageView.transform = finalTransform;

     [UIView commitAnimations]; 

     NSLog(@"imageview width : %f", imageView.frame.size.width);
     NSLog(@"imageview height : %f", imageView.frame.size.height);
     NSLog(@"imageview x : %f", imageView.frame.origin.x);
     NSLog(@"imageview y : %f", imageView.frame.origin.y);
}

NSLog first run:

2009-10-19 19:13:41.339 MyProject[15847:207] imageview width : 48.000000
2009-10-19 19:13:41.341 MyProject[15847:207] imageview height : 45.000000
2009-10-19 19:13:41.341 MyProject[15847:207] imageview x : 8.000000
2009-10-19 19:13:41.342 MyProject[15847:207] imageview y : 57.000000
(gdb) continue
Current language:  auto; currently objective-c
2009-10-19 19:13:44.649 MyProject[15847:207] imageview width : 144.000000
2009-10-19 19:13:44.650 MyProject[15847:207] imageview height : 135.000000
2009-10-19 19:13:44.650 MyProject[15847:207] imageview x : 50.000000
2009-10-19 19:13:45.080 MyProject[15847:207] imageview y : 72.000000

NSLog second run:

2009-10-19 19:13:51.487 MyProject[15847:207] imageview width : 48.000000
2009-10-19 19:13:51.488 MyProject[15847:207] imageview height : 45.000000
2009-10-19 19:13:51.489 MyProject[15847:207] imageview x : 8.000000
2009-10-19 19:13:51.489 MyProject[15847:207] imageview y : 57.000000
(gdb) continue
2009-10-19 19:13:54.424 MyProject[15847:207] imageview width : 48.000000
2009-10-19 19:13:54.425 MyProject[15847:207] imageview height : 45.000000
2009-10-19 19:13:54.425 MyProject[15847:207] imageview x : 8.000000
2009-10-19 19:13:57.433 MyProject[15847:207] imageview y : 57.000000

I can´t figure out what I´m doing wrong. Any help is apprechiated!


You need to reset the transform before you can apply a second animation. At the start of the method you can add:

imageView.transform = CGAffineTransformIdentity;

I don't have enough understanding of CoreAnimation to properly explain why this is needed, but hopefully someone with that knowledge will stumble upon this question and enlighten us.

Tip: If you find yourself in need of writing out CGRect and CGPoint you can use the convenience methods NSStringFromCGRect and NSStringFromCGPoint.

NSLog(@"imageView.frame = %@", NSStringFromCGRect(imageView.frame));
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜