How to add UIImages like animation using NSTimer? - ios

I want to add a array of UIImages to UIImageView using NSTimer. Such that it will be like an animation. How to do it in iOS?
Thanks in advance.

There is a very simple way to do it.
// create the view that will execute our animation
UIImageView* campFireView = [[UIImageView alloc] initWithFrame:self.view.frame];
// load all the frames of our animation
campFireView.animationImages = [NSArray arrayWithObjects:
[UIImage imageNamed:#"campFire01.gif"],
[UIImage imageNamed:#"campFire02.gif"],
[UIImage imageNamed:#"campFire03.gif"],
[UIImage imageNamed:#"campFire04.gif"],
[UIImage imageNamed:#"campFire05.gif"],
[UIImage imageNamed:#"campFire06.gif"],
[UIImage imageNamed:#"campFire07.gif"],
[UIImage imageNamed:#"campFire08.gif"],
[UIImage imageNamed:#"campFire09.gif"],
[UIImage imageNamed:#"campFire10.gif"],
[UIImage imageNamed:#"campFire11.gif"],
[UIImage imageNamed:#"campFire12.gif"],
[UIImage imageNamed:#"campFire13.gif"],
[UIImage imageNamed:#"campFire14.gif"],
[UIImage imageNamed:#"campFire15.gif"],
[UIImage imageNamed:#"campFire16.gif"],
[UIImage imageNamed:#"campFire17.gif"], nil];
// all frames will execute in 1.75 seconds
campFireView.animationDuration = 1.75;
// repeat the annimation forever
campFireView.animationRepeatCount = 0;
// start animating
[campFireView startAnimating];
// add the animation view to the main window
[self.view addSubview:campFireView];
Source: http://web.archive.org/web/20120305060003/http://appsamuck.com/day2.html

Related

XCode 5 vs XCode 6 animation changes?

I recently just upgraded to xcode 6 and suddenly a bunch of my code in my app doesn't work. What happened originally was if you touched to the left of the screen, your character would face the left, and if you touched to the right he would face the right. When a certain interaction happened, there would be a method that would check whether or not he was looking to the left or right and the animation would correspond accordingly. Now in XCode 6, he can look to the left but play the animation that correlates to the right, and when I click to the left again, he briefly looks to the left and then will only play the right animation. I have a sample of the part of the code I think that is malfunctioning, but I can't seem to troubleshoot why.
if (Stickman.image == [UIImage imageNamed:#"BjLeft6.png"])
{
Stickman.animationImages = [NSArray arrayWithObjects:
[UIImage imageNamed:#"BjLeft2.png"],
[UIImage imageNamed:#"BjLeft2.png"],
[UIImage imageNamed:#"BjLeft3.png"],
[UIImage imageNamed:#"BjLeft4.png"],
[UIImage imageNamed:#"BjLeft4.png"],
[UIImage imageNamed:#"BjLeft4.png"],
[UIImage imageNamed:#"BjLeft5.png"],
[UIImage imageNamed:#"BjLeft6.png"],
[UIImage imageNamed:#"BjLeft5.png"],
[UIImage imageNamed:#"BjLeft4.png"],
[UIImage imageNamed:#"BjLeft3.png"],
[UIImage imageNamed:#"BjLeft2.png"],
[UIImage imageNamed:#"BjLeft1.png"], nil];
[Stickman setAnimationRepeatCount:1];
Stickman.animationDuration = .5;
[Stickman startAnimating];
}
else
{
Stickman.image = [UIImage imageNamed:#"BjRight6.png"];
Stickman.animationImages = [NSArray arrayWithObjects:
[UIImage imageNamed:#"BjRight2.png"],
[UIImage imageNamed:#"BjRight2.png"],
[UIImage imageNamed:#"BjRight3.png"],
[UIImage imageNamed:#"BjRight4.png"],
[UIImage imageNamed:#"BjRight4.png"],
[UIImage imageNamed:#"BjRight4.png"],
[UIImage imageNamed:#"BjRight5.png"],
[UIImage imageNamed:#"BjRight6.png"],
[UIImage imageNamed:#"BjRight5.png"],
[UIImage imageNamed:#"BjRight4.png"],
[UIImage imageNamed:#"BjRight3.png"],
[UIImage imageNamed:#"BjRight2.png"],
[UIImage imageNamed:#"BjRight1.png"], nil];
[Stickman setAnimationRepeatCount:1];
Stickman.animationDuration = .5;
[Stickman startAnimating];
}
The comparison is bad:
(Stickman.image == [UIImage imageNamed:#"BjLeft6.png"])
This is comparing addresses and UIImage imageNamed may not return the same instance of the image as Stickman.image received and == will fail.
Rarely is comparing instance addresses the correct solution when comparing for equality.

Objective-C Animating a character

For starters I am trying to animate my main character of the game so I can later replace the images in the array with sprites -
Ive done this in my header file for my main character :
- (void)Animate;
And in my implementation file ive written :
-(void) Animate{
UIImage* img1 = [UIImage imageNamed:#"Obstacle.png"];
UIImage* img2 = [UIImage imageNamed:#"Obstacle02.png"];
UIImage* img3 = [UIImage imageNamed:#"Obstacle03.png"];
UIImage* img4 = [UIImage imageNamed:#"Obstacle04.png"];
NSArray *images = [NSArray arrayWithObjects:img1,img2,img3,img4, nil];
UIImageView* imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0.0, 0.0, 160.0, 160.0)];
//images.name = #"animation";
[imageView setAnimationImages:images];
[imageView setAnimationRepeatCount:0];
[imageView setAnimationDuration:0.5];
//imageView.center = character.center ;
[imageView startAnimating];
// character used to be myView
// the images inside the array !
}
Take note I am using the game feature of Xcode .
So what am I not doing and how can I fix it ?
UIImage* img1 = [UIImage imageNamed:#"Obstacle"];
UIImage* img2 = [UIImage imageNamed:#"Obstacle02"];
UIImage* img3 = [UIImage imageNamed:#"Obstacle03"];
UIImage* img4 = [UIImage imageNamed:#"Obstacle04"];
NSArray *animateArray = [NSArray arrayWithObjects:img1,img2,img3,img4, nil];
self.imageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 160, 160)];
self.imageView.animationImages = animateArray;
self.imageView.animationDuration = 3.0;
[self.imageView startAnimating];
self.imageView.animationRepeatCount = 0;
[self.view addSubview:self.imageView];

IOS NSArray arrayWithObjects

I try to use a ArrayWithObjects to create a scroll's movement.
And I have a problem, my animation is stopped and repeat at the number nine :/
This is my code :
background.animationImages = [NSArray arrayWithObjects: [UIImage imageNamed:#"background_wall2.png"], [UIImage imageNamed:#"background_wall3.png"], [UIImage imageNamed:#"background_wall4.png"], [UIImage imageNamed:#"background_wall5.png"], [UIImage imageNamed:#"background_wall6.png"], [UIImage imageNamed:#"background_wall7.png"], [UIImage imageNamed:#"background_wall8.png"], [UIImage imageNamed:#"background_wall9.png"], [UIImage imageNamed:#"background_wall10.png"], [UIImage imageNamed:#"background_wall11.png"], [UIImage imageNamed:#"background_wall12.png"], [UIImage imageNamed:#"background_wall13.png"], [UIImage imageNamed:#"background_wall14.png"], [UIImage imageNamed:#"background_wall15.png"], [UIImage imageNamed:#"background_wall16.png"], [UIImage imageNamed:#"background_wall17.png"], [UIImage imageNamed:#"background_wall18.png"], [UIImage imageNamed:#"background_wall19.png"], [UIImage imageNamed:#"background_wall20.png"], [UIImage imageNamed:#"background_wall21.png"], [UIImage imageNamed:#"background_wall22.png"], [UIImage imageNamed:#"background_wall23.png"], [UIImage imageNamed:#"background_wall24.png"], [UIImage imageNamed:#"background_wall.png"], nil];
[background setAnimationRepeatCount:0];
background.animationDuration = 0.3;
[background startAnimating];
Can you help me please ?
Thanks in advance
PS : Sorry for my bad English
Your animationDuration is too small. You are forcing the object to present all your frame faster than its max possibility.
From the Apple documentation:
The time duration is measured in seconds. The default value of this
property is equal to the number of images multiplied by 1/30th of a
second. Thus, if you had 30 images, the value would be 1 second.
This means that if you have 24 images, at least you have to set 0.8 as duration. (24 / 30).

0.5 Second Button Pressed, Play half of animation and sound? (edited, better conveyed))

In my app you press a button and it plays an animation frame 1,2,3,4,5,6,7,8,9,10.
However what I want is basically pressing the button for 0.2 seconds plays frames 1,2,3 but pressing it for 0.5 seconds plays frames 1,2,3,4,5,6 etc...The more frames are played the longer you hold the button. I want this with the sound that plays when you press the same button i.e.play first 0.2 seconds of the sound with 0.2 seconds of the button pressed etc..
I am really struggling to get this to work on find a good tutorial because I am just a beginner. So please help but remember to explain with as much instruction as possible. Here is my code:
#import "ViewController.h"
#interface ViewController ()
#end
#implementation ViewController {
}
- (IBAction)Button {
AudioServicesPlaySystemSound(SoundID);
}
-(IBAction)startanimating{
animatedimage.animationImages = [NSArray arrayWithObjects:
[UIImage imageNamed:#"frames_00.png"],
[UIImage imageNamed:#"frames_05.png"],
[UIImage imageNamed:#"frames_10.png"],
[UIImage imageNamed:#"frames_15.png"],
[UIImage imageNamed:#"frames_20.png"],
[UIImage imageNamed:#"frames_25.png"],
[UIImage imageNamed:#"frames_30.png"],
[UIImage imageNamed:#"frames_35.png"],
[UIImage imageNamed:#"frames_40.png"],
[UIImage imageNamed:#"frames_45.png"],
[UIImage imageNamed:#"frames_50.png"],
[UIImage imageNamed:#"frames_50.png"],
[UIImage imageNamed:#"frames_45.png"],
[UIImage imageNamed:#"frames_45.png"],
[UIImage imageNamed:#"frames_45.png"],
[UIImage imageNamed:#"frames_40.png"],
[UIImage imageNamed:#"frames_40.png"],
[UIImage imageNamed:#"frames_40.png"],
[UIImage imageNamed:#"frames_40.png"],
[UIImage imageNamed:#"frames_40.png"],
[UIImage imageNamed:#"frames_40.png"],
[UIImage imageNamed:#"frames_40.png"],
[UIImage imageNamed:#"frames_35.png"],
[UIImage imageNamed:#"frames_35.png"],
[UIImage imageNamed:#"frames_35.png"],
[UIImage imageNamed:#"frames_30.png"],
[UIImage imageNamed:#"frames_30.png"],
[UIImage imageNamed:#"frames_30.png"],
[UIImage imageNamed:#"frames_25.png"],
[UIImage imageNamed:#"frames_25.png"],
[UIImage imageNamed:#"frames_25.png"],
[UIImage imageNamed:#"frames_25.png"],
[UIImage imageNamed:#"frames_25.png"],
[UIImage imageNamed:#"frames_25.png"],
[UIImage imageNamed:#"frames_20.png"],
[UIImage imageNamed:#"frames_20.png"],
[UIImage imageNamed:#"frames_20.png"],
[UIImage imageNamed:#"frames_15.png"],
[UIImage imageNamed:#"frames_15.png"],
[UIImage imageNamed:#"frames_15.png"],
[UIImage imageNamed:#"frames_10.png"],
[UIImage imageNamed:#"frames_10.png"],
[UIImage imageNamed:#"frames_10.png"],
[UIImage imageNamed:#"frames_05.png"],
[UIImage imageNamed:#"frames_05.png"],
[UIImage imageNamed:#"frames_05.png"],
[UIImage imageNamed:#"frames_00.png"],nil];
[animatedimage setAnimationRepeatCount:1];
animatedimage.animationDuration = 0.7;
[animatedimage startAnimating];
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
NSURL *buttonURL = [NSURL fileURLWithPath: [[NSBundle mainBundle] pathForResource:#"AVTREV" ofType:#"mp3"]];
AudioServicesCreateSystemSoundID((__bridge CFURLRef)buttonURL, &SoundID);
{
}
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end
I have also heard I should use CALayers and CAanimation for this, would this be better? if so please provide help on how I can use CA and how to implement the button duration thing. Thank you I will really appreciate the help! :D
(This is not a repost, sorry for the confusion, This post is more focused on the animation and better articulated.
First declare a class property to hold your time.
Then:
UIButton * myButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
myButton.frame = CGRectMake(100.0, 100.0, 100.0, 20.0);
[myButton setTitle:#"Test" forState:UIControlStateNormal];
[myButton addTarget:self action:#selector(userEndsTap:) forControlEvents:UIControlEventTouchUpInside];
[myButton addTarget:self action:#selector(userStartsTap:) forControlEvents:UIControlEventTouchDown];
- (void)userStartsTap:(id)sender {
// start time measurement
}
- (void)userEndsTap:(id)sender {
// stop time measurement, get duration and execute code according to time passed
}

UIButton with Animation Image doesn't showing

I have found posts that concern with UIButton with Animation Image.
However i didn't found any solutions.
Here is my code.
UIImage *imageOne=[UIImage imageNamed:#"1Ads.png"];
UIImage *imageTwo=[UIImage imageNamed:#"2Ads.png"];
UIImage *imageThree=[UIImage imageNamed:#"3Ads.png"];
NSArray *arr = [[NSArray alloc] initWithObjects:imageOne,imageTwo,imageThree, nil];
self.btnAds.imageView.animationDuration = 7.0;
self.btnAds.imageView.animationImages = arr;
[self.btnAds.imageView startAnimating];
It's doesn't showing anything in View.
How can i do it?
Use the imageView property of UIButton. You can create an animation like this:
[myButton setImage:[UIImage imageNamed:#"1.png"] forState:UIControlStateNormal];
myButton.imageView.animationImages = [NSArray arrayWithObjects:
[UIImage imageNamed:#"1.png"],
[UIImage imageNamed:#"2.png"],
[UIImage imageNamed:#"3.png"],
[UIImage imageNamed:#"4.png"],
[UIImage imageNamed:#"5.png"],
nil];
myButton.imageView.animationDuration = 0.5;
[myButton.imageView startAnimating];

Resources