开发者

UIView cancel animation

开发者 https://www.devze.com 2023-01-15 14:04 出处:网络
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 i

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 fe开发者_如何学Cw 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];      

}


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.


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

精彩评论

暂无评论...
验证码 换一张
取 消