我正在使用 Xcode 6 Beta 5 和 Swift 制作一款 iPhone 游戏。目前看起来像这样:
用户必须用手指在向上移动的方 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
用于动画。使用 CADisplayLink
。 From 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/