TrickSpades
TrickSpades

Reputation: 62

Repeat UIAnimation with Random Coordinates

I'm trying to create an animation that moves a circle around the screen to random points at a given delay.

My issue however is that when I call to repeat the animation. It moves my circle back to the origin and repeats that exact animation with the exact same coordinates. Is there a way for me to have a repeat animation that will call for new coordinates each time?

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
}

Upvotes: 0

Views: 637

Answers (1)

Reinier Melian
Reinier Melian

Reputation: 20804

Hello i had been working on your question, this is what I have found, check this code, this code will never stop, but you can define an variable with the number of executions

    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
    }

and this is my code to find Random value between

func randomIntBewtween(min : Int, max : Int) ->Int
{
    let randomNumber = arc4random_uniform(UInt32(max) - UInt32(min)) + UInt32(min)
    return Int(randomNumber)
}

I hope this helps you

Upvotes: 2

Related Questions