views:

56

answers:

2

hi all,

I have a UIView with several UILabels that is animating from top to bottom and vice versa. A sort of Autoque let's say :) I use 2 functions:

-(void)goUp 
-(void)goDown 

Those functions start a UIView animation to the required position.They both have an AnimationDidStopSelector defined that calls the other function at the end. This all works smoothly.

When touching the screen, using touchesBegan, I would like to pause the current animation and change the vertical position of the UIView using touchesMoved event. In touchesEnded I want to resume the animation to the required end position.

What would be the proper way to do so?

Thomas

A: 

I'm not sure if it is possible with UIView's directly but you definitely can do that animating view's CALayers instead. See this question about pausing and resuming of CAAnimations.

Vladimir
A: 

Vladimir, the question about CAAnimations make sense.. but I found a way to 'pause' so I could keep using UIView animations:

CALayer *pLayer = [self.containerView.layer presentationLayer];
CGRect frameStop = pLayer.frame;
pausedX = frameStop.origin.x;
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationBeginsFromCurrentState:YES];
[UIView setAnimationDuration:0.01];
[UIView setAnimationCurve: UIViewAnimationCurveLinear];     
// set view properties

frameStop.origin.x = pausedX;
self.containerView.frame = frameStop;
[UIView commitAnimations];

What I'm doing here is using the presentationLayer to find out current x value of the animated view. After that I execute a new animation which overwrites the original animation. Make sure to setAnimationBeginsFromCurrentstate:YES for this. This cancels the original animation and puts the animated view not to it's destination position (which it does automatically ) but at the current position of the animation proces..

Hope this helps others too! :)

Thomas Joos