Insert a frame to an animation(using UIImageView) - ios

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

Related

How to add animate images and text in the same view programatically?

I am currently learning iOS development and I want to develop an iOS app where there is an animated image at the beginning top of the View Controller and a lot of text just after it. Please have a look at the picture below:
I have thought about the codes for both the image animation at the top and the large amount of text just after it. I was wondering whether someone could please review them and tell me whether they are fine or not.
The codes for both the image animation and the large amount of text will be placed inside the viewDidLoad method of the implementation file of the controller like this:
- (void)viewDidLoad
{
[super viewDidLoad];
// codes for image animation
// codes for large amount of text to be displayed
}
IMAGE ANIMATION CODES
For the image animation, I am going to use 5 png pictures: A.png, B.png, C.png, D.png and E.png. The slide show will start from A, then move on to B, then to C, to D and finally to E. All the 5 pictures are added to my project in Xcode. Below are the codes:
NSArray *imagelist = [[NSArray alloc] initWithObjects: [UIImage imageNamed:#"A.png"], [UIImage imageNamed:#"B.png"], [UIImage imageNamed:#"C.png"], [UIImage imageNamed:#"D.png"], [UIImage imageNamed:#"E.png"],nil];
UIImageView *animationImageView = [[UIImageView alloc] initWithFrame: CGRectMake(5,5,90,180)];
animationImageView.animationImages = imagelist;
animationimageView.animationDuration = 10;
[self.view addSubView:animationimageView];
[animationImageView startAnimating];
LARGE AMOUNT OF TEXT CODES
For the text, I have not added all the necessary text....because there is a lot more. That is why I need the text object to be scrollable. Below are the codes:
UITextView *largeText = [[UITextView alloc] initWitthFrame: CGRectMake(5,100,90,180)];
largeText.text = #"In organizations, we must work with and for others. To be able to mutually achieve our goals, we must be able to relate to others effectively. These leadership tips will help you do just that:
1)Catch people doing things right and then let them know that they are doing things right
2)Use feedback to stay informed about what other people are doing in your area of responsibility and authority
3)Have regular, focused meetings regarding the projects that you are responsible for
4)Provide adequate instructions. Time is lost if things are not done correctly
5)Train others to do jobs. You cannot do them all, nor can others do them if they have not been trained
6)Expect others to succeed. It becomes a self-fulfilling prophecy when you believe others are loyal, dedicated and doing a good job";
largeText.font = [UIFont fontWithName:#"TimesNewRomanPSMT" size:18];
largeText.editable = NO;
largeText.scrollEnabled = YES;
[self.view addSubView:largeText];
Please review the codes and can someone tell me whether the codes are fine or not.
Thanks for reading.
NSArray *imagelist = [[NSArray alloc] initWithObjects: [UIImage imageNamed:#"A.png"], [UIImage imageNamed:#"B.png"], [UIImage imageNamed:#"C.png"], [UIImage imageNamed:#"D.png"], [UIImage imageNamed:#"E.png"],nil];
UIImageView *animationImageView = [[UIImageView alloc] initWithFrame: CGRectMake(5,5,90,180)];
[self.view addSubView:animationimageView];
NSTimeInterval delay=0.0;
for (NSInteger i=0; i<[imagelist count]; i++) {
delay+=3.0;
[UIView animateWithDuration:0.5 delay:delay options:UIViewAnimationOptionCurveLinear animations:^{
animationImageView.image=[imagelist objectAtIndex:i];
}completion:nil];
}

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?

UIImageView animationRepeatCount strange behaviour

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

Changing the properties of a UIImageView

I have picked up programming as a hobby, so please bear with any 'old school' or completely wrong practices!
I am trying to move three images across the view using a timer. They then need to be dodged by another UIImageView. The obstacles are in an array:
objArray = [NSMutableArray arrayWithObjects:[UIImage imageNamed:#"img-1.png"],
[UIImage imageNamed:#"img-2.png"],
[UIImage imageNamed:#"img-3.png"], nil];
I then have a for loop to create these views, and it's here that I think the problems start.
count = [objArray count];
for(int i=0; i < count; i++)
{
NSLog (#"Element %i = %#", i, [objArray objectAtIndex: i]);
randX = arc4random_uniform(450);
randX = randx + 50;
randY = arc4random_uniform(236);
randY = randy + 45;
imgObj = [[UIImageView alloc] initWithImage:[UIImage imageNamed:[objArray objectAtIndex:i]]];
imgObj.center = CGPointMake(randX, randY);
[self.view addSubview:imgObj];
[self startAnimation];
All three images display ok. The problem is getting these to move.
I understand the need for the timer, which is initiated in the startAnimation method. I have tried setting a tag in the for loop. However this always results in the last object "img-3.png", which then moves ok.
My question is: how do I differentiate the three views I create in the loop so I can call them elsewhere?
Assigning tags to you UIImageViews should work:
imgObj.tag = i+1; //skipping 0 here since other subviews may have this tag
Then you should be able to access the views via
[self.view viewWithTag:tag];
Or you could create another NSMutableArray and store the UIImageViews in it.

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.

Resources