简体   繁体   English

检测屏幕UIView动画的边缘

[英]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. 我有一个UIView 动画可以在屏幕上移动对象,并且当对象离开屏幕时我希望动画停止 (并且动画停止时该对象保持位置)。

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. 干杯。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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