我的代码可以很好地运行我的动画。问题是我希望整个动画移动到屏幕左侧然后停止并重新开始。基本上它是一只熊动画,但我需要整个动画在屏幕上向左移动。我不希望它以不同的方式设置动画,我只需要改变它的 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 = 图片; animationImageView.animationDuration = 0.5;
[self.view addSubview:animationImageView];
[animationImageView startAnimating];
// Do any additional setup after loading the view, typically from a nib.
最佳答案
在您发布的代码下方添加:
[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
标志即可。
尝试显示的选项以获得所需的结果。
关于ios - Objective C 动画我想在屏幕上移动动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24092799/