Objective C动画我想在屏幕上移动动画

时间:2014-06-07 01:31:26

标签: ios objective-c animation uiimageview

我的代码运行正常。问题是我希望整个动画移动到屏幕左侧然后停止并重新开始。基本上它是一个动画的熊,但我需要整个动画在屏幕左侧移动。我不希望它以不同的方式动画我只需要它的x值来改变,但我被卡住了。有人可以请帮助。这是我到目前为止所拥有的。

  • (无效)viewDidLoad中

{

[super viewDidLoad];

NSArray *imageNames = @[@"bear1.gif", @"bear2.gif", @"bear3.gif", @"bear4.gif",
                        @"bear5.gif", @"bear6.gif"];

NSMutableArray *images = [[NSMutableArray alloc] init];
for (int i = 0; i < imageNames.count; i++) 
{

    [images addObject:[UIImage imageNamed:[imageNames objectAtIndex:i]]];
}

// Normal Animation

UIImageView * animationImageView = [[UIImageView alloc] initWithFrame:CGRectMake(60,95,50,50)];     animationImageView.animationImages = images;     animationImageView.animationDuration = 0.5;

[self.view addSubview:animationImageView];
[animationImageView startAnimating];
// Do any additional setup after loading the view, typically from a nib.

}

1 个答案:

答案 0 :(得分:2)

在您发布的代码下方添加以下内容:

[UIView animateWithDuration:5 delay:1 options:UIViewAnimationOptionAutoreverse | UIViewAnimationOptionRepeat animations:^{
    [UIView setAnimationRepeatCount:4];
    animationImageView.frame = CGRectMake(160, 95, 50, 50);
} completion:^(BOOL finished) {
    animationImageView.frame = CGRectMake(60, 95, 50, 50);
}];

这会将熊沿着x轴向右移动,花费5秒钟完成每个动画循环,并重复动画4次。在完成每个循环后,它还会将动画自动反转到其原始位置。

如果您不希望它平稳地反转,而是在完成每个周期后将其快速恢复到原始位置,只需删除UIViewAnimationOptionAutoreverse标记。

使用显示的选项来实现所需的结果。

相关问题