How to give 'image' Animations to multiple images
at Different
Locations
in a 'UIView'?
1) I had 3 images.
2) i want to Add these 3 Images
at 3 different locations
animatedly, means add one by one images animatedly
with some duration
??
How can I achieve this?
As per your requirement you want to animate image one by one
for that purpose use completion block of first image animation to animate second image and so on like below code
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) {
}];
}];
}];
It will Help you :)
Lets say 3 views are _redView
, _greenView
and _blueView
check out following method to move those from one to another location. I have used random location you can give specific if you want. Here you can have working example of it just select ViewAnimationsTest
project of the work space and run.
- (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
}];
}
Probably the easiest way to do this is to use 3 animations called with delay.
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];
}
Where myViews is array of views to be animated, frameForViewAtIndex: is the method that returns frame for corresponding view.