我正在尝试创建一个动画,在给定的延迟将一个圆在屏幕上移动到随机点。
然而,我的问题是,当我调用重复动画时。它将我的圆移回原点,并用完全相同的坐标重复那个精确的动画。有没有办法让我有一个重复的动画,每次都需要新的坐标?
startAnimations():
func startAnimations() {
UIView.beginAnimations(nil, context: nil)
UIView.setAnimationDelay(gameSpeed)
UIView.setAnimationRepeatCount(100)
moveCircle()
UIView.setAnimationDelegate(self)
UIView.setAnimationDidStopSelector(#selector(killFloor))
UIView.commitAnimations()
}moveCircle():
func moveCircle() {
self.view.layoutIfNeeded()
animator.removeAllBehaviors()
let randomPoint = grabRandomPoint(from: self)
print("Random Point (\(randomPoint.x), \(randomPoint.y))")
circle.center = randomPoint
}grabRandomPoint():
func grabRandomPoint(from vc: ViewController) -> CGPoint {
// x coordinate between MinX (left) and MaxX (right):
let randomX = randomInRange(Int(CGRectGetMinX(vc.view.frame)), hi: Int(CGRectGetMaxX(vc.view.frame)))
// y coordinate between MinY (top) and MaxY (bottom):
let randomY = randomInRange(Int(CGRectGetMinY(vc.view.frame)), hi: Int(CGRectGetMaxY(vc.view.frame)))
let randomPoint = CGPoint(x: randomX, y: randomY)
return randomPoint
}发布于 2016-09-02 04:09:01
你好,我一直在研究你的问题,这是我发现的,检查这段代码,这段代码永远不会停止,但你可以定义一个变量的执行次数
func startAnimations() {
UIView.beginAnimations(nil, context: nil)
UIView.setAnimationDelay(0.5)
moveCircle()
UIView.setAnimationDelegate(self)
UIView.setAnimationDidStopSelector(#selector(self.startAnimations))
UIView.commitAnimations()
}
func moveCircle() {
self.view.layoutIfNeeded()
//animator.removeAllBehaviors()
let randomPoint = grabRandomPoint(from: self)
print("Random Point (\(randomPoint.x), \(randomPoint.y))")
objectToMove.center = randomPoint
}
func grabRandomPoint(from vc: ViewController) -> CGPoint {
// x coordinate between MinX (left) and MaxX (right):
let randomX = randomIntBewtween(Int(CGRectGetMinX(vc.view.frame)), max: Int(CGRectGetMaxX(vc.view.frame)))
// y coordinate between MinY (top) and MidY (middle):
let randomY = randomIntBewtween(Int(CGRectGetMinY(vc.view.frame)), max: Int(CGRectGetMidY(vc.view.frame)))
let randomPoint = CGPoint(x: randomX, y: randomY)
return randomPoint
}这是我的代码,用来在
func randomIntBewtween(min : Int, max : Int) ->Int
{
let randomNumber = arc4random_uniform(UInt32(max) - UInt32(min)) + UInt32(min)
return Int(randomNumber)
}我希望这对你有帮助
https://stackoverflow.com/questions/39275593
复制相似问题