简体   繁体   中英

Detect edges of the screen UIView animation

I have an UIView animation to move an object on the screen and I want the animation to be stopped (and the object to keep the position when the animation is stopped) when the object goes off the screen.

I want my object to always be visible, and to stop moving when it meets an edge of the screen. The target point may be anywhere.

[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:1];
[UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
self.MyObject.center = targetPoint;
[UIView commitAnimations];

I tried to fire a scheduled timer which check if the object is off screen but it seems that the position my object does not change.

Thanks for your help.

Below is the code sample if you are looking to animate the object from left to right edge of the screen.

CGRect objectFrame = self.MyObject.frame;
objectFrame.origin.x = self.view.frame.size.width-objectFrame.size.width;
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:1];
[UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
[self.MyObject setFrame:objectFrame];
[UIView commitAnimations];

Hope it helps. Cheers.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM