Swift Animation: UI dynamic animator


Swift Animation: UI dynamic animator

yesUIViewAnimation of attributes

UIViewPropertyAnimatorIt can realize the function of uiview elementframe, center, alpha, transformThe transformation of these attributes

When using, call directlyUIViewPropertyAnimatorInrunningPropertyAnimatormethod

    withDuration: 2,
    delay: 0,
    options: [.curveEaseInOut, .allowUserInteraction],
    animations: {
        let animatingView = self.view.viewWithTag(100)!
        animatingView.center = CGPoint.zero
}) { (animationPosition) in

be careful:
animationThe code in is executed immediately, only the animation is shown to the user later, but the result has been executed


  • repeatRepeat
  • autoreverseAfter the animation is executed, reverse the animation again

Animation of the entire uiview

This method isUIViewHow to do it

func transition(with view: UIView, duration: TimeInterval, options: UIView.AnimationOptions = [], animations: (() -> Void)?, completion: ((Bool) -> Void)? = nil)
UIView.transition(with: animatingView,
                  duration: 1,
                  options: .transitionFlipFromLeft,
                  animations: {
                    animatingView.backgroundColor = UIColor.magenta
                    animatingView.center = CGPoint(x: animatingView.frame.midX - 20, y: animatingView.frame.midY - 50)
                  completion: nil)


UIDynamicAnimatorCan achieve the interaction between objects, collision animation, gravity animation and so on

The process is as follows:

  1. newly buildUIDynamicAnimatorobject
  2. newly buildUIDynamicBehaviorObject and add it to theUIDynamicAnimatorObject

    UIDynamicBehaviorIt is the parent class of animation actions, and its subclasses include:UIAttachmentBehavior, UICollisionBehavior, UIGravityBehavior, UIDynamicItemBehavior, UIPushBehavior, UISnapBehavior

  3. toBehaviorAdd implementationUIDynamicItemThe object of the protocol,UIViewThis protocol has been implemented, so it can be used directly

Diagram of all classes

About allUIDynamicAnimatorThe related class relationships are as follows:

Swift Animation: UI dynamic animator


import UIKit

class AnimationDymanicVC: UIViewController {
    // 1. create Animator
    lazy var animator = UIDynamicAnimator(referenceView: view)

    override func viewDidLoad() {
        let ballView = BallView(frame: CGRect(origin: view.center, size: CGSize(width: 60, height: 60)))
        ballView.isOpaque = false
        // 2. create behavior
        let gravityBehavior = UIGravityBehavior()
        // 3. add behavior to animator
        // 4.add item to behavior
        //Uidynamicitem is a protocol, which has been implemented by uiview
        //When item is added to animator, this element belongs to animator. If you want to update this element, you need to call this method
        // animator.updateItem(usingCurrentState: UIDynamicItem)
        let collisinoBehavior = UICollisionBehavior()
        collisinoBehavior.translatesReferenceBoundsIntoBoundary = true

class BallView: UIView {
    override func draw(_ rect: CGRect) {
        let path = UIBezierPath(ovalIn: rect)
        path.lineWidth = 1
    override func layoutSubviews() {

It’s only when gravity is applied that the elements fly straight out of the screen. add toCollisionBehaviorYou can limit it to the screen

Swift Animation: UI dynamic animator
Swift Animation: UI dynamic animator