Timer in my app; delay in between commands - ios

Okay, so I am making an app somewhat similar to flappy bird, but instead using a helicopter and a tap and hold mechanic instead of a tap, first of all when I press and hold I have the helicopter file a picture of the helicopter going up, and when I let go, I have the file a picture of it going down. To make it more look fluid I want to create an animation of just simply shifting the helicopter up/down about 10 degrees every .05ish seconds, how would I right a timer to be able to do this. This is the command that switches the image file:
Heli.image = [UIImage imageNamed:#"HeliUp.png"];
or
Heli.image = [UIImage imageNamed:#"Helidown.png"];
Basically I want this:
Heli.image = [UIImage imageNamed:#"HeliUp1.png"];
(wait .05 seconds)
Heli.image = [UIImage imageNamed:#"Heli2.png"];
(wait .05 seconds)
Heli.image = [UIImage imageNamed:#"HeliUp5.png"];
Something along the lines of that, however I know you need to use a timer with intervals and such..
SECOND of all, how would I make it so that I only can move the helicopter up and down when this animation isn't going on, so it doesn't look stupid.
Any help is appreciated, thanks.

In this case, the animation is straight-forward enough that you can handle it all within the UIImageView:
Heli.image = normalImage;
Heli.animationImages = #[ normalImage, upImage, normalImage, downImage ];
Heli.animationDuration = 4 * .05;
Heli.animationRepeatCount = 0;
To stop the animation just set animationImages to nil, to restart the animation reset animationImages = #[ ... ]
When you assign an array of images to animationImages, the images will be cycled through at a rate determined by animationDuration. If you subsequently assign nil to animationImages, then the the image property will be used to determine what is displayed. So, by assigning either nil or the array #[ normalImage, upImage, normalImage, downImage ] you can turn animation on and off.

Related

What is the best way to animate images to my apporach?

I have used
image.animationImages = [NSArray arrayWithObjects:
[UIImage imageNamed:#"image1"],
[UIImage imageNamed:#"image2"],
[UIImage imageNamed:#"image3"],
[UIImage imageNamed:#"image4"], nil];
[image setAnimationRepeatCount:1];
image.animationDuration = 1;
[image startAnimating];
but it gives a flip book effect to the animation. What I am looking for is a smooth animation to set each image at given time. I have currently is NSTimers to trigger a method then I have code within the method to set a image and another NSTimer to set another image which is in a method so fourth. It gives the animation I am looking for but the problem I am facing is that I am developing a game and I want when the user pauses the game it stops on the current image it is on. The game runs on a NSTimer which triggers a method. All I do to pause the game is to invalidate the NSTimer.
What approach should I take?

Whats the best way to animate images to my apporach?

I have used
image.animationImages = [NSArray arrayWithObjects:
[UIImage imageNamed:#"image1"],
[UIImage imageNamed:#"image2"],
[UIImage imageNamed:#"image3"],
[UIImage imageNamed:#"image4"], nil];
[image setAnimationRepeatCount:1];
image.animationDuration = 1;
[image startAnimating];
but it gives a flip book effect to the animation. What I am looking for is a smooth animation to set each image at given time. I have currently is NSTimers to trigger a method then I have code within the method to set a image and another NSTimer to set another image which is in a method so fourth. It gives the animation I am looking for but the problem I am facing is that I am developing a game and I want when the user pauses the game it stops on the current image it is on. The game runs on a NSTimer which triggers a method. All I do to pause the game is to invalidate the NSTimer.
What approach should I take?
you should add NSTimer and user_flag input.
This user_flag input you can declare as static int user_flag = 0
and when you are doing stop then simply make this variable as user_flag = 1.
Now implement the logic in all this condition that will look something like this one :
if(NSTimer_obj == condition && user_flag == 0)
{
//your code...
}

Morphing with CoreAnimation

I am creating an Navigation Button. When user presses it, the image of the button should change, reflecting it's state (e.g. menu opened / closed). I decided to do a morphing-liek animation for this. Is it possible to implement with CoreAnimation? What animation type do I need to use?
I attached the picture to clearly illustrate what I want. Also, you might see these animations in "Pimp My Ride" show.
You can use animationImages property of UIImageView for this, like this:
myImageView = [[UIImageView alloc] initWithFrame:myImageViewbounds];
//Add images which will be used for the animation using an array. These should exist in your project and bundle already.
myImageView.animationImages = #[[UIImage imageNamed:#"1.png"], [UIImage imageNamed:#"2.png"],[UIImage imageNamed:#"3.png"], [UIImage imageNamed:#"4.png"],[UIImage imageNamed:#"5.png"]];
//Set the duration of the entire animation
myImageView.animationDuration = 0.5;
//Set the repeat count. If you don't set that value, by default will be a loop (infinite)
myImageView.animationRepeatCount = 1;
//Start the animation
[myImageView startAnimating];
To ensure smooth morphing like transition you may check out GPUImage which has many great blur filters. Ideally you should be able to wrap a blur animation between two image animations to achieve what you want.

How can I make an animation correlate to the current position of a UIImageView?

I have an ImageView that changes between 2 different images. When an event happens and it's in ImageView1, I want it to play a specific animation. And when an event happens and it's in ImageView2, I want it to play a specific, different animation. My code set up so far is essentially
if (ImageView1 == YES)
{
ImageView.animationImages = [NSArray arrayWithObjects:
[UIImage imageNamed:#"ImageAnimate1.png"],
[UIImage imageNamed:#"ImageAnimate2.png"],
[UIImage imageNamed:#"ImageAnimate3.png"],
[UIImage imageNamed:#"ImageAnimate4.png"], nil];
[ImageView setAnimationRepeatCount:1];
ImageView.animationDuration = 1;
[ImageView startAnimating];
}
else
{
ImageView.animationImages = [NSArray arrayWithObjects:
[UIImage imageNamed:#"ImageAnimate5.png"],
[UIImage imageNamed:#"ImageAnimate6.png"],
[UIImage imageNamed:#"ImageAnimate7.png"],
[UIImage imageNamed:#"ImageAnimate8.png"], nil];
[ImageView setAnimationRepeatCount:1];
ImageView.animationDuration = 1;
[ImageView startAnimating];
}
I'm aware it doesn't need to have 2 different sets of animation repeats and duration, but that was just the beginning in the event they change later.
The main problem is, the first set of animations only plays if i'm currently setting it to ImageView1. What I mean is, If i'm tapping/ touching the screen to the left it creates ImageView1, and if i'm tapping to the right it creates ImageView2. How can I change it from having to currently be tapping to the left to play that animation, to just playing the animation when the ImageView is currently displayed as such?
It might be as simple as setting it to the current UIImage, but i'm not exactly sure how to do it.
In other words, I would want to somehow write.
if (UIImage ImageViewLeft = YES)
ImageView.animationImages etc etc etc.
else ImageView.animationImages would be the second variation.
EDIT: I just changed it to
if (ImageView.image == [UIImage imageNamed:#"ImageLeft.png"])
{ code here
What happens is though now i cant change direction until another event happens. Essentially it will be stuck in either left or right until the event happens and then it correlates to the correct image then. .
A simple way would be to create a BOOL for imageIsRight. You set it to YES when you start playing an animation on the right, you set it to NO, when it goes left. Then create a timer which fires every .05 seconds (or whatever works). That timer can call a method which checks the BOOL you created and either repeats/continues the current animation or stops it and starts the other.
The above details may not be perfect for your scenario, which is a bit fuzzy to me, but the approach should get you where you need to be.

iOS Animation for balloon blown

How to create a balloon blown animation in iOS?
I'm using this code:
UIImage *image1 = [UIImage imageNamed:#"exp0.png"];
UIImage *image2 = [UIImage imageNamed:#"exp1.png"];
_ImageView.animationImages = [NSArray arrayWithObjects:image1, image2, nil];
[_ImageView setAnimationDuration:0.75f];
[_ImageView startAnimating];
Only one image is animated using this code, so what would I have to do? Have to create a for loop to change image with respect to time interval?
With UIImageView animation, you don't use a for loop. You give the image view an array of images and a duration, and you start it animating. The system takes care of displaying each frame of your animation in sequence.
You could not use a for loop anyway because the for loop would run too quickly. If you want to do timed animation yourself you would need to use a relating timer. You could use NSTimer, but it isn't super-accurate. It would be better to use CADisplayLink. But then you're back to doing what the UIImageView animation methods are doing.
What is the code you posted doing differently than what you want it to do? Your question is not at all clear.

Resources