I have a animation assigned to the uiimageview with the following code
Fire.animationImages = [NSArray arrayWithObjects:
[UIImage imageNamed:#"firework1.png"],
[UIImage imageNamed:#"firework2.png"],
[UIImage imageNamed:#"firework3.png"],
[UIImage imageNamed:#"firework10.png"],nil];
[Fire setAnimationRepeatCount:0];
Fire.animationDuration = 0.5;
[Fire startAnimating];
I want the animation to stop when it reaches 0.5 I tried a if statement but it didn't work. Please help.
According to the UIImageView documentation setting the animation repeat count to 0 will make it repeat indefinitely, try using [Fire setAnimationRepeatCount:1]; instead.
Related
Ive looked around and couldn't find a simple method. I have an animation that plays when a collision occurs, although the animation can't get past the first frame until the objects are completely separated as the collision detection keeps firing.
Does anyone know how to either force the animation to play all the way through or have the collision detection only check once? (although the collision detection would need to be running again afterwards to repeat the process).
if (CGRectIntersectsRect(Heli.frame, AstroidBig.frame)) {
animation2.animationImages = [NSArray arrayWithObjects:
[UIImage imageNamed:#"Image 1"],
[UIImage imageNamed:#"Image 2"],
[UIImage imageNamed:#"Image 3"],
nil];
[animation2 setAnimationRepeatCount:1];
animation2.animationDuration = 0.4;
[animation2 startAnimating];
[self performSelector:#selector(enemyreset) withObject:nil afterDelay:1];
}
The 'enemyreset' is the function that will reset the object back into play from the collision point.
Set a flag in the above method to indicate that you are animating (you will need to add it as a property). Only start the animation if you are not already animating. Then reset the flag in your enemyreset method.
if (CGRectIntersectsRect(Heli.frame, AstroidBig.frame)) {
if (!self.alreadyAnimating) {
animation2.animationImages = [NSArray arrayWithObjects:
[UIImage imageNamed:#"Image 1"],
[UIImage imageNamed:#"Image 2"],
[UIImage imageNamed:#"Image 3"],
nil];
[animation2 setAnimationRepeatCount:1];
animation2.animationDuration = 0.4;
[animation2 startAnimating];
[self performSelector:#selector(enemyreset) withObject:nil afterDelay:1];
self.alreadyAnimating = YES;
}
}
I am trying to set up a button that plays an animation in an image view that's part of my root view controller and then proceed to change the sub view controller.
But for some reason, the code changes the view controller and THEN plays the animation.
I would think that the animation segment would be implemented before the view controller change takes place because, well, it's first in the code.
The code does everything fine, it just does it backwards. Any one have any ideas of what's going wrong?
- (IBAction)next:(UIButton *)sender {
clouds.animationImages = [NSArray arrayWithObjects:
[UIImage imageNamed:#"cloudAnim.0001.png"],
[UIImage imageNamed:#"cloudAnim.0002.png"],
[UIImage imageNamed:#"cloudAnim.0003.png"],
[UIImage imageNamed:#"cloudAnim.0004.png"],
[UIImage imageNamed:#"cloudAnim.0005.png"],
[UIImage imageNamed:#"cloudAnim.0006.png"],
[UIImage imageNamed:#"cloudAnim.0007.png"],
[UIImage imageNamed:#"cloudAnim.0008.png"],
[UIImage imageNamed:#"cloudAnim.0009.png"],
[UIImage imageNamed:#"cloudAnim.0010.png"],
[UIImage imageNamed:#"cloudAnim.0011.png"],
[UIImage imageNamed:#"cloudAnim.0012.png"],
[UIImage imageNamed:#"cloudAnim.0013.png"],
[UIImage imageNamed:#"cloudAnim.0014.png"],
[UIImage imageNamed:#"cloudAnim.0015.png"],
[UIImage imageNamed:#"cloudAnim.0016.png"],
[UIImage imageNamed:#"cloudAnim.0017.png"],
[UIImage imageNamed:#"cloudAnim.0018.png"],
[UIImage imageNamed:#"cloudAnim.0019.png"],
[UIImage imageNamed:#"cloudAnim.0020.png"],
[UIImage imageNamed:#"cloudAnim.0021.png"],
[UIImage imageNamed:#"cloudAnim.0022.png"],
[UIImage imageNamed:#"cloudAnim.0023.png"],
[UIImage imageNamed:#"cloudAnim.0024.png"],
[UIImage imageNamed:#"cloudAnim.0025.png"],
[UIImage imageNamed:#"cloudAnim.0026.png"],
[UIImage imageNamed:#"cloudAnim.0027.png"],
[UIImage imageNamed:#"cloudAnim.0028.png"],
[UIImage imageNamed:#"cloudAnim.0029.png"],
[UIImage imageNamed:#"cloudAnim.0030.png"],
[UIImage imageNamed:#"cloudAnim.0031.png"],
[UIImage imageNamed:#"cloudAnim.0032.png"],
[UIImage imageNamed:#"cloudAnim.0033.png"],
[UIImage imageNamed:#"cloudAnim.0034.png"],
[UIImage imageNamed:#"cloudAnim.0035.png"],
[UIImage imageNamed:#"cloudAnim.0036.png"],
[UIImage imageNamed:#"cloudAnim.0037.png"],
[UIImage imageNamed:#"cloudAnim.0038.png"],
[UIImage imageNamed:#"cloudAnim.0039.png"],
[UIImage imageNamed:#"cloudAnim.0040.png"],
[UIImage imageNamed:#"cloudAnim.0041.png"],
[UIImage imageNamed:#"cloudAnim.0042.png"],
[UIImage imageNamed:#"cloudAnim.0043.png"],
[UIImage imageNamed:#"cloudAnim.0044.png"],
[UIImage imageNamed:#"cloudAnim.0045.png"],
[UIImage imageNamed:#"cloudAnim.0046.png"],
[UIImage imageNamed:#"cloudAnim.0047.png"],
[UIImage imageNamed:#"cloudAnim.0048.png"], nil];
[clouds setAnimationRepeatCount:1];
clouds.animationDuration = 2.0;
[clouds startAnimating];
if (currentPage == 1){
self.page02ViewController = [self.storyboard instantiateViewControllerWithIdentifier:#"page02"];
[self.view insertSubview:_page02ViewController.view atIndex:0];
[self.page01ViewController.view removeFromSuperview];
}
if (currentPage == 2){
self.page03ViewController = [self.storyboard instantiateViewControllerWithIdentifier:#"page03"];
[self.page02ViewController.view removeFromSuperview];
[self.view insertSubview:_page03ViewController.view atIndex:0];
}
currentPage = currentPage +1;
if (currentPage >3){
currentPage = 3;
}
}
Animations are asynchronous, meaning the code below will be executed immediately.
If you want to wait until one loop of the animation has completed, you'll need to delay the code afterward. The simplest way is to use dispatch_after:
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
// Your view controller switching code will be executed after 2 seconds...
});
Beware that, since the method returns immediately, there is nothing stopping the user from hitting the button again and queuing up another transition. You may wish to disable user interaction during the transition, though a 2-second transition will feel like an eternity to the user if they have to switch often.
This question already has answers here:
iOS: How to detect when an animation is finished?
(3 answers)
Closed 8 years ago.
I have this code for my animation:
facebookImage.animationImages = [NSArray arrayWithObjects:
[UIImage imageNamed:#"Animation01.png"],
[UIImage imageNamed:#"Animation02.png"],
[UIImage imageNamed:#"Animation03.png"],
[UIImage imageNamed:#"Animation04.png"],
[UIImage imageNamed:#"Animation05.png"],
nil];
facebookImage.animationDuration = 1.3;
facebookImage.animationRepeatCount = 1;
[facebookImage startAnimating];
How do I use delegate callback (or something else) for doing something after this animation is complete?
I can't do this :
[UIView setAnimationDelegate:self];
because I don't have this option in UIImageview
thanks :)
just add like this:
float animationDuration = 1.3;
facebookImage.animationImages = [NSArray arrayWithObjects:
[UIImage imageNamed:#"Animation01.png"],
[UIImage imageNamed:#"Animation02.png"],
[UIImage imageNamed:#"Animation03.png"],
[UIImage imageNamed:#"Animation04.png"],
[UIImage imageNamed:#"Animation05.png"],
nil];
facebookImage.animationDuration = animationDuration;
facebookImage.animationRepeatCount = 1;
[facebookImage startAnimating];
[self performSelector:#selector(someMethodDelayedToEndOfAnimation) withObject:nil afterDelay: animationDuration];
In Xcode I've got 2 buttons: Start and Stop.
When you click Start startClickis called, when you click Stop stopClick is called.
When you click Start a UIImageView image will flash different images from the NSArray. When you click Stop, it stops, but I want it to stop on the image that was showing when I clicked Stop. At the moment it just stops and disapears... Any help?
Heres the part of the code:
- (IBAction)startClick:(id)sender {
color.animationImages = [NSArray arrayWithObjects:
[UIImage imageNamed: #"img1.png"],
[UIImage imageNamed: #"img2.png"],
[UIImage imageNamed: #"img3.png"],
[UIImage imageNamed: #"img4.png"],nil];
[color setAnimationRepeatCount: 0];
color.animationDuration = 1;
[color startAnimating];
}
- (IBAction)stopClick:(id)sender {
[color stopAnimating];
//What code do i put here to display the last image? (as i clicked stop).
}
I would also like to add if statments for if the color displayed is img1.png then do this... and if its img2.png do that...
This code will pause your Animation:
color.frame = [[color.layer presentationLayer] frame];
[color.layer removeAllAnimations];
I have successfully created a UIView which plays an animation of images from an NSArray of images. I am trying to get this image to move at the same time the animation is playing. Use of the .center property of UIView just isn't working, and I must have some sort of syntactic error, which I cannot for the life of me figure out.
Original post:
I'm playing with brandontreb's tutorial. I can successfully get the animated sprite to dance in place, but I want it to actually scoot across the screen. Can some dear soul:
Please help me fix what I'm doing wrong to make the center of the UIimage
actually move across screen?
Or even better, update this to
"animation block" best practices?
I will surely study your fixes for hours.
- (IBAction)startWalking:(UIButton *)sender {
NSArray * imageArray = [[NSArray alloc] initWithObjects:
[UIImage imageNamed:#"1.png"],
[UIImage imageNamed:#"2.png"],
[UIImage imageNamed:#"3.png"],
[UIImage imageNamed:#"4.png"],
[UIImage imageNamed:#"5.png"],
[UIImage imageNamed:#"6.png"],
[UIImage imageNamed:#"7.png"],
[UIImage imageNamed:#"8.png"],
[UIImage imageNamed:#"9.png"],
[UIImage imageNamed:#"10.png"],
[UIImage imageNamed:#"11.png"],
[UIImage imageNamed:#"12.png"],
nil];
UIImageView * ryuJump = [[UIImageView alloc] initWithFrame:
CGRectMake(100, 125, 150, 130)];
ryuJump.animationImages = imageArray;
ryuJump.animationDuration = 1.1;
ryuJump.animationRepeatCount=5;
CGPoint p = ryuJump.center;
p.x += 100;
ryuJump.center = p;
[UIView animateWithDuration:20
delay:0
options: UIViewAnimationOptionCurveEaseIn
animations:^{
ryuJump.center = CGPointMake(p.x, p.y);
}
completion:^(BOOL finished){
}];
ryuJump.contentMode = UIViewContentModeBottomLeft;
[self.view addSubview:ryuJump];
[ryuJump startAnimating];
}
Remove:
ryuJump.center = p;
You're basically telling uiview to animate to the exact same position you just set it to (so nothing will happen).