|
8 | 8 | import UIKit |
9 | 9 | import QuartzCore |
10 | 10 |
|
| 11 | + |
11 | 12 | @IBDesignable |
12 | 13 | public class LGButton: UIControl { |
13 | 14 |
|
| 15 | + enum TouchAlphaValues : CGFloat { |
| 16 | + case touched = 0.7 |
| 17 | + case untouched = 1.0 |
| 18 | + } |
| 19 | + |
| 20 | + let touchDisableRadius : CGFloat = 100.0 |
| 21 | + |
14 | 22 | let availableFontIcons = ["fa", "io", "oc", "ic", "ma", "ti", "mi"] |
15 | 23 |
|
16 | 24 | var gradient : CAGradientLayer? |
17 | | - |
| 25 | + |
| 26 | + |
18 | 27 | fileprivate var rootView : UIView! |
19 | 28 | @IBOutlet fileprivate weak var titleLbl: UILabel! |
20 | 29 | @IBOutlet fileprivate weak var mainStackView: UIStackView! |
@@ -589,16 +598,57 @@ public class LGButton: UIControl { |
589 | 598 |
|
590 | 599 | // MARK: - Touches |
591 | 600 | // 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 |
597 | 611 | } |
| 612 | + |
| 613 | + touchAlpha = (pressed) ? .touched : .untouched |
598 | 614 | } |
599 | 615 | } |
| 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 | + } |
600 | 646 |
|
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 | + } |
603 | 653 | } |
604 | 654 | } |
0 commit comments