菜鸟教程小白 发表于 2022-12-12 16:07:18

ios - 由于 PanGesture 导致计时器滞后?


                                            <p><p>当计时器正在运行并且我试图在屏幕上拖动一个按钮时,我的计时器会滞后。这很明显,因为计时器是移动图像。</p>

<p>有没有办法通过改变一些东西来解决这个问题,还是我必须改变我的整个项目?</p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>我建议使用 CoreAnimation (<a href="https://developer.apple.com/library/ios/documentation/cocoa/conceptual/coreanimation_guide/introduction/introduction.html" rel="noreferrer noopener nofollow">https://developer.apple.com/library/ios/documentation/cocoa/conceptual/coreanimation_guide/introduction/introduction.html</a>) 或者对于简单的动画,UIView 的 <code>animateWithDuration:animations</code> 方法 (<a href="https://developer.apple.com/library/ios/documentation/uikit/reference/uiview_class/UIView/UIView.html#//apple_ref/occ/clm/UIView/animateWithDuration:animations" rel="noreferrer noopener nofollow">https://developer.apple.com/library/ios/documentation/uikit/reference/uiview_class/UIView/UIView.html#//apple_ref/occ/clm/UIView/animateWithDuration:animations</a> :)。</p>

<p>这样做的原因是 Apple 框架试图保持动画时间正确和流畅,而不管外部事件如何。换句话说,如果外部事件导致动画丢帧,框架仍然会通过在指定的持续时间内正确插入动画来生成相对漂亮的动画。</p>

<p>如果您想坚持使用计时器,请自己进行插值:</p>

<ol>
<li>记住动画开始的时间</li>
<li>定期触发计时器</li>
<li>根据新时间和原始时间之间的差异,根据动画的总持续时间和它需要移动的距离来计算您的图像应该在哪里</li>
</ol>

<p>这将产生比简单的“每 Y 毫秒移动图像 X 个像素”方法更平滑的结果,并确保动画在指定的持续时间内运行。</p></p>
                                   
                                                <p style="font-size: 20px;">关于ios - 由于 PanGesture 导致计时器滞后?,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/19694762/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/19694762/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - 由于 PanGesture 导致计时器滞后?