Skip to content

Commit

Permalink
Merge pull request #17 from gumbright/develop
Browse files Browse the repository at this point in the history
Remove touch "flash" and make it work more like a UIButton
  • Loading branch information
loregr authored Sep 23, 2017
2 parents 3e3c543 + 5449c36 commit bd19e79
Showing 1 changed file with 58 additions and 8 deletions.
66 changes: 58 additions & 8 deletions LGButton/Classes/LGButton.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,22 @@
import UIKit
import QuartzCore


@IBDesignable
public class LGButton: UIControl {

enum TouchAlphaValues : CGFloat {
case touched = 0.7
case untouched = 1.0
}

let touchDisableRadius : CGFloat = 100.0

let availableFontIcons = ["fa", "io", "oc", "ic", "ma", "ti", "mi"]

var gradient : CAGradientLayer?



fileprivate var rootView : UIView!
@IBOutlet fileprivate weak var titleLbl: UILabel!
@IBOutlet fileprivate weak var mainStackView: UIStackView!
Expand Down Expand Up @@ -589,16 +598,57 @@ public class LGButton: UIControl {

// MARK: - Touches
// MARK:
override public func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
if showTouchFeedback {
alpha = 0.7
UIView.animate(withDuration: 0.3) {
self.alpha = 1
var touchAlpha : TouchAlphaValues = .untouched {
didSet {
updateTouchAlpha()
}
}

var pressed : Bool = false {
didSet {
if !showTouchFeedback {
return
}

touchAlpha = (pressed) ? .touched : .untouched
}
}

override public func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?){
pressed = true
}

override public func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?){
let shouldSendActions = pressed
pressed = false
if shouldSendActions{
sendActions(for: .touchUpInside)
}
}

override public func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?){
if let touchLoc = touches.first?.location(in: self){
if (touchLoc.x < -touchDisableRadius ||
touchLoc.y < -touchDisableRadius ||
touchLoc.x > self.bounds.size.width + touchDisableRadius ||
touchLoc.y > self.bounds.size.height + touchDisableRadius){
pressed = false
}
else if self.touchAlpha == .untouched {
pressed = true
}
}
}

override public func touchesCancelled(_ touches: Set<UITouch>, with event: UIEvent?) {
pressed = false
}

override public func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
sendActions(for: .touchUpInside)
func updateTouchAlpha() {
if self.alpha != self.touchAlpha.rawValue {
UIView.animate(withDuration: 0.3) {
self.alpha = self.touchAlpha.rawValue
}
}
}
}

0 comments on commit bd19e79

Please sign in to comment.