Skip to content

Commit eb4fdf6

Browse files
committed
Refactor HasAnimatablePath
1 parent 84830f1 commit eb4fdf6

File tree

6 files changed

+37
-43
lines changed

6 files changed

+37
-43
lines changed

Example/ViewController.swift

+12
Original file line numberDiff line numberDiff line change
@@ -64,3 +64,15 @@ class ViewController: UIViewController {
6464
}
6565
}
6666

67+
class AnimatableDoneView2: UIView, HasAnimatablePath {
68+
let animatableLayer = CAShapeLayer()
69+
var animatablePath: UIBezierPath {
70+
let length = frame.width
71+
let path = UIBezierPath()
72+
path.move(to: CGPoint(x: length * 0.196, y: length * 0.527))
73+
path.addLine(to: CGPoint(x: length * 0.47, y: length * 0.777))
74+
path.addLine(to: CGPoint(x: length * 0.99, y: length * 0.25))
75+
return path
76+
}
77+
}
78+

NativePopup.xcodeproj/project.pbxproj

+4-8
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,13 @@
88

99
/* Begin PBXBuildFile section */
1010
FE4CECF61ED1171400D5DB31 /* AnimatableCrossView.swift in Sources */ = {isa = PBXBuildFile; fileRef = FE4CECF51ED1171400D5DB31 /* AnimatableCrossView.swift */; };
11-
FE4CECF81ED119F800D5DB31 /* HasAnimatablePath.swift in Sources */ = {isa = PBXBuildFile; fileRef = FE4CECF71ED119F800D5DB31 /* HasAnimatablePath.swift */; };
1211
FE8533901EAC7C92004BB756 /* NativePopup.h in Headers */ = {isa = PBXBuildFile; fileRef = FE85338E1EAC7C92004BB756 /* NativePopup.h */; settings = {ATTRIBUTES = (Public, ); }; };
1312
FEB0316D1ECFF6770039A1C6 /* Preset.swift in Sources */ = {isa = PBXBuildFile; fileRef = FEB0316C1ECFF6770039A1C6 /* Preset.swift */; };
1413
FEB031711ECFF6A30039A1C6 /* Bundle.extension.swift in Sources */ = {isa = PBXBuildFile; fileRef = FEB031701ECFF6A30039A1C6 /* Bundle.extension.swift */; };
1514
FEB031731ECFF6E00039A1C6 /* InitialEffectType.swift in Sources */ = {isa = PBXBuildFile; fileRef = FEB031721ECFF6E00039A1C6 /* InitialEffectType.swift */; };
1615
FEB031751ECFFB6C0039A1C6 /* Animatable.swift in Sources */ = {isa = PBXBuildFile; fileRef = FEB031741ECFFB6C0039A1C6 /* Animatable.swift */; };
1716
FEB031771ECFFE120039A1C6 /* AnimatableDoneView.swift in Sources */ = {isa = PBXBuildFile; fileRef = FEB031761ECFFE120039A1C6 /* AnimatableDoneView.swift */; };
18-
FEB031791ED014D70039A1C6 /* HasAnimatableLayer.swift in Sources */ = {isa = PBXBuildFile; fileRef = FEB031781ED014D70039A1C6 /* HasAnimatableLayer.swift */; };
17+
FEB031791ED014D70039A1C6 /* HasAnimatablePath.swift in Sources */ = {isa = PBXBuildFile; fileRef = FEB031781ED014D70039A1C6 /* HasAnimatablePath.swift */; };
1918
FECA87BA1EAC7CCF00D07CB1 /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = FECA87B91EAC7CCF00D07CB1 /* AppDelegate.swift */; };
2019
FECA87BC1EAC7CCF00D07CB1 /* ViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = FECA87BB1EAC7CCF00D07CB1 /* ViewController.swift */; };
2120
FECA87BF1EAC7CCF00D07CB1 /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = FECA87BD1EAC7CCF00D07CB1 /* Main.storyboard */; };
@@ -57,7 +56,6 @@
5756

5857
/* Begin PBXFileReference section */
5958
FE4CECF51ED1171400D5DB31 /* AnimatableCrossView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = AnimatableCrossView.swift; sourceTree = "<group>"; };
60-
FE4CECF71ED119F800D5DB31 /* HasAnimatablePath.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = HasAnimatablePath.swift; sourceTree = "<group>"; };
6159
FE85338B1EAC7C92004BB756 /* NativePopup.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = NativePopup.framework; sourceTree = BUILT_PRODUCTS_DIR; };
6260
FE85338E1EAC7C92004BB756 /* NativePopup.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = NativePopup.h; sourceTree = "<group>"; };
6361
FE85338F1EAC7C92004BB756 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
@@ -66,7 +64,7 @@
6664
FEB031721ECFF6E00039A1C6 /* InitialEffectType.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = InitialEffectType.swift; sourceTree = "<group>"; };
6765
FEB031741ECFFB6C0039A1C6 /* Animatable.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Animatable.swift; sourceTree = "<group>"; };
6866
FEB031761ECFFE120039A1C6 /* AnimatableDoneView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = AnimatableDoneView.swift; sourceTree = "<group>"; };
69-
FEB031781ED014D70039A1C6 /* HasAnimatableLayer.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = HasAnimatableLayer.swift; sourceTree = "<group>"; };
67+
FEB031781ED014D70039A1C6 /* HasAnimatablePath.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = HasAnimatablePath.swift; sourceTree = "<group>"; };
7068
FECA87B71EAC7CCF00D07CB1 /* Example.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = Example.app; sourceTree = BUILT_PRODUCTS_DIR; };
7169
FECA87B91EAC7CCF00D07CB1 /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = "<group>"; };
7270
FECA87BB1EAC7CCF00D07CB1 /* ViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ViewController.swift; sourceTree = "<group>"; };
@@ -105,8 +103,7 @@
105103
isa = PBXGroup;
106104
children = (
107105
FEB031741ECFFB6C0039A1C6 /* Animatable.swift */,
108-
FEB031781ED014D70039A1C6 /* HasAnimatableLayer.swift */,
109-
FE4CECF71ED119F800D5DB31 /* HasAnimatablePath.swift */,
106+
FEB031781ED014D70039A1C6 /* HasAnimatablePath.swift */,
110107
FE4CECF51ED1171400D5DB31 /* AnimatableCrossView.swift */,
111108
FEB031761ECFFE120039A1C6 /* AnimatableDoneView.swift */,
112109
);
@@ -293,10 +290,9 @@
293290
FEB031711ECFF6A30039A1C6 /* Bundle.extension.swift in Sources */,
294291
FEB031731ECFF6E00039A1C6 /* InitialEffectType.swift in Sources */,
295292
FEB031771ECFFE120039A1C6 /* AnimatableDoneView.swift in Sources */,
296-
FEB031791ED014D70039A1C6 /* HasAnimatableLayer.swift in Sources */,
293+
FEB031791ED014D70039A1C6 /* HasAnimatablePath.swift in Sources */,
297294
FEB031751ECFFB6C0039A1C6 /* Animatable.swift in Sources */,
298295
FECA87E31EACC7F000D07CB1 /* UIImageConvertible.swift in Sources */,
299-
FE4CECF81ED119F800D5DB31 /* HasAnimatablePath.swift in Sources */,
300296
FECA87F21EADBA3C00D07CB1 /* Extension.swift in Sources */,
301297
FECA87D81EAC7D9E00D07CB1 /* NativePopup.swift in Sources */,
302298
FEB0316D1ECFF6770039A1C6 /* Preset.swift in Sources */,

NativePopup/Animatable/Animatable.swift

-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import Foundation
1010

1111
public protocol Animatable {
1212
func animate()
13-
var duration: TimeInterval { get }
1413
}
1514

1615
public extension Animatable {

NativePopup/Animatable/HasAnimatableLayer.swift

-25
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,38 @@
11
//
2-
// HasAnimatablePath.swift
2+
// HasAnimatableLayer.swift
33
// NativePopup
44
//
5-
// Created by mono on 2017/05/21.
5+
// Created by mono on 2017/05/20.
66
// Copyright © 2017 mono. All rights reserved.
77
//
88

99
import Foundation
1010

11-
public protocol HasAnimatablePath: HasAnimatableLayer {
11+
public protocol HasAnimatablePath: Animatable {
12+
var layer: CALayer { get }
13+
var tintColor: UIColor! { get }
14+
/** Should return same instance */
15+
var animatableLayer: CAShapeLayer { get }
1216
var animatablePath: UIBezierPath { get }
13-
func setupLayer()
1417
}
1518

16-
public extension HasAnimatablePath where Self: UIView {
17-
public func setupLayer() {
19+
public extension HasAnimatablePath {
20+
public func animate() {
21+
let animation = CABasicAnimation(keyPath: "strokeEnd")
22+
animation.duration = duration
23+
animation.fromValue = 0
24+
animation.toValue = 1
25+
animation.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseInEaseOut)
26+
animatableLayer.strokeEnd = 1
27+
animatableLayer.add(animation, forKey: "animation")
28+
}
29+
public func configureAnimatableLayer() {
1830
animatableLayer.path = animatablePath.cgPath
1931
animatableLayer.fillColor = UIColor.clear.cgColor
20-
animatableLayer.strokeColor = tintColor.cgColor
32+
animatableLayer.strokeColor = tintColor?.cgColor
2133
animatableLayer.lineWidth = 9
2234
animatableLayer.lineCap = kCALineCapRound
2335
animatableLayer.lineJoin = kCALineCapRound
2436
animatableLayer.strokeEnd = 0
25-
layer.addSublayer(animatableLayer)
2637
}
2738
}

NativePopup/NativePopup.swift

+2-1
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,8 @@ public class NativePopup: UIView {
112112

113113
if let animatable = imageView as? HasAnimatablePath {
114114
imageView.layoutIfNeeded()
115-
animatable.setupLayer()
115+
animatable.configureAnimatableLayer()
116+
animatable.layer.addSublayer(animatable.animatableLayer)
116117
}
117118
}
118119

0 commit comments

Comments
 (0)