Wrong return of UIImageView.isAnimating? - objective-c

I have a Problem with using the isAnimating function.
I have got an UIImageView called imgBecher.
In the viewDidLoad function I play a small Animation:
imgBecher.image = [UIImage imageNamed:#"diceholderAni.11.png"];
AniArray = [NSArray arrayWithObjects:
[UIImage imageNamed:#"diceholderAni.1.png"],
[UIImage imageNamed:#"diceholderAni.2.png"],
[UIImage imageNamed:#"diceholderAni.3.png"],
[UIImage imageNamed:#"diceholderAni.4.png"],
[UIImage imageNamed:#"diceholderAni.5.png"],
[UIImage imageNamed:#"diceholderAni.6.png"],
[UIImage imageNamed:#"diceholderAni.7.png"],
[UIImage imageNamed:#"diceholderAni.8.png"],
[UIImage imageNamed:#"diceholderAni.9.png"],
[UIImage imageNamed:#"diceholderAni.10.png"],
nil];
imgBecher.animationImages = AniArray;
[imgBecher setAnimationRepeatCount:1];
imgBecher.animationDuration = 1;
[imgBecher startAnimating];
Everything fine so far.
Now I call a Loop with:
[NSTimer scheduledTimerWithTimeInterval:1.0/60 target:self selector:#selector(gameLoop) userInfo:nil repeats:YES];
-(void)gameLoop {
NSLog(#"%i",imgBecher.isAnimating);
}
Now gameLoop fires 1 all over the time, not seeing that the animation is over.
BUT
as soon as I touch the screen anywhere, it fires 0, just as it should be.
I need this later on, where I have 4 animations, which should be played right after another without any user actions.
Thanks in advance! ;)

Probably you have to use userInteractionEnabled property:
imgBecher.userInteractionEnabled = NO; or self.view.userInteractionEnabled = NO;

Related

How to use animated Navigation bar button in Objective c

I want to add this Image on my Navigation Bar button, How can I use this image on my navigation Right Bar button or Left bar button?
Please check below code those I am using to View same like work code on Navigation bar button:-
- (void)viewDidLoad
{
[super viewDidLoad];
// Animation
UIImageView*animationView = [[UIImageView alloc] initWithFrame:self.view.frame];
animationView.animationImages = [NSArray arrayWithObjects:
[UIImage imageNamed:#"1.gif"],
[UIImage imageNamed:#"2.gif"],
[UIImage imageNamed:#"3.gif"],
[UIImage imageNamed:#"4.gif"],
[UIImage imageNamed:#"5.gif"],
[UIImage imageNamed:#"6.gif"],nil];
animationView.animationDuration = 1.25;
animationView.animationRepeatCount = 0;
[animationView startAnimating];
[self.view addSubview:animationView];
}
Let me know How
to use this code on navigation bar button.
Thank You!
I have tried your code, with a little trick of mine and it work like magic.
NSArray *imageArray = [NSArray arrayWithObjects:
[UIImage imageNamed:#"tmp-0"],
[UIImage imageNamed:#"tmp-1"],
[UIImage imageNamed:#"tmp-2"],
[UIImage imageNamed:#"tmp-3"],
[UIImage imageNamed:#"tmp-4"],
[UIImage imageNamed:#"tmp-5"],
[UIImage imageNamed:#"tmp-6"],
[UIImage imageNamed:#"tmp-7"],nil];
UIButton *barButton = [UIButton buttonWithType:UIButtonTypeCustom];
[barButton setImage:[UIImage imageNamed:#"tmp-0"] forState:UIControlStateNormal]; // mine trick
[barButton.imageView setAnimationImages:imageArray];
[barButton.imageView setAnimationDuration:1.0f];
[barButton.imageView startAnimating];
[barButton sizeToFit];
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:barButton];
You need to give a image to barButton at first, so your button can make its frame

images from an array

i create an nsmutableArray and i add for it an images. also i create an imageView that i want to show the images from the array, i tried to use animationImages but i nothing is happened, how can i fix it?
here is my code:
//Numbers images
UIImage *numberZero = [UIImage imageNamed:#"numberZero.png"];
UIImage *numberOne = [UIImage imageNamed:#"numberOne.png"];
UIImage *numberTwo = [UIImage imageNamed:#"numberTwo.png"];
UIImage *numberThree = [UIImage imageNamed:#"numberThree.png"];
UIImage *numberFour = [UIImage imageNamed:#"numberFour.png"];
UIImage *numberFive = [UIImage imageNamed:#"numberFive.png"];
UIImage *numberSix = [UIImage imageNamed:#"numberSix.png"];
UIImage *numberSeven = [UIImage imageNamed:#"numberSeven.png"];
UIImage *numberEight = [UIImage imageNamed:#"numberEight.png"];
UIImage *numberNine = [UIImage imageNamed:#"numberNine.png"];
//add numbers uiimage to numbersArray
numbersArray = [NSMutableArray arrayWithObjects:numberZero, numberOne, numberTwo, numberThree, numberFour, numberFive, numberSix, numberSeven, numberEight, numberNine, nil];
imgview1 = [[UIImageView alloc] init];
imgview1.animationImages = numbersArray;
imgview1.animationDuration = 2;
imgview1.animationRepeatCount=0;
[imgview1 startAnimating];
[imgview1 stopAnimating];
thanks!
You need to actually add imgview1 to something so that it displays. If imgview1 was created in Interface Builder then there is no need to assign. Also you stop your animation immediately after starting it.
//If created in IB comment out the next line
imgview1 = [[UIImageView alloc] init];
imgview1.animationImages = numbersArray;
imgview1.animationDuration = 2;
imgview1.animationRepeatCount=0;
//If not created in IB then add this to a view e.g:
[self.view addSubview:imgview1];
[imgview1 startAnimating];
//[imgview1 stopAnimating]; this is too soon to stop animating
I assume that by "nothing happened" you mean that you see the first image, but the sequence does not move beyond that.
This is probably because you stopAnimating too soon: it does not even get a chance to start!
i solve the problem, i guess the problem were because i didn't use init with frame to my imgview.
here is the final code:
UIImageView *imgview1 = [[UIImageView alloc] initWithFrame:CGRectMake(40, 90, 240, 240)];
imgview1.animationImages = numbersArray;
imgview1.animationDuration = 2;
imgview1.animationRepeatCount=1;
[imgview1 startAnimating];
[self.view addSubview:imgview1];
hope u understand...

stop animation after one repeat

i create an array of 2 images, animate then and everything work perfectly. but now i want the animation to stop after one repeat, i mean that the animation will start with the first image, move to second one and stop (and the second image will stay on my imageview). how can i do it?
here is my code:
UIImage *mistakeOne = [UIImage imageNamed:#"Xmark.png"];
UIImage *mistakeOneB = [UIImage imageNamed:#"XmarkWhite.png"];
NSMutableArray *animation = [[NSMutableArray alloc] initWithObjects:mistakeOne, mistakeOneB , nil];
[mistakeNumberOne setAnimationImages:animation];
mistakeNumberOne.animationDuration = 5.0;
[mistakeNumberOne startAnimating]
thanks!
Assuming mistakeNumberOne is a UIImageView
[mistakeNumberOne animationRepeatCount:1];
If you want to keep an image after the animation set the desired image in the UIImageview before the animation:
mistakeNumberOne.image = myDesiredImage;
After the animation it will show the image.
Putting it all together:
UIImage *mistakeOne = [UIImage imageNamed:#"Xmark.png"];
UIImage *mistakeOneB = [UIImage imageNamed:#"XmarkWhite.png"];
NSMutableArray *animation = [[NSMutableArray alloc] initWithObjects:mistakeOne, mistakeOneB , nil];
[mistakeNumberOne setAnimationImages:animation];
mistakeNumberOne.animationDuration = 5.0;
[mistakeNumberOne animationRepeatCount:1];
mistakeNumberOne.image = mistakeOneB;
[mistakeNumberOne startAnimating];
Pfitz's answer is great for animating. However, for this is specific case where you have only two images and you only need to 'swap' between them its probably simpler to just change the image after a delay.
-(void)swapImage {
[mistakeNumberOne setImage:mistakeOneB];
}
then delay swapImage:
[self performSelector:#selector(swapImage) withObject:nil afterDelay:5.0];
hope this helps

layer of animation images in iphone

How I can animate two sets of images one on the other on xcode? I mean that one set is background or landscape and the other set is a figure in the background
Here's a basic approach you might try:
UIImageView* backdropScene = [[UIImageView alloc] initWithFrame:self.view.frame];
backdropScene.animationImages = [NSArray arrayWithObjects:
[UIImage imageNamed:#"rainy_background01.png"],
[UIImage imageNamed:#"rainy_background02.png"],
[UIImage imageNamed:#"rainy_background03.png"],
[UIImage imageNamed:#"rainy_background04.png"], nil];
backdropScene.animationDuration = .2f; // image change rate, .2 seconds
backdropScene.animationRepeatCount = 0; // repeat infinitely
[self.view addSubview:backdropScene];
[backdropScene startAnimation];
UIImageView* foregroundAnimation = [[UIImageView alloc] initWithFrame:self.view.frame];
foregroundAnimation.animationImages = [NSArray arrayWithObjects:
[UIImage imageNamed:#"man_walk01.png"],
[UIImage imageNamed:#"man_walk02.png"],
[UIImage imageNamed:#"man_walk03.png"],
[UIImage imageNamed:#"man_walk04.png"], nil];
foregroundAnimation.animationDuration = .5f; // image change rate, .2 seconds
foregroundAnimation.animationRepeatCount = 0; // repeat infinitely
[self.view addSubview:foregroundAnimation];
[foregroundAnimation startAnimation];

Pausing between image views

I want to run a series of pictures with dialog bubble (like a strip cartoon). I've tried to use the action:
-(IBAction) runDialog:(id)sender {
imageView.image = [UIImage imageNamed:#"a1.png"];
[NSThread sleepForTimeInterval:5.0];
imageView.image = [UIImage imageNamed:#"b1.png"];
[NSThread sleepForTimeInterval:5.0];
imageView.image = [UIImage imageNamed:#"b2.png"];
[NSThread sleepForTimeInterval:5.0];
imageView.image = [UIImage imageNamed:#"a2.png"];
[NSThread sleepForTimeInterval:5.0];
}
This doesn't work. All it does is show the last image (a2.png) After About 20 seconds
Any ideas how I should go about this:showing a series of pictures with a pause in between?
Use NSTimer instead.
- (IBAction)runDialog:(id)sender {
yourInstanceVariableOfNSTimer = [NSTimer scheduledTimerWithTimeInterval:5.0f
target:self selector:#selector(showNextPage) userInfo:nil repeats:NO];
}
- (void)showNextPage {
imageView.image = [yourInstanceVariableOfNSArray
objectAtIndex:++yourInstanceVariableOfNSInteger];
if (!transitionsFinished) {
yourInstanceVariableOfNSTimer = [NSTimer scheduledTimerWithTimeInterval:5.0f
target:self selector:#selector(showNextPage) userInfo:nil repeats:NO];
}
}
A simpler way to handle this would be to use a UIImageView in the dialog and set its animationImages, animationDuration, etc... properties.