XCode 5 vs XCode 6 animation changes? - ios

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.

Related

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

iCarouselTypeLinear with more than 6 images

for some reason when I set iCarousel.type = iCarouselTypeLinear, I can't use more than 6 images. When I try to scroll I get the following error:
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFString objectAtIndex:]: unrecognized selector sent to instance 0xa47c2b0'
What am I missing?
Any help will be really appreciated.
Controller Code:
journalCarouselItems = [NSMutableArray arrayWithObjects:
[UIImage imageNamed:#"Icon-JCO1.png"],
[UIImage imageNamed:#"Icon-JCO2.png"],
[UIImage imageNamed:#"Icon-JCO3.png"],
[UIImage imageNamed:#"Icon-JCO4.png"],
[UIImage imageNamed:#"Icon-JCO5.png"],
[UIImage imageNamed:#"Icon-JCO6.png"],
[UIImage imageNamed:#"Icon-JCO7.png"],
[UIImage imageNamed:#"Icon-JCO8.png"],
[UIImage imageNamed:#"Icon-JCO9.png"],
[UIImage imageNamed:#"Icon-JCO10.png"],
[UIImage imageNamed:#"Icon-JCO11.png"],
nil];
// Initialize and configure the carousel
carouselId = 2;
journalCarousel = [[iCarousel alloc] initWithFrame: CGRectMake ( 10, 340, 748, 240)];
journalCarousel.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
journalCarousel.type = iCarouselTypeLinear;
journalCarousel.delegate = self;
journalCarousel.dataSource = self;
[self.view addSubview:journalCarousel];
EDIT: While debugging I notice that when I have 6 images the function below is called every time and the index value goes from 0 to 5, but when I have more than 6 images the same function is not called for each image, the index values are 0,2,8,9.
- (UIView *)carousel:(iCarousel *)carousel viewForItemAtIndex:(NSUInteger)index reusingView:(ReflectionView *)view
{
UIImage *image = [journalCarouselItems objectAtIndex:index];
UIButton *button = [[[UIButton alloc] initWithFrame:CGRectMake(0, 0, image.size.width, image.size.height)] autorelease];
[button setBackgroundImage:image forState:UIControlStateNormal];
[button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
button.titleLabel.font = [button.titleLabel.font fontWithSize:50];
button.layer.cornerRadius = 8.0f;
button.tag=index;
[view addSubview:button];
return view;
}
I think the issue may be coming from the initial declaration of the journalCarouselItems array. That error message means that you're sending an objectAtIndex: messages to an object of type NSString (or possibly CFString). You are then expecing your journalCarouselItems to be an array, but it is probably being released (almost certainly autoreleased) too early and that by the time the method above is called the memory that was being pointed at by journalCarouselItems now holds something different.
Try retaining the array at the very end of its declaration to ensure it stays at the correct memory location.
journalCarouselItems = [[NSMutableArray arrayWithObjects:
[UIImage imageNamed:#"Icon-JCO1.png"],
[UIImage imageNamed:#"Icon-JCO2.png"],
[UIImage imageNamed:#"Icon-JCO3.png"],
[UIImage imageNamed:#"Icon-JCO4.png"],
[UIImage imageNamed:#"Icon-JCO5.png"],
[UIImage imageNamed:#"Icon-JCO6.png"],
[UIImage imageNamed:#"Icon-JCO7.png"],
[UIImage imageNamed:#"Icon-JCO8.png"],
[UIImage imageNamed:#"Icon-JCO9.png"],
[UIImage imageNamed:#"Icon-JCO10.png"],
[UIImage imageNamed:#"Icon-JCO11.png"],
nil] retain];

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