Skip to content

Commit bd19e79

Browse files
authored
Merge pull request #17 from gumbright/develop
Remove touch "flash" and make it work more like a UIButton
2 parents 3e3c543 + 5449c36 commit bd19e79

File tree

1 file changed

+58
-8
lines changed

1 file changed

+58
-8
lines changed

LGButton/Classes/LGButton.swift

Lines changed: 58 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,22 @@
88
import UIKit
99
import QuartzCore
1010

11+
1112
@IBDesignable
1213
public class LGButton: UIControl {
1314

15+
enum TouchAlphaValues : CGFloat {
16+
case touched = 0.7
17+
case untouched = 1.0
18+
}
19+
20+
let touchDisableRadius : CGFloat = 100.0
21+
1422
let availableFontIcons = ["fa", "io", "oc", "ic", "ma", "ti", "mi"]
1523

1624
var gradient : CAGradientLayer?
17-
25+
26+
1827
fileprivate var rootView : UIView!
1928
@IBOutlet fileprivate weak var titleLbl: UILabel!
2029
@IBOutlet fileprivate weak var mainStackView: UIStackView!
@@ -589,16 +598,57 @@ public class LGButton: UIControl {
589598

590599
// MARK: - Touches
591600
// MARK:
592-
override public func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
593-
if showTouchFeedback {
594-
alpha = 0.7
595-
UIView.animate(withDuration: 0.3) {
596-
self.alpha = 1
601+
var touchAlpha : TouchAlphaValues = .untouched {
602+
didSet {
603+
updateTouchAlpha()
604+
}
605+
}
606+
607+
var pressed : Bool = false {
608+
didSet {
609+
if !showTouchFeedback {
610+
return
597611
}
612+
613+
touchAlpha = (pressed) ? .touched : .untouched
598614
}
599615
}
616+
617+
override public func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?){
618+
pressed = true
619+
}
620+
621+
override public func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?){
622+
let shouldSendActions = pressed
623+
pressed = false
624+
if shouldSendActions{
625+
sendActions(for: .touchUpInside)
626+
}
627+
}
628+
629+
override public func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?){
630+
if let touchLoc = touches.first?.location(in: self){
631+
if (touchLoc.x < -touchDisableRadius ||
632+
touchLoc.y < -touchDisableRadius ||
633+
touchLoc.x > self.bounds.size.width + touchDisableRadius ||
634+
touchLoc.y > self.bounds.size.height + touchDisableRadius){
635+
pressed = false
636+
}
637+
else if self.touchAlpha == .untouched {
638+
pressed = true
639+
}
640+
}
641+
}
642+
643+
override public func touchesCancelled(_ touches: Set<UITouch>, with event: UIEvent?) {
644+
pressed = false
645+
}
600646

601-
override public func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
602-
sendActions(for: .touchUpInside)
647+
func updateTouchAlpha() {
648+
if self.alpha != self.touchAlpha.rawValue {
649+
UIView.animate(withDuration: 0.3) {
650+
self.alpha = self.touchAlpha.rawValue
651+
}
652+
}
603653
}
604654
}

0 commit comments

Comments
 (0)