Iphone 第二次运行时动画不工作

Iphone 第二次运行时动画不工作,iphone,animation,Iphone,Animation,我正在运行这段代码来缩放和移动动画中的图像。它在第一次运行时工作得非常完美,但下次运行此代码时,图像不会动画化。 我已经在转换前后放置了NSLog语句,以查看图像是否受到影响。请参见代码下面的结果 -(void)animateShowImage:(UIButton *)button { //set the starting animate position CGRect frameRect = imageView.frame; frameRect.size.wid

我正在运行这段代码来缩放和移动动画中的图像。它在第一次运行时工作得非常完美,但下次运行此代码时,图像不会动画化。 我已经在转换前后放置了NSLog语句,以查看图像是否受到影响。请参见代码下面的结果

-(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.origin;
     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首次运行:

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第二次运行:

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

我不知道我做错了什么。欢迎任何帮助

在应用第二个动画之前,需要重置变换。在方法的开头,您可以添加:

imageView.transform = CGAffineTransformIdentity;
我对CoreAnimation没有足够的理解,无法恰当地解释为什么需要这样做,但希望有人能够偶然发现这个问题并启发我们

提示:如果您发现自己需要写出CGRect和CGPoint,可以使用方便的方法NSStringFromCGRect和NSStringFromCGPoint

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

请内联添加您的问题。@Georg-您有18k的声誉-您可以自己编辑;)@迪恩乌姆伯恩:我本来可以,但是把每件事都安排得很好比我当初花在OP应该做的事情上更多(这一次)。谢谢你这么做!