forked from yankodimitrov/SwiftPasscodeLock
-
Notifications
You must be signed in to change notification settings - Fork 51
Expand file tree
/
Copy pathPasscodeSignButton.swift
More file actions
103 lines (75 loc) · 2.4 KB
/
PasscodeSignButton.swift
File metadata and controls
103 lines (75 loc) · 2.4 KB
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
//
// PasscodeSignButton.swift
// PasscodeLock
//
// Created by Yanko Dimitrov on 8/28/15.
// Copyright © 2015 Yanko Dimitrov. All rights reserved.
//
import UIKit
@IBDesignable
public class PasscodeSignButton: UIButton {
@IBInspectable
public var passcodeSign: String = "1"
@IBInspectable
public var borderColor: UIColor = UIColor.whiteColor() {
didSet {
setupView()
}
}
@IBInspectable
public var borderRadius: CGFloat = 30 {
didSet {
setupView()
}
}
@IBInspectable
public var highlightBackgroundColor: UIColor = UIColor.clearColor() {
didSet {
setupView()
}
}
public override init(frame: CGRect) {
super.init(frame: frame)
setupView()
setupActions()
}
public required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
setupActions()
}
public override func intrinsicContentSize() -> CGSize {
return CGSizeMake(60, 60)
}
public var defaultBackgroundColor = UIColor.clearColor()
private func setupView() {
layer.borderWidth = 1
layer.cornerRadius = borderRadius
layer.borderColor = borderColor.CGColor
if let backgroundColor = backgroundColor {
defaultBackgroundColor = backgroundColor
}
}
private func setupActions() {
addTarget(self, action: #selector(PasscodeSignButton.handleTouchDown), forControlEvents: .TouchDown)
addTarget(self, action: #selector(PasscodeSignButton.handleTouchUp), forControlEvents: [.TouchUpInside, .TouchDragOutside, .TouchCancel])
}
func handleTouchDown() {
animateBackgroundColor(highlightBackgroundColor)
}
func handleTouchUp() {
animateBackgroundColor(defaultBackgroundColor)
}
private func animateBackgroundColor(color: UIColor) {
UIView.animateWithDuration(
0.3,
delay: 0.0,
usingSpringWithDamping: 1,
initialSpringVelocity: 0.0,
options: [.AllowUserInteraction, .BeginFromCurrentState],
animations: {
self.backgroundColor = color
},
completion: nil
)
}
}