如何为 UIView 中不同位置的多个图像提供图像动画?

How to give image Animations to multiple images at Different Locations in a UIView?

How to give 'image' Animations to multiple images at Different Locations in a 'UIView'?

1) 我有 3 张图片。

2) 我想在 3 different locations 添加这些 3 Images 动画,意思是将图像 animatedlysome duration 一张一张添加??

如何实现?

假设 3 个视图是 _redView_greenView_blueView 查看以下方法将它们从一个位置移动到另一个位置。我使用了随机位置,您可以根据需要提供具体位置。 Here you can have working example of it 只是 select ViewAnimationsTest 作品的项目 space 和 运行.

- (int) randomFloatingGenerator : (int) max{
    return arc4random() % max;
}

- (void) animate3Views {
    [UIView animateWithDuration:2.0
                     animations:^{
                         _redView.transform = CGAffineTransformMakeTranslation([self randomFloatingGenerator : 270], [self randomFloatingGenerator:480]);
                         _greenView.transform = CGAffineTransformMakeTranslation([self randomFloatingGenerator : 270], [self randomFloatingGenerator:480]);
                         _blueView.transform = CGAffineTransformMakeTranslation([self randomFloatingGenerator : 270], [self randomFloatingGenerator:480]);

                     } completion:^(BOOL finished) {
                         [self animate3Views];//if don;t want continuous translation remove this line
                     }];
}

可能最简单的方法是使用 3 个延迟调用的动画。

    for (NSInteger i = 0; i < 3; i++) {
        UIView *view = myViews[i];
        CGRect frame = [self frameForViewAtIndex: i];
        NSTimeInterval duration = 0.5;
        NSTimeInterval delay = i * 0.5;

        [UIView animateWithDuration: duration delay: delay options: 0 animations: ^{
            view.frame = frame;
        } completion: nil];
    }

其中myViews是要动画的视图数组,frameForViewAtIndex:是returns帧对应视图的方法.

根据您的要求,您想要一张一张地制作动画图像

为此目的,使用第一个图像动画的完成块来为第二个图像设置动画等等,如下面的代码

UIImageView *imgView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:[NSString stringWithFormat:@"-t%d",1]]];
[imgView setBackgroundColor:[UIColor redColor]];

UIImageView *imgView2 = [[UIImageView alloc] initWithImage:[UIImage imageNamed:[NSString stringWithFormat:@"-t%d",2]]];
[imgView2 setBackgroundColor:[UIColor redColor]];

UIImageView *imgView3 = [[UIImageView alloc] initWithImage:[UIImage imageNamed:[NSString stringWithFormat:@"-t%d",2]]];
[imgView3 setBackgroundColor:[UIColor redColor]];

[self.view addSubview:imgView];
[self.view addSubview:imgView2];
[self.view addSubview:imgView3];

[UIView animateWithDuration:0.5 delay:1.0 options:UIViewAnimationOptionLayoutSubviews animations:^{

    [imgView setFrame:CGRectMake(0, 0, 150, 150)];


} completion:^(BOOL finished) {

    [UIView animateWithDuration:0.5 delay:1.0 options:UIViewAnimationOptionLayoutSubviews animations:^{
        [imgView2 setFrame:CGRectMake(self.view.frame.size.width - 150, 0, 150, 150)];

    } completion:^(BOOL finished) {

        [UIView animateWithDuration:0.5 delay:1.0 options:UIViewAnimationOptionLayoutSubviews animations:^{
            [imgView3 setFrame:CGRectMake(0, self.view.frame.size.height - 150, 150, 150)];

        } completion:^(BOOL finished) {


        }];

    }];

}];

它会帮助你:)