-
Notifications
You must be signed in to change notification settings - Fork 1
/
PPDrawableArrow.swift
177 lines (141 loc) · 4.98 KB
/
PPDrawableArrow.swift
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
//
// PPDrawableArrow.swift
// drawArch
//
// Created by Abhijit KG on 30/08/17.
// Copyright © 2017 PhonePe. All rights reserved.
//
import Foundation
import UIKit
public enum PPCurveDirection {
case up
case down
case left
case right
var firstAngle: CGFloat {
switch self {
case .up:
return -45
case .down:
return 45
case .left:
return 135
case .right:
return 45
}
}
var secondAngle: CGFloat {
switch self {
case .up:
return 135
case .down:
return -135
case .left:
return -135
case .right:
return -45
}
}
var firstFactor: CGFloat {
switch self {
case .up:
return 1
case .down:
return 1
case .left:
return -1
case .right:
return 1
}
}
var secondFactor: CGFloat {
switch self {
case .up:
return -1
case .down:
return -1
case .left:
return -1
case .right:
return 1
}
}
}
final public class PPDrawableArrow {
public static func drawCurve(inView: UIView,
from: CGPoint,
to: CGPoint,
curveDirection: PPCurveDirection,
animated: Bool = true,
duration:TimeInterval = 0.25,
lineColor: UIColor,
lineWidth: CGFloat = 1.0,
completion:((_ hasFinished: Bool, _ error: String?) -> Void)? = nil) -> CALayer? {
if from == to {
completion?(false, "from and to points cant be same")
return nil
}
if PPDrawableArrow.isValidInputs(from: from, to: to, curveDirection: curveDirection) == false {
completion?(false, "wrong arrow direction for the given points")
return nil
}
let path = UIBezierPath()
path.move(to: CGPoint(x: from.x, y: from.y))
let controlPonint = PPDrawableArrow.getControlPoint(fromPoint: from, toPoint: to, direction: curveDirection)
path.addQuadCurve(to: CGPoint(x: to.x, y: to.y), controlPoint: controlPonint)
let arrowLenght:CGFloat = 8.0
let firstEndOfArrow = CGPoint(x: to.x - arrowLenght * curveDirection.firstFactor , y: to.y - (tan(curveDirection.firstAngle * (.pi / 180)) * arrowLenght))
path.addLine(to: firstEndOfArrow)
path.move(to: CGPoint(x: to.x, y: to.y))
let secondEndOfArrow = CGPoint(x: to.x - arrowLenght * curveDirection.secondFactor, y: to.y - (tan(curveDirection.secondAngle * (.pi / 180)) * arrowLenght))
path.addLine(to: secondEndOfArrow)
let layer = CAShapeLayer()
layer.path = path.cgPath
layer.strokeColor = UIColor.clear.cgColor
layer.lineWidth = lineWidth
layer.fillColor = nil
layer.lineJoin = kCALineCapSquare
inView.layer.addSublayer(layer)
CATransaction.begin()
// Add the animation to the curve
CATransaction.setCompletionBlock {
completion?(true, nil)
}
if animated {
let animate = CABasicAnimation(keyPath: "strokeEnd")
animate.repeatCount = 1.0
// Animate from the full stroke being drawn to none of the stroke being drawn
animate.fromValue = NSNumber(value: 0.0)
animate.toValue = NSNumber(value: 1.0)
animate.duration = duration
animate.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseIn)
layer.strokeColor = lineColor.cgColor
layer.add(animate, forKey: "drawAnimation")
}
CATransaction.commit()
return layer
}
private static func getControlPoint(fromPoint: CGPoint, toPoint: CGPoint, direction: PPCurveDirection) -> CGPoint {
switch direction {
case .right, .left:
return CGPoint(x: fromPoint.x, y: toPoint.y)
case .up,.down:
return CGPoint(x: toPoint.x , y: fromPoint.y)
}
}
private static func isValidInputs(from: CGPoint, to: CGPoint, curveDirection: PPCurveDirection) -> Bool {
if (from.y > to.y) && curveDirection == .down {
return false
}
if (from.x > to.x) && curveDirection == .right {
return false
}
if (to.y > from.y) && curveDirection == .up {
return false
}
if (to.x > from.x) && curveDirection == .left {
return false
}
return true
}
}