简体   繁体   English

具有碰撞检测功能的UIView永久线性动画

[英]UIView perpetual linear animation with collision detection

I'm trying to make a circular UIView animate in a particular direction until it collides with the borders of the view (which is full screen, so basically the borders of the device), at which point it will reflect off it and continue on its way infinitely. 我试图使圆形的UIView朝特定方向动画,直到它与视图的边框(全屏,因此基本上是设备的边框)碰撞时,在该点上它将反射并继续在其上方式无限。 However, I'm not really sure how to pose my question, so I'm having trouble finding any information on it. 但是,我不太确定如何提出我的问题,因此我很难找到有关它的任何信息。 I already have the view, the direction it will move in, and its velocity. 我已经有了视图,其移动方向和速度。 I'm just not sure how to handle an animation like that. 我只是不确定如何处理这样的动画。

Any advice is much appreciated! 任何建议深表感谢! Thanks! 谢谢!

Use a CADisplayLink to continuously update the position of the view, synced with the refresh rate of the screen. 使用CADisplayLink与屏幕的刷新率同步更新视图的位置。 The update method that that display link triggers will take into account the current x and y velocity of the view and update the frame based on it. 显示链接触发的更新方法将考虑视图的当前x和y速度并基于该视图更新框架。 If at ever point the x- or y-coordinate goes under or over a limit (0 or screen width/height), reverse the appropriate velocity value and recalculate the position. 如果在任何时候x或y坐标都低于或超过限制(0或屏幕宽度/高度),请反转适当的速度值并重新计算位置。

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

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