-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathHCGlobeButton.swift
153 lines (121 loc) · 5.08 KB
/
HCGlobeButton.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
//
// HCGlobeButton.swift
// HCPolytonicGreekKBapp
//
// Created by Jeremy March on 1/18/17.
// Copyright © 2017 Jeremy March. All rights reserved.
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
//
import UIKit
class HCGlobeButton: UIButton {
var lineColor1:UIColor = HopliteConstants.otherTextColor
var bgColor1:UIColor = HopliteConstants.otherBGColor
var lineColorDown:UIColor = HopliteConstants.otherTextColorDown
var bgColorDown:UIColor = HopliteConstants.otherBGColorDown
var buttonDown:Bool = false
required init() {
super.init(frame: .zero)
self.addTarget(self, action: #selector(touchUpInside1(sender:)), for: .touchUpInside)
self.addTarget(self, action: #selector(touchUpOutside1(sender:)), for: .touchUpOutside)
self.addTarget(self, action: #selector(touchDown1(sender:)), for: .touchDown)
self.addTarget(self, action: #selector(touchDown1(sender:)), for: .touchDragEnter)
self.addTarget(self, action: #selector(touchUpInside1(sender:)), for: .touchDragExit)
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
@objc func touchUpInside1(sender: UIButton!) {
buttonDown = false
setNeedsDisplay()
}
@objc func touchUpOutside1(sender: UIButton!) {
buttonDown = false
setNeedsDisplay()
}
@objc func touchDown1(sender: UIButton!) {
buttonDown = true
setNeedsDisplay()
}
func radians(_ degrees:Double) -> Double
{
return degrees * Double.pi/180;
}
override func draw(_ rect: CGRect) {
let ctx = UIGraphicsGetCurrentContext()
var lineColor:CGColor?
var bgColor:CGColor?
if buttonDown
{
lineColor = lineColorDown.cgColor
bgColor = bgColorDown.cgColor
}
else
{
lineColor = lineColor1.cgColor
bgColor = bgColor1.cgColor
}
// Draw background
let outerRect:CGRect = self.bounds.insetBy(dx: 0, dy: 0);
let outerPath:CGPath
if UIDevice.current.userInterfaceIdiom == .pad
{
outerPath = UIBezierPath(roundedRect: outerRect, cornerRadius: HopliteConstants.ipadRadius ).cgPath
}
else
{
outerPath = UIBezierPath(roundedRect: outerRect, cornerRadius: HopliteConstants.normalRadius).cgPath
}
ctx!.addPath(outerPath)
ctx!.setFillColor(bgColor!)
ctx?.fillPath()
let min:CGFloat
if self.frame.size.width > self.frame.size.height
{
min = self.frame.size.height
}
else
{
min = self.frame.size.width
}
let center = CGPoint(x:self.frame.size.width / 2.0, y:self.frame.size.height / 2.0)
var vCenter = CGPoint(x:self.frame.size.width / 2.0, y:self.frame.size.height / 2.0)
ctx!.beginPath()
ctx!.setLineWidth(1.5)
ctx!.setStrokeColor(lineColor!)
let radius:CGFloat = min / 3.0
let endAngle: CGFloat = CGFloat(2 * Double.pi)
//circle
ctx!.addArc(center: center, radius: radius, startAngle: 0.0, endAngle: endAngle, clockwise: true)
ctx!.strokePath()
//clip everything outside the circle
ctx!.addArc(center: center, radius: radius, startAngle: 0.0, endAngle: endAngle, clockwise: true)
ctx!.clip()
//left arc
vCenter = CGPoint(x:center.x + (radius/0.73), y:center.y)
ctx!.addArc(center: vCenter, radius: radius + (radius/1.2), startAngle: 0.0, endAngle: endAngle, clockwise: true)
ctx!.strokePath()
//right arc
vCenter = CGPoint(x:center.x - (radius/0.73), y:center.y)
ctx!.addArc(center: vCenter, radius: radius + (radius/1.2), startAngle: 0.0, endAngle: endAngle, clockwise: true)
ctx!.strokePath()
//bottom arc
vCenter = CGPoint(x:center.x, y:center.y + (radius*2.29))
ctx!.addArc(center: vCenter, radius: radius + (radius/1.2), startAngle: 0.0, endAngle: endAngle, clockwise: true)
ctx!.strokePath()
//top arc
vCenter = CGPoint(x:center.x, y:center.y - (radius*2.29))
ctx!.addArc(center: vCenter, radius: radius + (radius/1.2), startAngle: 0.0, endAngle: endAngle, clockwise: true)
ctx!.strokePath()
//horizontal line
ctx!.move(to: CGPoint(x:0, y:self.frame.size.height / 2.0))
ctx!.addLine(to: CGPoint(x:self.frame.size.width, y: self.frame.size.height / 2.0))
ctx!.strokePath()
//vertical line
ctx!.move(to: CGPoint(x:self.frame.size.width / 2.0, y:0))
ctx!.addLine(to: CGPoint(x:self.frame.size.width / 2.0, y: self.frame.size.height))
ctx!.strokePath()
}
}