Annimation in iphone inside for loop not working - iphone

I am currently developing an application in iphone, i want to animate some images on screen and for the same i have written this code
for (int i = 1 ; i <= 5 ; i++)
{
imgview.image = [UIImage imageNamed: [NSString stringWithFormat:#"spleshScreen%d.png", 1]];
NSLog(#"i = %d" , i);
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDelegate:self];
[UIView setAnimationDuration:0.9];
[UIView setAnimationRepeatCount:5];
[UIView setAnimationBeginsFromCurrentState:YES];
imgview.image = [UIImage imageNamed: [NSString stringWithFormat:#"spleshScreen%d.png", i]];
i++;
imgview.frame = CGRectMake(imgview.frame.origin.x, (imgview.frame.origin.y + 180), imgview.frame.size.width, imgview.frame.size.height);
imgview.frame = CGRectMake(imgview.frame.origin.x, (imgview.frame.origin.y - 350), imgview.frame.size.width, imgview.frame.size.height);
[UIView commitAnimations];
}
if i remove the for loop code works fine, but with for loop it's not working neither it get any error
Please let me know what can be the problem

You can use the animationImages property of a UIImageView to handle the animation for you.
Here is a code sample:
imageView.animationImages = [NSArray arrayWithObjects:[UIImage imageNamed: [NSString stringWithFormat:#"spleshScreen%d.png", 1]],
[UIImage imageNamed: [NSString stringWithFormat:#"spleshScreen%d.png", 2]],
[UIImage imageNamed: [NSString stringWithFormat:#"spleshScreen%d.png", 3]],
[UIImage imageNamed: [NSString stringWithFormat:#"spleshScreen%d.png", 4]],
[UIImage imageNamed: [NSString stringWithFormat:#"spleshScreen%d.png", 5]],
nil]; // Don't forget to nil terminate this array!
[imageView setAnimationDuration:2.0]; // Define the amount of time it takes to go through one cycle of all the images.
// The property animationRepeatCount default value is 0, which specifies to repeat the animation indefinitely.
[imageView startAnimating];
In the last 2 lines of your code, you were changing the y origin of the frame of your image (adding 180 then substracting 350 which doesn't really make sense).
As you didn't speak about it in your question, I leaved this out.

UIView Class Reference:
The animations are run in a separate thread and begin when the application returns to the run loop.
So when you start your loop you create some threads and there is may be problems with that.
Try to delete your for loop and use setAnimationDidStopSelector method (see the UIView Class Reference):
[UIView setAnimationDidStopSelector:#selector(annimationDidFinish:)];
When your annimationDidFinish is called you can invoke a method to start an "other loop".
I haven't tested this solution, but I just give you another track to help you.

Related

UIImageView change Background Color and setImage not working during Animation

As the title said, it's very strange, did you agree?
So , if someone found your codes
imageView.backgroundColor = [UIColor greenColor];
or
imageView.image = [UIImage imageName:#"angryBird"];"
are not working. Just mind that if your imageView is animating , or whether the animation has been removed or not.
----------The Answer Below---------------
Just stop animating before you set image and change background of the animating UIImageView.
UIImageView* imageView = [UIImageView alloc] init];
//......do sth. like setFrame ...
imageView.images = [NSArray arrayWithObjects....]; // set the animation images array
imageView.animationDuration = 1.0;
[imageView startAnimating];
//......do sth.
[imageView stopAnimating]; // **** do not forget this!!!!! ****
imageView.backgroundColor = [UIColor greenColor];
imageView.image = [UIImage imageName:#"angryBird"];
When you performSelector: withObject: afterDey:1.0s , in the selector method , also, you need to stopAnimating too, and then setImage or change background color.
Try to change background color and changing the image once animation is complete. it will simply work for you.
Try this:
[UIView animateWithDuration:1.0 animations:^(){} completion:^(BOOL finished){}];
Try this,Animation using block
[UIView animateWithDuration:1.0 animations:^{
}
completion:^(BOOL finished)
{
[imageView setImage:[UIImage imageNamed:#"YOUR_IMAGE_NAME"]];
[imageView setBackgroundColor:[UIColor greenColor]];
}];
Note: It is imageNamed: not imageName: ,I am not sure how this get compiled
Try this code and performSelector:withObject:afterDelay:: in this way
[self performSelector:#selector(animationDidFinish:) withObject:nil
afterDelay:instructions.animationDuration];
or use dispatch_after:
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, instructions.animationDuration * NSEC_PER_SEC);
dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
[self animationDidFinish];
});
i hope it helps you

How to move an array of images randomly in iphone app

How can I move an array of images randomly in iPhone app.
Bellow code is for moving one image. I've to use an array of images.
- (void)moveImage:(UIImageView *)image duration:(NSTimeInterval)duration
curve:(int)curve x:(CGFloat)x y:(CGFloat)y
{
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:duration];
[UIView setAnimationCurve:curve];
[UIView setAnimationBeginsFromCurrentState:YES];
// The transform matrix
CGAffineTransform transform = CGAffineTransformMakeTranslation(x, y);
image.transform = transform;
// Commit the changes
[UIView commitAnimations];
}
- (void)viewDidLoad
{
[super viewDidLoad];
UIImageView *imageToMove =
[[UIImageView alloc] initWithImage:[UIImage imageNamed:#"fish.png"]];
imageToMove.frame = CGRectMake(70, 120, 100, 100);
[self.view addSubview:imageToMove];
[self moveImage:imageToMove duration:1.0
curve:UIViewAnimationCurveLinear x:0.0 y:110.0];
}
you can use this it might help you:
NSArray *animationArray=[NSArray arrayWithObjects:
[UIImage imageNamed:#"img1.png"],
[UIImage imageNamed:#"img2.png"],
[UIImage imageNamed:#"img3.png"],
[UIImage imageNamed:#"img4.png"],
nil];
UIImageView *animationView=[[UIImageView alloc]initWithFrame:CGRectMake(0, 0,320, 460)];
animationView.backgroundColor=[UIColor purpleColor];
animationView.animationImages=animationArray;
animationView.animationDuration=1.5;
animationView.animationRepeatCount=0;
[animationView startAnimating];
[self.view addSubview:animationView];
[animationView release];
This is just a conceptual code so please make changes according to your need.

Accessing the current position of UIView during animation

I am trying to find the current position of an iPhone ImageView that is animating across the screen.
I create and animate the imageView like so
-(IBAction)button:(UIButton *)sender{
UIImageView *myImageView =[[UIImageView alloc] initWithImage:
[UIImage imageNamed:#"ball.png"]];
myImageView.frame = CGRectMake(0, self.view.frame.size.height/2, 40, 40);
[self.view addSubview:myImageView];
[myArray addObject:myImageView];
[UIView animateWithDuration:.5
delay:0
options:(UIViewAnimationOptionAllowUserInteraction) // | something here?)
animations:^{
myImageView.frame = CGRectOffset(myImageView.frame, 500, 0);
}
completion:^(BOOL finished){
[myArray removeObject:myImageView];
[myImageView removeFromSuperview];
[myImageView release];
}
];
}
then to detect the current CGPoint of the imageView I call this method every 60th of a second
-(void)findPoint{
for (UIImageView *projectile in bullets) {
label.text = [NSString stringWithFormat:#"%f", projectile.center.x];
label2.text = [NSString stringWithFormat:#"%f", projectile.center.y];
}
}
However this only gives me the point where the animation ends, I want to get the current CGPoint of the image that is animating across the screen. Is there an option that I need to apply other than UIViewAnimationOptionAllowUserInteraction to do this? If not, how do I get the current position of an animating imageView?
You can use the presentationLayer - a property of the CALayer that "provides a close approximation to the version of the layer that is currently being displayed". Just use it like this:
CGRect projectileFrame = [[projectile.layer presentationLayer] frame];
Swift 5:
Get temporary position and size (CGRect) during the animation with:
let currentFrame = projectileFrame.layer.presentation()!.frame

collision of two images, problem with the declaration

I have a method with an image "imageView":
- (void)createNewImageView {
// Get the view's frame to make it easier later on
UIImage *image = [UIImage imageNamed:#"abouffer_03.png"];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
// Add it at a random point
[imageView setCenter:[self randomPointSquare]];
[[self view] addSubview:imageView];
// Animate it into place
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:8.0f];
[imageView setCenter:CGPointMake(240, 160)];
[UIView commitAnimations];
[imageView release];
}
and another image "viewToRotate" (IBOuutlet defined in .h and interface builder)
And I want to check the collision with this method :
- (void)myRunloop
{
// check collision
if( CGRectIntersectsRect(imageView.frame, viewToRotate.frame) )
{
viewToRotate.alpha=0.2;
}
}
But xcode always give me the error :"imageView undeclared" and I don't know how to solve this . I don't wanna define it again in this method.
I just'd been through a similar problem actually when you set the frame of the imageView on line
[imageView setCenter:CGPointMake(240, 160)];
the imageView gets settled at that point in the memory, It's just that due to being in animation block it's showing you the transition that imageView is being moved towards the destination but in actual the view's coordinates are assigned with the destination location, you can confirm it by logging coordinates right after the line of code. If you really need to do this the similar way, you can use the timer instead of the animation block and animate yourself. But I circumvent this problem by using the animationDelegate. Just set animation delegate to self and define animationDidStopSelector and you're up. You're animationDidstopSelector will get fired when the animation's ended and hence the object reached it's final destination or you can say there's a collision (on UI). Hope that helps
Here's a code sample:
- (void)createNewImageView {
// Get the view's frame to make it easier later on
UIImage *image = [UIImage imageNamed:#"abouffer_03.png"];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
// Add it at a random point
[imageView setCenter:[self randomPointSquare]];
[[self view] addSubview:imageView];
// Animate it into place
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDelegate:self];
[UIView setAnimationDidStopSelector:#selector(animationDidStop:)];
[UIView setAnimationDuration:8.0f];
[imageView setCenter:CGPointMake(240, 160)];
[UIView commitAnimations];
[imageView release];
}
your animationDidStopSelector will be just like:
-(void)animationDidStop:(id)sender {
//This is almost similar to collision detection, you can do something here
}
imageView is a local variable to the function createNewImageView and hence cannot be accessed by myRunloop
If you have declared imageView as an IBOutlet
you can set the image like this
UIImage *image = [UIImage imageNamed:#"abouffer_03.png"];
imageView.image = image
In Interface (.h file) declare like this
UIImageView *imageView;
And In your createNewImageView() method.Use the
imageView = [[UIImageView alloc] initWithImage:image];
You can assign a tag to the image view so you can find it later in the other method:
[imageView setTag:1];
// in myRunloop
UIImageView* imageView = [[self view] viewWithTag:1];

UIImage view animation problem

I have done the effect of playing lots of images like below:
NSInteger faceNum = 12;
NSMutableArray *faceArray = [[NSMutableArray alloc] initWithCapacity:faceNum];
for (int i = 1;i<faceNum+1; i++) {
NSString *facename = [[NSBundle mainBundle]
pathForResource:[NSString stringWithFormat:#"animationFace%d",i] ofType:#"png"];
UIImage *faceImage = [UIImage imageWithContentsOfFile:facename];
[faceArray addObject:faceImage];
}
UIImageView *faceView = [[UIImageView alloc]
initWithFrame:CGRectMake(414, 157, 161, 124)];
faceView.animationImages = faceArray;
faceView.animationDuration = 30;
faceView.animationRepeatCount = 0;
[faceView startAnimating];
[self.view addSubview:faceView];
[faceView release];
[faceArray release];
And how to add the EaseInEaseOut effect to this.One picture disappear gradually,then another picture appear gradually.
Thanks
There is no inbuilt fade-in and out feature with imageview startAnimating. You can achieve this by manually setting the alpha of two view laid over each other:
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.5f];
imageviewToFadeOut.alpha = 0.0f;
imageviewToFadeIn.alpha = 1.0f;
[UIView commitAnimations];
and setImage manually alternatively to these UIViews.
[imageview setImage:image];
and to recursively call this method, use something like [self performSelector:#selector(methodname) withObject: afterDelay: ] inside the method itself.
Edit: For clarity, specifying the recursion to call this method over and over with delay:
-(void)methodname {
.. (do your task)
[self performSelector:#selector(methodname) withObject:NULL afterDelay:10 ];
}