UIView.animateWithDuration swift loop animation

前端 未结 2 1964
悲哀的现实
悲哀的现实 2020-12-13 12:54

In ViewController.Swift I managed to make a box animate from one point to another. I thought it would be easy to loop this so the box will animate to one point and then anim

相关标签:
2条回答
  • 2020-12-13 13:09

    No need to do the completion block approach, just use the animation options argument:

    updated for Swift 3.0

    UIView.animate(withDuration: 2.0, delay: 0, options: [.repeat, .autoreverse], animations: {
    
        coloredSquare.frame = CGRect(x: 120, y: 220, width: 100, height: 100)
    
    }, completion: nil)
    

    If for any reason you want to stop the animation later, just use:

    coloredSquare.layer.removeAllAnimations()
    
    0 讨论(0)
  • 2020-12-13 13:14
    UIView.animate(withDuration: 3.0,
                               delay: 0.0,
                               options: [.curveLinear, .repeat],
                               animations: { () -> Void in
                               coloredSquare.frame = CGRect(x: 120, y: 220, width: 100, height: 100)
    
    }, completion: { (finished: Bool) -> Void in
    
    })
    
    0 讨论(0)
提交回复
热议问题