views:

54

answers:

2

I currently have a view animating from one part of the screen to another. I want the animation to pause whenever the "pause" button is pressed. This means the animation needs to remain right where it is (frame location) when I "pause" the animation. I've tried a few things but nothing has given me the result I want. Below is a rough version of my current code.

-(IBAction)startScroll
{
NSLog(@"Start scroll");
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationBeginsFromCurrentState:YES];
[UIView setAnimationCurve:UIViewAnimationCurveLinear];
[UIView setAnimationDuration:30];

//set new frame for view

[UIView commitAnimations];  
}

and one method to stop the animation

-(IBAction)pauseScroll
{
NSLog(@"Pause Scroll");

[UIView beginAnimations:nil context:NULL];
[UIView setAnimationBeginsFromCurrentState:YES];
[UIView setAnimationCurve:UIViewAnimationCurveLinear];
[UIView setAnimationDuration:0.1];

//Pause animation in it's current location

[UIView commitAnimations];      

}
A: 

UIView animations don't quite work like that. Once you set the frame, the view instantly moves there from your app's perspective; it's just the display to the user that's animated.

You can use the "presentation layer" to figure out roughly what's being displayed on screen (it's only approximate since the compositing happens in another thread):

#import <QuartzCore/CALayer.h>

...

[fooView setFrame:fooView.layer.presentationLayer.frame]
tc.
I tried setting the frame to the presentationLayer.frame as you mentioned before but for some reason it's setting it is moving the view to the 0,0 coord point. Anything else I can try?
Jeff
Nevermind that behavior is expected ... It's because I'm trying to animate a UIScrollView not the frame itself.
Jeff
A: 

Ended up going with a timer and updating the contentOffset every few seconds. Wish there was a better way to get a view current coords even during an animation. The presentationLayer stuff did not yield successful results.

Jeff