forked from Boris-Em/BEMCheckBox
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBEMPathManager.swift
114 lines (93 loc) · 4.01 KB
/
BEMPathManager.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
//
// BEMPathManager.swift
// CheckBox
//
// Created by Bobo on 9/19/15.
// Copyright (c) 2015 Boris Emorine. All rights reserved.
//
import UIKit
final class BEMPathManager: NSObject {
/** The paths are assumed to be created in squares.
* This is the size of width, or height, of the paths that will be created.
*/
var size: CGFloat = 0.0
/** The width of the lines on the created paths. */
var lineWidth: CGFloat = 0.0
/** The corner radius of the path when the boxType is BEMBoxTypeSquare. */
var cornerRadius: CGFloat = 0.0
/** The type of box.
* Depending on the box type, paths may be created differently
* @see BEMBoxType
*/
var boxType: BEMCheckBox.BoxType = .circle
/** Returns a UIBezierPath object for the box of the checkbox
* @returns The path of the box.
*/
func pathForBox() -> UIBezierPath {
var path: UIBezierPath
switch boxType {
case .square:
let offset = lineWidth / 2.0
let size = self.size - lineWidth
path = UIBezierPath(
roundedRect: CGRect(
x: 0.0, y: 0.0,
width: size, height: size),
cornerRadius: cornerRadius)
path.apply(CGAffineTransform.identity.rotated(by: CGFloat.pi * 2.5))
path.apply(CGAffineTransform(translationX: size + offset, y: offset))
default:
let radius: CGFloat = (size - lineWidth) / 2
path = UIBezierPath(
arcCenter: CGPoint(x: size / 2, y: size / 2),
radius: radius,
startAngle: -.pi / 4,
endAngle: 2 * .pi - .pi / 4,
clockwise: true)
}
return path
}
/** Returns a UIBezierPath object for the checkmark of the checkbox
* @returns The path of the checkmark.
*/
func pathForCheckMark() -> UIBezierPath {
let checkMarkPath = UIBezierPath()
checkMarkPath.move(to: CGPoint(x: size / 3.1578, y: size / 2))
checkMarkPath.addLine(to: CGPoint(x: size / 2.0618, y: size / 1.57894))
checkMarkPath.addLine(to: CGPoint(x: size / 1.3953, y: size / 2.7272))
if boxType == .square {
// If we use a square box, the check mark should be a little bit bigger
checkMarkPath.apply(CGAffineTransform(scaleX: 1.5, y: 1.5))
checkMarkPath.apply(CGAffineTransform(translationX: -size / 4, y: -size / 4))
}
return checkMarkPath
}
/** Returns a UIBezierPath object for an extra long checkmark which is in contact with the box.
* @returns The path of the checkmark.
*/
func pathForLongCheckMark() -> UIBezierPath {
let checkMarkPath = UIBezierPath()
checkMarkPath.move(to: CGPoint(x: size / 3.1578, y: size / 2))
checkMarkPath.addLine(to: CGPoint(x: size / 2.0618, y: size / 1.57894))
if boxType == .square {
// If we use a square box, the check mark should be a little bit bigger
checkMarkPath.addLine(to: CGPoint(x: size / 1.2053, y: size / 4.5272))
checkMarkPath.apply(CGAffineTransform(scaleX: 1.5, y: 1.5))
checkMarkPath.apply(CGAffineTransform(translationX: -size / 4, y: -size / 4))
} else {
checkMarkPath.addLine(to: CGPoint(x: size / 1.1553, y: size / 5.9272))
}
return checkMarkPath
}
/** Returns a UIBezierPath object for the flat checkmark of the checkbox
* @see BEMAnimationTypeFlat
* @returns The path of the flat checkmark.
*/
func pathForFlatCheckMark() -> UIBezierPath {
let flatCheckMarkPath = UIBezierPath()
flatCheckMarkPath.move(to: CGPoint(x: size / 4, y: size / 2))
flatCheckMarkPath.addLine(to: CGPoint(x: size / 2, y: size / 2))
flatCheckMarkPath.addLine(to: CGPoint(x: size / 1.2, y: size / 2))
return flatCheckMarkPath
}
}