UIButton with Animation Image doesn't showing - ios

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];

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];

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
}

UITableView's subview not showing

I have a UITableViewController subclass. When I try to add a subView its not showing.
The subView is a UIImageView, I made a custom loading view (its shown when user is loading data from web).
But when I add,
[self.tableView addSubview:spinner];
[spinner startAnimating];
The spinner is not showing. What am I missing or I did wrong?
EDIT
Code of spinner
UIImageView *spinner = [[UIImageView alloc]init];
spinner.animationImages = [NSArray arrayWithObjects:
[UIImage imageNamed:#"spin8.png"],
[UIImage imageNamed:#"spin7.png"],
[UIImage imageNamed:#"spin6.png"],
[UIImage imageNamed:#"spin5.png"],
[UIImage imageNamed:#"spin4.png"],
[UIImage imageNamed:#"spin3.png"],
[UIImage imageNamed:#"spin2.png"],
[UIImage imageNamed:#"spin1.png"],
nil];
Use MBProgress HUD
Its an custom control through which you can easily add various type of Loading indicator
MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
hud.mode = MBProgressHUDModeAnnularDeterminate;
hud.labelText = #"Loading";
[self doSomethingInBackgroundWithProgressCallback:^(float progress) {
hud.progress = progress;
} completionCallback:^{
[hud hide:YES];
}];
Use above code or there are lot of other options as well
Try this
CGRect spinnerFrame = //frame of spinner
UIImageView *spinner = [[UIImageView alloc] initWithFrame:spinnerFrame];
spinner.animationImages = [NSArray arrayWithObjects:
[UIImage imageNamed:#"spin8.png"],
[UIImage imageNamed:#"spin7.png"],
[UIImage imageNamed:#"spin6.png"],
[UIImage imageNamed:#"spin5.png"],
[UIImage imageNamed:#"spin4.png"],
[UIImage imageNamed:#"spin3.png"],
[UIImage imageNamed:#"spin2.png"],
[UIImage imageNamed:#"spin1.png"],
nil];
[self.tableView addSubview:spinner];
[spinner startAnimating];
Try adding it to the tableView's contentView:
[self.tableVew.contentView addSubview:spinner];

How to add UIImages like animation using NSTimer?

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

Resources