Blame view

HDFwear/Basic/LoadingButton/LoaderBallBeatAnimation.swift 2.26 KB
f2cf74c7   yangbin   1.0.20(4)
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
  //
  //  LoaderBallBeatAnimation.swift
  //  LoaderButton
  //
  //  Created by 黄进文 on 2018/6/13.
  //  Copyright © 2018 Jovins. All rights reserved.
  //
  
  import UIKit
  
  class LoaderBallBeatAnimation: 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 space: CGFloat = 2
          let padding: CGFloat = 10.0
          let sizeValue: CGFloat = max(min(frame.width, frame.height) - padding, 1.0)
          let circleValue = (sizeValue - 2 * space) / 3
          let x: CGFloat = padding * 0.5
          let y: CGFloat = (sizeValue + padding - circleValue) * 0.5
          let duration: CFTimeInterval = 0.75
          let beginTime = CACurrentMediaTime()
          let beginTimes = [0.35, 0,  0.35]
  
          /// Scale Animation
          let scaleAnimaton = CAKeyframeAnimation(keyPath: "transform.scale")
          scaleAnimaton.keyTimes = [0, 0.5, 1]
          scaleAnimaton.values = [1, 0.75, 1]
          scaleAnimaton.duration = duration
  
          /// Opacity Animation
          let opacityAnimation = CAKeyframeAnimation(keyPath: "opacity")
          opacityAnimation.keyTimes = [0, 0.5, 1]
          opacityAnimation.values = [1, 0.25, 1]
          opacityAnimation.duration = duration
  
          /// Group Animation
          let animation = CAAnimationGroup()
          animation.animations = [scaleAnimaton, opacityAnimation]
          animation.timingFunction = CAMediaTimingFunction(name: .linear)
          animation.duration = duration
          animation.repeatCount = HUGE
          animation.isRemovedOnCompletion = false
  
          /// Draw Circles
          for i in 0..<3 {
              let circle = LoaderShape.circle.layer(with: CGSize(width: circleValue, height: circleValue), color: color)
              let frame = CGRect(x: x + circleValue * CGFloat(i) + space * CGFloat(i),
                                 y: y,
                                 width: circleValue, height: circleValue)
              animation.beginTime = beginTime + beginTimes[i]
              circle.frame = frame
              circle.add(animation, forKey: "animation")
              layer.addSublayer(circle)
          }
  
      }
  }