首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >对随机坐标重复UIAnimation

对随机坐标重复UIAnimation
EN

Stack Overflow用户
提问于 2016-09-01 23:28:58
回答 1查看 571关注 0票数 0

我正在尝试创建一个动画,在给定的延迟将一个圆在屏幕上移动到随机点。

然而,我的问题是,当我调用重复动画时。它将我的圆移回原点,并用完全相同的坐标重复那个精确的动画。有没有办法让我有一个重复的动画,每次都需要新的坐标?

startAnimations():

代码语言:javascript
复制
func startAnimations() {

    UIView.beginAnimations(nil, context: nil)
    UIView.setAnimationDelay(gameSpeed)
    UIView.setAnimationRepeatCount(100)

    moveCircle()

    UIView.setAnimationDelegate(self)
    UIView.setAnimationDidStopSelector(#selector(killFloor))

    UIView.commitAnimations()
}

moveCircle():

代码语言:javascript
复制
func moveCircle() {
    self.view.layoutIfNeeded()

    animator.removeAllBehaviors()

    let randomPoint = grabRandomPoint(from: self)
    print("Random Point (\(randomPoint.x), \(randomPoint.y))")
    circle.center = randomPoint
}

grabRandomPoint():

代码语言:javascript
复制
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
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-09-02 04:09:01

你好,我一直在研究你的问题,这是我发现的,检查这段代码,这段代码永远不会停止,但你可以定义一个变量的执行次数

代码语言:javascript
复制
    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
    }

这是我的代码,用来在

代码语言:javascript
复制
func randomIntBewtween(min : Int, max : Int) ->Int
{
    let randomNumber = arc4random_uniform(UInt32(max) - UInt32(min)) + UInt32(min)
    return Int(randomNumber)
}

我希望这对你有帮助

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/39275593

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档