使用随机坐标重复 UIAnimation
Posted
技术标签:
【中文标题】使用随机坐标重复 UIAnimation【英文标题】:Repeat UIAnimation with Random Coordinates 【发布时间】:2016-09-01 15:28:57 【问题描述】:我正在尝试创建一个动画,以给定的延迟将屏幕上的圆圈移动到随机点。
然而,我的问题是当我打电话重复动画时。它将我的圆圈移回原点,并以完全相同的坐标重复该精确动画。有没有办法让我制作一个每次都需要新坐标的重复动画?
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
【问题讨论】:
块。基于。动画片。自从。 ios。 4. 【参考方案1】:你好我一直在处理你的问题,这是我发现的,检查这段代码,这段代码永远不会停止,但你可以定义一个执行次数的变量
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)
希望对你有帮助
【讨论】:
删除所有行为是我的问题吗?以上是关于使用随机坐标重复 UIAnimation的主要内容,如果未能解决你的问题,请参考以下文章