ios - 使用 NSTimer 连续向上移动 UIViews 看起来不稳定/不流畅

标签 ios uiview swift nstimer

我正在使用 Xcode 6 Beta 5 和 Swift 制作一款 iPhone 游戏。目前看起来像这样:enter image description here

用户必须用手指在向上移动的方 block 之间移动。为此,我为每个 block 创建了 UIView,并使用算法生成随机路径。我首先使用 animateWithDuration 向上移动 block ,但是当我尝试添加对用户何时触摸 block 的检测时出现了复杂情况。

我现在正在尝试使用 NSTimer 制作动画:

var shiftBlockTimer = NSTimer.scheduledTimerWithTimeInterval(0.01, target: self, selector: Selector("shiftBlocks"), userInfo: nil, repeats: true)

func shiftBlocks() {
    for leftBlock in leftBlocks {
         leftBlock.frame.origin.y -= 70
    }

    for rightBlock in rightBlocks {
         rightBlock.frame.origin.y -= 70
    }
}

这确实有效,但是运动都是跳跃的,而不是我希望的平滑运动,并且在我使用 animateWithDuration 时也有。

有什么方法可以让这个运动变得更平滑,而不增加迭代次数? (这会减慢我的应用程序的速度)

最佳答案

不要将 NSTimer 用于动画。使用 CADisplayLinkFrom the documentation :

A CADisplayLink object is a timer object that allows your application to synchronize its drawing to the refresh rate of the display.

但实际上,也不要使用 CADisplayLink。使用 Sprite 套件。它专为此类 2D 游戏而设计。

关于ios - 使用 NSTimer 连续向上移动 UIViews 看起来不稳定/不流畅,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25188295/

相关文章:

ios - 单个生命周期长的ios后台任务(voip)

iphone - 在 UILongPressGestureRecognizer 持续时间内执行操作

ios - UITableViewCell 内的 UIView 上的 UIGestureRecognizer

ios - 如何将 UIView reshape 为菱形

iOS检测触摸来关闭键盘?

ios - 自定义 UITableViewCell 内的圆形 UIView 底角

iOS 8 : Launch Screen StoryBoard appears black [single XIB file works fine]

ios - 如何在 Swift 中检查 NSTimer 是否正在运行?

arrays - 从 Swift 中的数组中获取 3 个独特的项目 - 每个项目都有独特的值(value)

ios - 将 NSFetchedResultsController 与 UITableView 同步