UIImageView animationRepeatCount strange behaviour - ios

So i have this code that animates a set of images an unlimited amount of times just fine, however as soon as i try to limit the number of times it animates it simply does not work. No image is even displayed.
Code that works:
animatedMap.animationImages = [NSArray arrayWithObjects:
[UIImage imageNamed:#"0.gif"],
[UIImage imageNamed:#"1.gif"],
[UIImage imageNamed:#"2.gif"],
[UIImage imageNamed:#"3.gif"],
//etc...];
animatedMap.animationDuration = 20.0f;
animatedMap.animationRepeatCount = 0;
[animatedMap startAnimating];
Code that doesn't work:
animatedMap.animationImages = [NSArray arrayWithObjects:
[UIImage imageNamed:#"0.gif"],
[UIImage imageNamed:#"1.gif"],
[UIImage imageNamed:#"2.gif"],
[UIImage imageNamed:#"3.gif"],
//etc...];
animatedMap.animationDuration = 20.0f;
animatedMap.animationRepeatCount = 1;
[animatedMap startAnimating];
This just seems like really strange behaviour?

I noticed the same behavior, my solution was to put the start of animation code in a different place.
For me, it was a View Controller, so I put imageView.startAnimating() in viewWillAppear: instead of viewDidLoad:.
I don't 100% understand what is the problem, but one can assume that the animation doesn't fire because by setting the animationRepeatCount to 1, your "one animation" happens before the view is displayed. To be more technical, the CATransaction::Commit with the one animation gets performed before the view is displayed, and not when the view is displayed.

animationRepeatCount is used for cycle all the images in given animationDuration duration.
So if you provide animatedMap.animationRepeatCount = 1; that means only 1 cycle will be run and then the default set image will be displayed. If you didn't define default image to UIImageView then it will be blank(i.e. background color of UIImageView )
There is possibility to have more images then the given time duration for animation, which leads to super-fast animation.
Update 1
You can set last image just before starting your animation.
NSArray *imgArray = [NSArray arrayWithObjects:
[UIImage imageNamed:#"0.gif"],
[UIImage imageNamed:#"1.gif"],
[UIImage imageNamed:#"2.gif"],
[UIImage imageNamed:#"3.gif"],
//etc...];
[animatedMap setImage:[UIImage imageNamed:[imgArray lastObject]]];
animatedMap.animationImages = imgArray
animatedMap.animationDuration = 20.0f;
animatedMap.animationRepeatCount = 0;
[animatedMap startAnimating];

Related

Change position of UIImageView when Animating in NSArray

So I've seen a whole bunch of resources an answers to similar questions regarding how to change the position of a UIImageView, which can be done like this:
myUIImageView.center = CGPointMake(myImage_X, myImage_Y);
But I can not get this to work when I have my UIImageView animating.
I start my animation like this:
myUIImageView.animationImages = [NSArray arrayWithObjects:
[UIImage imageNamed:#"img1.png"],
[UIImage imageNamed:#"img2.png"],
[UIImage imageNamed:#"img3.png"],nil];
[myUIImageView setAnimationRepeatCount:0];
myUIImageView.animationDuration = 1.0;
[myUIImageView startAnimating];
Then try to move it with the line of code above... which didn't work.
Then I tried this after setting it as a property and synthesising it:
[myUIImageView setFrame:CGRectMake( 10,
10,
myUIImageView.frame.size.width,
myUIImageView.frame.size.height
)];
But it doesn't move the image :(
Currently my image is animating on the spot. But I want it to initially change position (set the coords to top of the screen), then have it fall in the Y direction. Am I doing something silly?

Why does the view disappear after an animation?

I've been trying to animate an image using this code.
- (void)viewDidLoad
{
dice.animationImages = [NSArray arrayWithObjects:
[UIImage imageNamed:#"image1.png"],
[UIImage imageNamed:#"image2.png"],
[UIImage imageNamed:#"image3.png"], nil];
[dice setAnimationRepeatCount:3];
dice.animationDuration = 1;
[super viewDidLoad];
// Do any additional setup after loading the view.
}
Then I have a button that makes the animation play.
-(IBAction) animate:(id)sender {
[dice startAnimating];
}
Everything works perfectly. The animation plays 3 times with duration of 1 second each time. However after the animation has played 3 times, the dice image view simply disappears from the screen. How do I stop this from happening so the image doesn't disappear and I can do other stuff to the image view?
Are you setting dice.image anywhere? The animations are different than the image the imageView contains, and "play in front of" the image and then are removed. I recommend using UIImageView's startAnimatingWithCompletionBlock method:
- (void) viewDidload {
dice.image = [UIImage imageNamed:#"image3.png"]; // big assumption on my part
// the assumption is that this is the image you want
// after animating
dice.animationImages = [NSArray arrayWithObjects:
[UIImage imageNamed:#"image1.png"],
[UIImage imageNamed:#"image2.png"],
[UIImage imageNamed:#"image3.png"], nil];
[dice setAnimationRepeatCount:3];
dice.animationDuration = 1
[super viewDidLoad];
}
A "block" is a chunk of one or more lines of code that wait until after the animation finishes to be executed. If you set the image beforehand it will appear "behind" the animations as they run and may not be 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 app - Apply background image object to UIView

Can anyone help me understand how I apply a background image object to a UIView please?
I have created a background image which is a blurred version of a background and I would like to apply it to be the background of a uiView in the foreground which would ideally mask the background image.
I have the following code so far -
_blurImage = [source stackBlur:50];
[_HPBlurView.backgroundColor = [UIColor colorWithPatternImage:[_blurImage]]];
I would like to apply the image object(_blurImage) to be the background image of _hpBlurView but i'm struggling to get it working!
At first glance, you are using too many brackets. Here is a working version of your code :
_burImage = [source stackBlur:50];
_HPBlurImage.backgroundColor = [UIColor colorWithPatternImage:_blurImage];
I can't see what stackBlur:50 returns. So start from the beginning. colorWithPatternImag takes UIImage as a parameter. So Start by adding a picture, any picture, to your application. Lets imagine that the image is called image.png. This is one way to do it:
UIImage *image = [UIImage imageNamed:#"image.png"];
_HPBlurView.backgroundColor = [UIColor colorWithPatternImage:image];
This should help to get you going.
Create an image and add to the background.:
UIImage *image = [UIImage imageNamed:#"youimage"];
self.view.backgroundColor = [UIColor colorWithPatternImage:image];
It's that.
To make sure everything resizes properly, no matter rotation, device size and iOS version, I just set an UIImageView
//Create UIImageView
UIImageView *backgroundImageView = [[UIImageView alloc] initWithFrame:self.view.frame]; //or in your case you should use your _blurView
backgroundImageView.image = [UIImage imageNamed:#"image.png"];
//set it as a subview
[self.view addSubview:backgoundImageView]; //in your case, again, use _blurView
//just in case
[self.view sendSubviewToBack:backgroundImageView];

Insert a frame to an animation(using UIImageView)

I have made my animation like this (it's a group of images)
mainViewGIF.animationImages = [NSArray arrayWithObjects:
[UIImage imageNamed:#"test1.jpg"],
[UIImage imageNamed:#"test2.jpg"],
[UIImage imageNamed:#"test3.jpg"],
[UIImage imageNamed:#"test4.jpg"],nil];
mainViewGIF.animationDuration = 2.0;
mainViewGIF.animationRepeatCount = HUGE_VAL;
[mainViewGIF startAnimating];
[mainViewGIF release];
What I want is to insert a frame (by touching a button)to the animation and this frame play only once, just like "talking Tom"(the cat) when you touch him, he make some kind of action.
Or there is some other kinds of solution? thank u all
Have you tried using a NSMutableArray and when you press that specific button just:
[array addObject:[UIImage imageYouWant]];
Then after you finsish the animation try something like:
[array removeObjectAtIndex:5];
Not exactly sure about the syntax of the code, but I hope this pseudo code can help you out!
Best of luck

Resources