Xcode UIView动画没有';行不通

Xcode UIView动画没有';行不通,xcode,animation,uiview,uiimageview,Xcode,Animation,Uiview,Uiimageview,代码非常基本: UIImageView *testView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 80, 80)]; [self.view addSubview:testView]; [testView setImage:[UIImage imageNamed:@"button.png"]]; [UIView animateWithDuration:100 animations:^{testV

代码非常基本:

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消息确实会显示出来

有人知道什么会使动画失败吗?

多谢各位

[self.view addSubview:testView];

添加子视图后..UI不会立即更新..刷新需要一点时间..但是在调用动画更新之前会调用动画..因此UI仅在动画完成后更新。。我建议在播放动画之前调用
[self.view setNeedsDisplay]
。。或者在稍微延迟后调用动画

我不确定粘贴的代码是否就是您的作品!!它的动画持续时间为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");
                 }
 ];

动画代码中缺少几行。

[UIView animateWithDuration:100 
    delay:0 
    options:UIViewAnimationOptionAllowAnimatedContent 
    animations:^{
        testView.center =  CGPointMake(140,350);
    } completion:^(BOOL finished){NSLog(@"animation finished")}
];
这些动画仅适用于iOS 7,是一种比您上面举例说明的更好的解决方案


您可能早就回答了这个问题,但我希望这个答案能帮助其他偶然发现这个答案的人。

这里提供的任何解决方案都不起作用,因为他可能在代码中的错误位置调用了他的旋转代码

在这里打电话就行了

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

}

添加QuartzCore框架:

#import <QuartzCore/QuartzCore.h>
#导入

谢谢,但我已经尝试了你的两个建议(setNeedsDisplay和animation delay),但仍然不起作用……我只想补充一点,根据Apple的UIView文档,如果变换属性不等于标识变换,则使用UIView帧属性制作动画将不起作用。transform的默认值是identity transform。当transform属性有其他值时,有必要使用center和bounds属性设置动画。我不知道100秒的时间。确保已将button.png添加到项目中。我只是复制了你的代码,并在10秒内进行了测试,没有问题。