diff --git a/LGButton.podspec b/LGButton.podspec index 980dabd..13db1f5 100644 --- a/LGButton.podspec +++ b/LGButton.podspec @@ -1,6 +1,6 @@ Pod::Spec.new do |s| s.name = 'LGButton' - s.version = '1.0.2' + s.version = '1.0.3' s.summary = 'A fully customisable subclass of the native UIControl which allows you to create beautiful buttons without writing any line of code.' s.homepage = 'https://cocoapods.org/pods/LGButton' s.license = { :type => 'MIT', :file => 'LICENSE.md' } diff --git a/LGButton/Classes/LGButton.swift b/LGButton/Classes/LGButton.swift index 2c854cb..707c449 100644 --- a/LGButton/Classes/LGButton.swift +++ b/LGButton/Classes/LGButton.swift @@ -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! @@ -589,16 +598,57 @@ public class LGButton: UIControl { // MARK: - Touches // MARK: - override public func touchesBegan(_ touches: Set, 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, with event: UIEvent?){ + pressed = true + } + + override public func touchesEnded(_ touches: Set, with event: UIEvent?){ + let shouldSendActions = pressed + pressed = false + if shouldSendActions{ + sendActions(for: .touchUpInside) + } + } + + override public func touchesMoved(_ touches: Set, 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, with event: UIEvent?) { + pressed = false + } - override public func touchesEnded(_ touches: Set, with event: UIEvent?) { - sendActions(for: .touchUpInside) + func updateTouchAlpha() { + if self.alpha != self.touchAlpha.rawValue { + UIView.animate(withDuration: 0.3) { + self.alpha = self.touchAlpha.rawValue + } + } } } diff --git a/README.md b/README.md index e35418b..7ed6c9e 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,7 @@ ![logo](media/header_btn.png) -[![build](https://travis-ci.org/loregr/LGButton.svg?branch=master)](https://travis-ci.org/loregr/LGButton) ![platform](https://img.shields.io/badge/platform-ios-blue.svg) [![license](https://img.shields.io/badge/license-MIT-lightgrey.svg)](./LICENSE.md) [![Version](https://img.shields.io/cocoapods/v/LGButton.svg?style=flat)](http://cocoadocs.org/docsets/LGButton) +[![build](https://travis-ci.org/loregr/LGButton.svg?branch=master)](https://travis-ci.org/loregr/LGButton) ![platform](https://img.shields.io/badge/platform-ios-blue.svg) [![license](https://img.shields.io/badge/license-MIT-lightgrey.svg)](./LICENSE.md) [![Version](https://img.shields.io/cocoapods/v/LGButton.svg?style=flat)](http://cocoadocs.org/docsets/LGButton) [![gitcheese.com](https://s3.amazonaws.com/gitcheese-ui-master/images/badge.svg)](https://www.gitcheese.com/donate/users/7204248/repos/94624954) A fully customisable subclass of the native `UIControl` which allows you to create beautiful buttons without writing any line of code.