Blame view

HDFwear/Basic/LoadingButton/LoaderCircleRotateAnimation.swift 1.42 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
  //
  //  LoaderBallClipRefreshAnimation.swift
  //  LoaderButton
  //
  //  Created by 黄进文 on 2018/6/13.
  //  Copyright © 2018 Jovins. All rights reserved.
  //
  
  import UIKit
  
  class LoaderCircleRotateAnimation: 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: CGFloat = max(min(frame.width+8, frame.height) - padding, 1.0)
  
          /// Rotate Animation
          let rotateAnimation = CAKeyframeAnimation(keyPath: "transform.rotation.z")
          rotateAnimation.keyTimes = [0, 0.5, 1.0].map{ NSNumber(value: $0) }
          rotateAnimation.values = [0, Double.pi, 2 * Double.pi].map{ NSNumber(value: $0) }
  
          let animationGroup = CAAnimationGroup()
          animationGroup.duration = 0.75
          animationGroup.repeatCount = .infinity
          animationGroup.animations = [rotateAnimation]
  
          let circle = LoaderShape.ringThirdFour.layer(with: CGSize(width: sizeValue, height: sizeValue), color: color)
          circle.frame = CGRect(x: padding * 0.5+3, y: padding * 0.5,
                                width: sizeValue, height: sizeValue)
          circle.add(animationGroup, forKey: "animation")
          layer.addSublayer(circle)
      }
  }