简体   繁体   English

iOS-UIView动画的限制?

[英]iOS - limit on UIView animate?

So I'm making a simple trivia game and I have a timerView that shrinks as time passes. 所以我正在制作一个简单的琐事游戏,并且我的timerView随着时间的流逝而缩小。 When the user selects an answer, it needs to stop shrinking immediately - it must be very responsive. 当用户选择答案时,它需要立即停止缩小-它必须非常敏感。 I give the user 10 seconds per question. 我给用户每个问题10秒的时间。 Originally I would animate 10 times (with a duration of 1.0f), calling the next "segment" of animation in the completion block of the previous animation. 最初,我会设置10次动画(持续时间为1.0f),在上一个动画的完成块中调用动画的下一个“段”。 In the completion block I would check to see if the user has tapped an answer, and if so I don't continue the chain. 在完成模块中,我将检查用户是否点击了答案,如果是,则不要继续进行搜索。 That solution works fine except that it's not very responsive because it's on a per second basis-- user taps an answer at the start of the second segment and the bar has a noticeable continuation. 该解决方案运行良好,除了它不是很响应,因为它是每秒进行的-用户在第二段的开头点按一个答案,并且该条具有明显的延续性。

My solution to THAT problem was to instead have 1000 animation calls with a duration of 0.01f. 我对该问题的解决方案是改为进行1000次动画调用,持续时间为0.01f。 After doing that, the responsiveness was on point - the view stops animating as soon as I tap an answer -- the issue though, is that it's not actually 10 seconds, it takes more like 20. 完成此操作后,响应就到位了-当我点击一个答案时,视图就会停止动画显示-但是问题是,这实际上不是10秒,而是需要20秒钟。

So question number 1: what's the smallest time interval animateWithDuration can actually process properly? 那么问题1:animateWithDuration可以正确处理的最小时间间隔是多少?

Question number 2: is there a better way to accomplish what I'm trying to do accomplish? 问题2:是否有更好的方法来完成我要完成的工作?

ill answer question two: yes there definitely is a better way, have a look at CADisplayLink 问题二:肯定有更好的方法,看看CADisplayLink

use it to shrink your view a little bit each frame, and end the display link when you need to 使用它可将您的视图每帧缩小一点,并在需要时结束显示链接

the most responsive way is: the user taps an answer, you response in the touch callback, remove animations. 响应速度最快的方法是:用户点击答案,您在触摸回调中做出响应,删除动画。 you can remove animations by CALayer 's removeAllAnimations method 您可以通过CALayerremoveAllAnimations方法删除动画

Another way to do it is to set the view to shrinking using a single animation with linear timing, and then set the speed of the view's layer to 0 to pause the animation. 另一种方法是使用线性时序将单个动画设置为缩小视图,然后将视图图层的速度设置为0以暂停动画。 When you set the speed on the layer to 0 the animation pauses instantly. 将图层的速度设置为0时,动画会立即暂停。

This works because under the covers, UIView animation actually creates and installs CAAnimation objects on the view's layers. 之所以可行,是因为在幕后,UIView动画实际上在视图的图层上创建并安装了CAAnimation对象。 It's possible to pause and continue an in-flight UIView animation just like you can a CAAnimation. 就像CAAnimation一样,可以暂停和继续运行中的UIView动画。

I have a project called KeyframeViewAnimations (link) on github that allows you to pause, continue, or "scrub" UIView and CAAnimations back and forth with a slider. 我在github上有一个名为KeyframeViewAnimations (链接)的项目,该项目使您可以使用滑块来回暂停,继续或“擦洗” UIView和CAAnimations。 You could use that technique. 您可以使用该技术。 The tricky bit will be figuring out how far along the animation is. 棘手的一点是要弄清楚动画有多远。

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

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