3

I am developing an application which needs to decrease the width of imageview to 0 from 320 in a 1hour time. for this i am using following code

     [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:3600];
    newRect.size.width = 0;
    imgView.frame = newRect;
    [UIView commitAnimations];

it is works fine if we stay in same view controller,after animation starts if the user navigate to some other viewcontoller, How to handle the animation.

Along with this i have one more issue- during the animation the image in imageview is looks like shrink as throughout the animation i am using same image ,So i want to change the image in imageView during the animation How can i achieve this.

Is there any other way to do this apart form the animation.

Any Help is Appreciated.

Madhu
  • 994
  • 1
  • 12
  • 33

1 Answers1

5

The life cycle of the controllers and views will not allow you to do this so simply. The objects you are using can and will be deallocated by the system if they are not currently needed anymore, so the animation you started is essentially discarded with it.

You will have to store the progress of your animation somewhere, e. g. in a file or a CoreData database to have it persistent across the instantiations of that view. Depending on the exact situation, it might be sufficient to store the start time of the animation once it begins. Then, in viewWillAppear you could load it and calculate how much progress into that one hour has been made and start a new animation from that point. To the user it would appear as if the animation had proceeded in the background.

Daniel Schneller
  • 13,728
  • 5
  • 43
  • 72
  • thanks for your answer,i have one more issue now i am using a same image in imageView through out the animation, So the image is looking like it is shrinking during the animation,I want to replace the image in imageview with other image during the animation.How can i achieve this. – Madhu Jan 04 '13 at 09:50
  • Not being exactly sure what you mean, but you might want to look into keyframe based animation. With a simple animation like you currently have, you can only set start and endpoint. The rest in between is interpolated. With a keyframe based animation you are in tighter control of what happens at specific points during the process. Please remember to accept the answer with the green checkmark below the vote countwhen you feel it helped you solve the problem. – Daniel Schneller Jan 04 '13 at 10:21
  • Can you please suggest any links or samples for Keyframe based animations,So that i can learn. – Madhu Jan 04 '13 at 11:10
  • See question's answer, for example: http://stackoverflow.com/questions/1031177/basic-keyframe-animation-rotation – Daniel Schneller Jan 04 '13 at 15:28