Skip to content

Commit eb9f41a

Browse files
committed
Create Spark Animator
1 parent 2f4104e commit eb9f41a

File tree

7 files changed

+200
-40
lines changed

7 files changed

+200
-40
lines changed

InfinitySample/samples/arrow/ArrowRefreshAnimator.swift

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,10 @@ extension UIColor {
1818
}
1919
}
2020

21-
public class ArrowRefreshAnimator: UIView, CustomPullToRefreshAnimator {
21+
class ArrowRefreshAnimator: UIView, CustomPullToRefreshAnimator {
2222

23-
public private(set) var animating = false
24-
public var color: UIColor? {
23+
private(set) var animating = false
24+
var color: UIColor? {
2525
didSet {
2626
if let color = color {
2727
arrowLayer.strokeColor = color.CGColor
@@ -37,7 +37,7 @@ public class ArrowRefreshAnimator: UIView, CustomPullToRefreshAnimator {
3737

3838
private var activityIndicatorView: UIActivityIndicatorView = UIActivityIndicatorView(activityIndicatorStyle: .Gray)
3939

40-
public override init(frame: CGRect) {
40+
override init(frame: CGRect) {
4141
super.init(frame: frame)
4242

4343
circleBackLayer.path = UIBezierPath(ovalInRect: CGRect(x: 0, y: 0, width: frame.width, height: frame.height)).CGPath
@@ -83,10 +83,10 @@ public class ArrowRefreshAnimator: UIView, CustomPullToRefreshAnimator {
8383
self.addSubview(activityIndicatorView)
8484

8585
}
86-
public required init?(coder aDecoder: NSCoder) {
86+
required init?(coder aDecoder: NSCoder) {
8787
fatalError("init(coder:) has not been implemented")
8888
}
89-
public func animateState(state: PullToRefreshState) {
89+
func animateState(state: PullToRefreshState) {
9090
switch state {
9191
case .None:
9292
stopAnimating()

InfinitySample/samples/circle/CircleInfinityAnimator.swift

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,12 @@
99
import UIKit
1010
import Infinity
1111

12-
public class CircleInfinityAnimator: UIView, CustomInfinityScrollAnimator {
12+
class CircleInfinityAnimator: UIView, CustomInfinityScrollAnimator {
1313

1414
var circle: CAShapeLayer = CAShapeLayer()
1515
private(set) var animating = false
1616

17-
public override init(frame: CGRect) {
17+
override init(frame: CGRect) {
1818
super.init(frame: frame)
1919

2020
circle.fillColor = UIColor.darkGrayColor().CGColor
@@ -23,23 +23,23 @@ public class CircleInfinityAnimator: UIView, CustomInfinityScrollAnimator {
2323

2424
self.layer.addSublayer(circle)
2525
}
26-
public required init?(coder aDecoder: NSCoder) {
26+
required init?(coder aDecoder: NSCoder) {
2727
fatalError("init(coder:) has not been implemented")
2828
}
29-
public override func layoutSubviews() {
29+
override func layoutSubviews() {
3030
super.layoutSubviews()
3131

3232
circle.frame = self.bounds
3333
}
34-
public override func didMoveToWindow() {
34+
override func didMoveToWindow() {
3535
super.didMoveToWindow()
3636

3737
if window != nil && animating {
3838
startAnimating()
3939
}
4040
}
4141

42-
public func animateState(state: InfinityScrollState) {
42+
func animateState(state: InfinityScrollState) {
4343
switch state {
4444
case .None:
4545
stopAnimating()

InfinitySample/samples/circle/CircleRefreshAnimator.swift

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,12 @@
99
import UIKit
1010
import Infinity
1111

12-
public class CircleRefreshAnimator: UIView, CustomPullToRefreshAnimator {
12+
class CircleRefreshAnimator: UIView, CustomPullToRefreshAnimator {
1313

14-
public var circle = CAShapeLayer()
15-
public private(set) var animating = false
14+
var circle = CAShapeLayer()
15+
private(set) var animating = false
1616

17-
public override init(frame: CGRect) {
17+
override init(frame: CGRect) {
1818
super.init(frame: frame)
1919

2020
circle.fillColor = UIColor.darkGrayColor().CGColor
@@ -23,23 +23,23 @@ public class CircleRefreshAnimator: UIView, CustomPullToRefreshAnimator {
2323

2424
self.layer.addSublayer(circle)
2525
}
26-
public required init?(coder aDecoder: NSCoder) {
26+
required init?(coder aDecoder: NSCoder) {
2727
fatalError("init(coder:) has not been implemented")
2828
}
2929

30-
public override func layoutSubviews() {
30+
override func layoutSubviews() {
3131
super.layoutSubviews()
3232

3333
circle.frame = self.bounds
3434
}
35-
public override func didMoveToWindow() {
35+
override func didMoveToWindow() {
3636
super.didMoveToWindow()
3737

3838
if window != nil && animating {
3939
startAnimating()
4040
}
4141
}
42-
public func animateState(state: PullToRefreshState) {
42+
func animateState(state: PullToRefreshState) {
4343
switch state {
4444
case .None:
4545
stopAnimating()

InfinitySample/samples/snake/SnakeInfinityAnimator.swift

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,22 +15,22 @@ extension UIColor {
1515
}
1616
}
1717

18-
public class SnakeInfinityAnimator: UIView, CustomInfinityScrollAnimator {
18+
class SnakeInfinityAnimator: UIView, CustomInfinityScrollAnimator {
1919

20-
public var color: UIColor? {
20+
var color: UIColor? {
2121
didSet {
2222
snakeLayer.strokeColor = color?.CGColor
2323
}
2424
}
25-
public var animating = false
25+
var animating = false
2626

2727
private var snakeLayer = CAShapeLayer()
2828
private var snakeLengthByCycle:CGFloat = 0 // 占的周期数
2929
private var cycleCount = 1000
3030

3131
private var pathLength:CGFloat = 0
3232

33-
public override init(frame: CGRect) {
33+
override init(frame: CGRect) {
3434
super.init(frame: frame)
3535

3636
let ovalDiametor = frame.width / 4
@@ -59,18 +59,18 @@ public class SnakeInfinityAnimator: UIView, CustomInfinityScrollAnimator {
5959
snakeLayer.frame = self.bounds
6060
self.layer.addSublayer(snakeLayer)
6161
}
62-
public required init?(coder aDecoder: NSCoder) {
62+
required init?(coder aDecoder: NSCoder) {
6363
fatalError("init(coder:) has not been implemented")
6464
}
65-
public override func didMoveToWindow() {
65+
override func didMoveToWindow() {
6666
super.didMoveToWindow()
6767

6868
if window != nil && animating {
6969
startAnimating()
7070
}
7171
}
7272

73-
public func animateState(state: InfinityScrollState) {
73+
func animateState(state: InfinityScrollState) {
7474
switch state {
7575
case .None:
7676
stopAnimating()

InfinitySample/samples/snake/SnakeRefreshAnimator.swift

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,22 +9,22 @@
99
import UIKit
1010
import Infinity
1111

12-
public class SnakeRefreshAnimator: UIView, CustomPullToRefreshAnimator {
12+
class SnakeRefreshAnimator: UIView, CustomPullToRefreshAnimator {
1313

14-
public var color: UIColor? {
14+
var color: UIColor? {
1515
didSet {
1616
snakeLayer.strokeColor = color?.CGColor
1717
}
1818
}
19-
public var animating = false
19+
var animating = false
2020

2121
private var snakeLayer = CAShapeLayer()
2222
private var snakeLengthByCycle:CGFloat = 0 // 显示的长度所占周期数
2323
private var cycleCount = 1000
2424

2525
private var pathLength:CGFloat = 0
2626

27-
public override init(frame: CGRect) {
27+
override init(frame: CGRect) {
2828
super.init(frame: frame)
2929

3030
let ovalDiametor = frame.width / 4
@@ -53,17 +53,17 @@ public class SnakeRefreshAnimator: UIView, CustomPullToRefreshAnimator {
5353
snakeLayer.frame = self.bounds
5454
self.layer.addSublayer(snakeLayer)
5555
}
56-
public required init?(coder aDecoder: NSCoder) {
56+
required init?(coder aDecoder: NSCoder) {
5757
fatalError("init(coder:) has not been implemented")
5858
}
59-
public override func didMoveToWindow() {
59+
override func didMoveToWindow() {
6060
super.didMoveToWindow()
6161

6262
if window != nil && animating {
6363
startAnimating()
6464
}
6565
}
66-
public func animateState(state: PullToRefreshState) {
66+
func animateState(state: PullToRefreshState) {
6767
switch state {
6868
case .None:
6969
stopAnimating()

InfinitySample/samples/spark/SparkInfinityAnimator.swift

Lines changed: 92 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,99 @@
77
//
88

99
import UIKit
10+
import Infinity
1011

11-
class SparkInfinityAnimator: UIView {
12-
12+
class SparkInfinityAnimator: UIView, CustomInfinityScrollAnimator {
13+
14+
private var circles = [CAShapeLayer]()
15+
var animating = false
16+
17+
private var positions = [CGPoint]()
18+
19+
override init(frame: CGRect) {
20+
super.init(frame: frame)
21+
22+
let ovalDiameter = min(frame.width,frame.height) / 8
23+
let ovalPath = UIBezierPath(ovalInRect: CGRect(x: 0, y: 0, width: ovalDiameter, height: ovalDiameter))
24+
25+
let count = 8
26+
for index in 0..<count {
27+
let circleLayer = CAShapeLayer()
28+
circleLayer.path = ovalPath.CGPath
29+
circleLayer.fillColor = UIColor.sparkColorWithIndex(index).CGColor
30+
circleLayer.anchorPoint = CGPoint(x: 0.5, y: 0.5)
31+
32+
self.circles.append(circleLayer)
33+
self.layer.addSublayer(circleLayer)
34+
35+
let angle = CGFloat(M_PI * 2) / CGFloat(count) * CGFloat(index)
36+
37+
let radius = min(frame.width, frame.height) * 0.4
38+
let position = CGPoint(x: bounds.midX + sin(angle) * radius, y: bounds.midY - cos(angle) * radius)
39+
circleLayer.position = position
40+
41+
positions.append(position)
42+
}
43+
}
44+
required init?(coder aDecoder: NSCoder) {
45+
fatalError("init(coder:) has not been implemented")
46+
}
47+
override func didMoveToWindow() {
48+
super.didMoveToWindow()
49+
50+
if window != nil && animating {
51+
startAnimating()
52+
}
53+
}
54+
55+
func animateState(state: InfinityScrollState) {
56+
switch state {
57+
case .None:
58+
stopAnimating()
59+
case .Loading:
60+
startAnimating()
61+
}
62+
}
63+
func startAnimating() {
64+
animating = true
65+
for index in 0..<8 {
66+
applyAnimationForIndex(index)
67+
}
68+
}
69+
private let CircleAnimationKey = "CircleAnimationKey"
70+
private func applyAnimationForIndex(index: Int) {
71+
let moveAnimation = CAKeyframeAnimation(keyPath: "position")
72+
let moveV1 = NSValue(CGPoint: positions[index])
73+
let moveV2 = NSValue(CGPoint: CGPoint(x: bounds.midX, y: bounds.midY))
74+
let moveV3 = NSValue(CGPoint: positions[index])
75+
moveAnimation.values = [moveV1,moveV2,moveV3]
76+
77+
let scaleAnimation = CAKeyframeAnimation(keyPath: "transform")
78+
let scaleV1 = NSValue(CATransform3D: CATransform3DIdentity)
79+
let scaleV2 = NSValue(CATransform3D: CATransform3DMakeScale(0.1, 0.1, 1.0))
80+
let scaleV3 = NSValue(CATransform3D: CATransform3DIdentity)
81+
scaleAnimation.values = [scaleV1,scaleV2,scaleV3]
82+
83+
let animationGroup = CAAnimationGroup()
84+
animationGroup.animations = [moveAnimation,scaleAnimation]
85+
animationGroup.duration = 1.0
86+
animationGroup.repeatCount = 1000
87+
animationGroup.beginTime = CACurrentMediaTime() + Double(index) * animationGroup.duration / 8 / 2
88+
animationGroup.timingFunction = CAMediaTimingFunction(controlPoints: 1, 0.5, 0, 0.5)
89+
90+
let circleLayer = circles[index]
91+
circleLayer.hidden = false
92+
circleLayer.addAnimation(animationGroup, forKey: CircleAnimationKey)
93+
94+
}
95+
func stopAnimating() {
96+
for circleLayer in circles {
97+
circleLayer.removeAnimationForKey(CircleAnimationKey)
98+
circleLayer.transform = CATransform3DIdentity
99+
circleLayer.hidden = true
100+
}
101+
animating = false
102+
}
13103
/*
14104
// Only override drawRect: if you perform custom drawing.
15105
// An empty implementation adversely affects performance during animation.

0 commit comments

Comments
 (0)