UIView.animateWithDuration swift loop animation

前端 未结 2 1972
悲哀的现实
悲哀的现实 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()
    

提交回复
热议问题