2012-04-27 52 views
1

的代码是非常基本的:的UIView动画不起作用

UIImageView *testView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 80, 80)]; 
[self.view addSubview:testView]; 
[testView setImage:[UIImage imageNamed:@"button.png"]]; 
[UIView animateWithDuration:100 
       animations:^{testView.center = CGPointMake(140,350);} 
       completion:^(BOOL finished){NSLog(@"animation finished"); 
       } 
]; 

不知怎么动画只是将无法正常工作。图像只显示在结束位置。 NSLog消息确实显示。

有谁知道什么可能会失败的动画?

非常感谢你。

+0

我不知道100秒意。确保您已将button.png添加到您的项目中。我只是复制你的代码并用10秒进行测试,没有问题。 – HMHero 2014-05-11 21:41:13

回答

0
[self.view addSubview:testView]; 

后添加一个subview..the UI不会更新instantly..it需要一点时间来refresh..However动画调用之前,它可以调用它update..so的UI更新只有在动画完成后..我会建议在动画之前调用[self.view setNeedsDisplay] ..或稍微延迟后调用动画

+0

谢谢,但我都试过你的建议(setNeedsDisplay和动画延迟),但它仍然无法正常工作... – ceriseche 2012-04-27 20:10:55

0

我不确定粘贴的代码是否是您的确切片段!它有100秒的动画持续时间。这是一种有意的行为吗?

还尝试改变帧,而不是直接中心。

UIImageView *testView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 80, 80)]; 
[self.view addSubview:testView]; 
[testView setImage:[UIImage imageNamed:@"button.png"]]; 
[UIView animateWithDuration:0.5 
       animations:^{testView.frame = CGRectMake(140,350,80,80);} 
       completion:^(BOOL finished){NSLog(@"animation finished"); 
       } 
]; 
+0

我只想补充一点,每个苹果的UIView的文档,与UIView的frame属性将动画如果transform属性不等于标识转换,则不起作用。 transform的默认值是标识转换。当transform属性具有其他值时,需要使用中心和边界属性进行动画处理。 – bneely 2013-02-13 23:58:34

0

您在动画代码中缺少几行。

[UIView animateWithDuration:100 
    delay:0 
    options:UIViewAnimationOptionAllowAnimatedContent 
    animations:^{ 
     testView.center = CGPointMake(140,350); 
    } completion:^(BOOL finished){NSLog(@"animation finished")} 
]; 

这些动画是独一无二的iOS 7只,而且比你上面例举有什么更好的解决方案。

你可能早已回答了这个,但我希望这个答案可以帮助其他人跨越这个答案绊倒。解决方案

0

未提供在这里的实际工作,因为他很可能在代码中的错误的地方叫他的轮换代码。

这里调用它,它会工作

-(void)viewDidAppear:(BOOL)animated 
{ 
    [super viewDidAppear:YES]; 

} 
0

添加QuartzCore框架:

#import <QuartzCore/QuartzCore.h>