UIView.animateWithDuration swift loop animation UIView.animateWithDuration swift loop animation ios ios

UIView.animateWithDuration swift loop animation


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()


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})