我有一个16位轮,我想旋转到一个随机位置。我的代码使车轮旋转3次,然后停在随机位置。这似乎工作得很好,除了有时轮子将在旋转的第一部分逆时针旋转。我希望它始终顺时针旋转。我的代码如下所示。我在哪里出错了?我已将帧拆分为9,但已尝试将其拆分为3,6,9 12等。
// Generate random numbered position to spin to (1 to 16)
spinto = Int(arc4random_uniform(16) + 1)
// Make Wheel spin 3 full times, then stop at random position
spin = CGFloat(M_PI * 6) + (CGFloat((M_PI * 2) / 16) * CGFloat(spinto))
anglelabel.text = "\(self.spin)"
// Animate the wheel spin
UIView.animateKeyframesWithDuration(3.0, delay: 0.0, options: UIViewKeyframeAnimationOptions.CalculationModePaced, animations: {
UIView.addKeyframeWithRelativeStartTime(0, relativeDuration: 1/9, animations: {
self.wheel.transform = CGAffineTransformMakeRotation(1/9 * self.spin)
})
UIView.addKeyframeWithRelativeStartTime(1/9, relativeDuration: 1/9, animations: {
self.wheel.transform = CGAffineTransformMakeRotation(2/9 * self.spin)
})
UIView.addKeyframeWithRelativeStartTime(2/9, relativeDuration: 1/9, animations: {
self.wheel.transform = CGAffineTransformMakeRotation(3/9 * self.spin)
})
UIView.addKeyframeWithRelativeStartTime(3/9, relativeDuration: 1/9, animations: {
self.wheel.transform = CGAffineTransformMakeRotation(4/9 * self.spin)
})
UIView.addKeyframeWithRelativeStartTime(4/9, relativeDuration: 1/9, animations: {
self.wheel.transform = CGAffineTransformMakeRotation(5/9 * self.spin)
})
UIView.addKeyframeWithRelativeStartTime(5/9, relativeDuration: 1/9, animations: {
self.wheel.transform = CGAffineTransformMakeRotation(6/9 * self.spin)
})
UIView.addKeyframeWithRelativeStartTime(6/9, relativeDuration: 1/9, animations: {
self.wheel.transform = CGAffineTransformMakeRotation(7/9 * self.spin)
})
UIView.addKeyframeWithRelativeStartTime(7/9, relativeDuration: 1/9, animations: {
self.wheel.transform = CGAffineTransformMakeRotation(8/9 * self.spin)
})
UIView.addKeyframeWithRelativeStartTime(8/9, relativeDuration: 1/9, animations: {
self.wheel.transform = CGAffineTransformMakeRotation(9/9 * self.spin)
})
}, completion: {finished in
// do next bit of code
})