Blame view

Twear/Basic/LoadingButton/LoaderBallPulseRotateAnimation.swift 3.56 KB
75d24c15   yangbin   123
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
  //
  //  LoaderBallClipRotatePulseAnimation.swift
  //  LoaderButton
  //
  //  Created by 黄进文 on 2018/6/12.
  //  Copyright © 2018 Jovins. All rights reserved.
  //
  
  import UIKit
  
  class LoaderBallPulseRotateAnimation: LoaderButtonAnimationDelegate {
  
      /// Setup Loader Layer
      ///
      /// - Parameters:
      /// - layer: layer Parent layer (Button layer)
      /// - frame: frame of parant layer
      /// - color: color of Loader
      func setupLoaderButtonAnimation(layer: CALayer, frame: CGRect, color: UIColor) {
  
          let padding: CGFloat = 10.0
          let sizeValue = max(min(frame.width, frame.height) - padding, 1.0)
          let duration: CFTimeInterval = 1.0
          let timingFunction = CAMediaTimingFunction(controlPoints: 0.09, 0.57, 0.49, 0.9)
          smallCircle(with: duration, timingFunction: timingFunction, layer: layer, sizeValue: sizeValue, color: color)
          bigCircle(with: duration, timingFunction: timingFunction, layer: layer, sizeValue: sizeValue, color: color)
      }
  
      /// Small Circle
      private func smallCircle(with duration: CFTimeInterval, timingFunction: CAMediaTimingFunction, layer: CALayer, sizeValue: CGFloat, color: UIColor) {
  
          let animation = CAKeyframeAnimation(keyPath: "transform.scale")
          animation.keyTimes = [0, 0.3, 1]
          animation.timingFunctions = [timingFunction, timingFunction]
          animation.values = [1, 0.3, 1]
          animation.duration = duration
          animation.repeatCount = HUGE
          animation.isRemovedOnCompletion = false
          // Draw Circle
          let circleSize = sizeValue * 0.5
          let padding: CGFloat = 10.0
          let circle = LoaderShape.circle.layer(with: CGSize(width: circleSize, height: circleSize), color: color)
          let frame = CGRect(x: (sizeValue + padding - circleSize) * 0.5,
                             y: (sizeValue + padding - circleSize) * 0.5,
                             width: circleSize, height: circleSize)
          circle.frame = frame
          circle.add(animation, forKey: "animation")
          layer.addSublayer(circle)
      }
  
      /// Big Circle
      private func bigCircle(with duration: CFTimeInterval, timingFunction: CAMediaTimingFunction, layer: CALayer, sizeValue: CGFloat, color: UIColor) {
  
          /// Scale Animation
          let scaleAnimation = CAKeyframeAnimation(keyPath: "transform.scale")
          scaleAnimation.keyTimes = [0, 0.5, 1]
          scaleAnimation.timingFunctions = [timingFunction, timingFunction]
          scaleAnimation.values = [1, 0.6, 1]
          scaleAnimation.duration = duration
  
          /// Rotate Animation
          let rotateAnimation = CAKeyframeAnimation(keyPath: "transform.rotation.z")
          rotateAnimation.keyTimes = scaleAnimation.keyTimes
          rotateAnimation.timingFunctions = [timingFunction, timingFunction]
          rotateAnimation.values = [0, Double.pi, 2 * Double.pi]
          rotateAnimation.duration = duration
  
          /// Group Animation
          let animation = CAAnimationGroup()
          animation.animations = [scaleAnimation, rotateAnimation]
          animation.duration = duration
          animation.repeatCount = HUGE
          animation.isRemovedOnCompletion = false
  
          /// Draw Circle
          let padding: CGFloat = 10.0
          let circle = LoaderShape.ringVertical.layer(with: CGSize(width: sizeValue, height: sizeValue), color: color)
          let frame = CGRect(x: padding * 0.5,
                             y: padding * 0.5,
                             width: sizeValue,
                             height: sizeValue)
          circle.frame = frame
          circle.add(animation, forKey: "animation")
          layer.addSublayer(circle)
      }
  }