如何在UIView的不同位置给图像animation多个图像?
如何在'UIView'中的
Different Locations
给“图像”animationmultiple images
?
1)我有3个图像。
2)我想要在3 different locations
添加这3 Images
animation,意味着在some duration
animatedly
添加一个个图像?
我怎样才能做到这一点?
根据你的要求,你想要一个接一个地animation
为此目的使用第一图像animation的完成块来animation第二图像等,如下面的代码
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) { }]; }]; }];
它会帮助你:)
可以说3个视图是_redView
, _greenView
和_blueView
检查下面的方法将这些从一个位置移动到另一个位置。 如果你愿意的话,我可以使用随机的位置。 在这里你可以通过select工作空间的ViewAnimationsTest
项目来运行。
- (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个带有延迟的animation。
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是animation的视图数组, frameForViewAtIndex:是为相应的视图返回帧的方法。