This class provides easy implementation of animations with UIView components
Similarly to SpriteKit's SKAction of SKNode, UIAnimation represents an action that can be run on a UIView. With it you can move, rotate, and create sequences of actions and blocks. It is possible to store an animation and reuse them with multiple views.
class func moveTo(point : CGPoint, duration : NSTimeInterval) -> UIAnimationclass func moveBy(point : CGPoint, duration : NSTimeInterval) -> UIAnimationclass func scaleTo(scale : CGPoint, duration : NSTimeInterval) -> UIAnimationclass func scaleBy(scale : CGFloat, duration : NSTimeInterval) -> UIAnimationclass func rotateTo(angle : CGFloat, duration : NSTimeInterval) -> UIAnimationclass func rotateBy(angle : CGFloat, duration : NSTimeInterval) -> UIAnimationclass func sequence(animations : [UIAnimation]) -> UIAnimationclass func group(animations : [UIAnimation]) -> UIAnimationclass func repeatAnimation(animation : UIAnimation, count : Int) -> UIAnimationclass func repeatAnimationForever(animation : UIAnimation) -> UIAnimationclass func runBlock(block : ()->Void) -> UIAnimationclass func waitForDuration(duration : NSTimeInterval) -> UIAnimationclass func fadeAlphaTo(alpha : CGFloat, duration : NSTimeInterval) -> UIAnimationclass func fadeAlphaBy(alpha : CGFloat, duration : NSTimeInterval) -> UIAnimationclass func fadeInWithDuration(duration : NSTimeInterval) -> UIAnimationclass func fadeOutWithDuration(duration : NSTimeInterval) -> UIAnimationclass func followPoints(points : [CGPoint], withSpeed speed : CGFloat) -> UIAnimationclass func shake(force : CGPoint, frequence : CGFloat, duration : NSTimeInterval) -> UIAnimationclass func removeFromSuperview() -> UIAnimation
func runAnimation(animation : UIAnimation)func runAnimation(animation : UIAnimation, completion : ()->Void)func removeAllAnimations()
Consider this simple example: we want to move myView (an UIView) to the origin point, and we want the movement to last 3 seconds:
letaction=UIAnimation.moveTo(point:CGPoint.zero, duration:3)
myView.runAnimation(action)
//or just
myView.runAnimation(UIAnimation.moveTo(point:CGPoint.zero, duration:3))A more complete example would be the one present here, in the 'ViewController' file:
letv=UIView()
v.backgroundColor =UIColor.red
v.center = view.center
v.frame.size =CGSize(width: view.frame.width/3, height: view.frame.width/3)
view.addSubview(v)letpoint1=CGPoint(x: v.frame.width*1.5/2, y: view.frame.height/2)letpoint2=CGPoint(x: view.frame.width-point1.x, y: point1.y)letanim=UIAnimation.moveTo(point: point1, duration:1) // This action moves to point1
letanim2=UIAnimation.moveTo(point: point2, duration:1) // This action moves to point2
letrot1=UIAnimation.rotateBy(angle:90, duration:0.6) // Rotates 90 degrees clockwise
letrot2=UIAnimation.rotateBy(angle:-90, duration:0.6) // Rotates 90 degrees anti-clockwise
letseq=UIAnimation.sequence(animations:[anim,rot1,anim2,rot2]) // Executes, sequentially, the given actions
letrep=UIAnimation.repeatAnimationForever(animation: seq) // Makes the sequence above repeats forever
v.runAnimation(rep) // Executes the animationThis produces the animation below:
