From 8d742aac9f56cb109f1b3bf6917f1ab31bef5747 Mon Sep 17 00:00:00 2001 From: Ben Guild Date: Wed, 30 Aug 2017 16:03:01 +0900 Subject: [PATCH 01/22] Fixing resizability/rotation. --- MMSlidingButton/MMSlidingButton.swift | 47 +++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) diff --git a/MMSlidingButton/MMSlidingButton.swift b/MMSlidingButton/MMSlidingButton.swift index 1ba411b..ae68cc0 100644 --- a/MMSlidingButton/MMSlidingButton.swift +++ b/MMSlidingButton/MMSlidingButton.swift @@ -97,6 +97,47 @@ protocol SlideButtonDelegate{ self.setUpButton() self.layoutSet = true } + else { + let dragPointWidthDifference=(self.frame.size.width-self.dragPoint.frame.size.width); + + func fixWidths() { + for (_, view) in [self.dragPoint, self.dragPointButtonLabel].enumerated() { + view.frame.size.width=view.frame.size.width + dragPointWidthDifference; + + } + + } + + func fixOffsets() { + self.dragPoint.frame.origin.x=self.dragPoint.frame.origin.x - dragPointWidthDifference; + self.imageView.frame.origin.x=self.imageView.frame.origin.x + dragPointWidthDifference; + + } + //// + + let widthsBeforeOffsets=(dragPointWidthDifference>=0); + + if (widthsBeforeOffsets) { + fixWidths(); + + } else { + fixOffsets(); + + } + + DispatchQueue.main.async { + if (widthsBeforeOffsets) { + fixOffsets(); + + } else { + fixWidths(); + + } + + } + + } + } func setStyle(){ @@ -118,6 +159,8 @@ protocol SlideButtonDelegate{ self.backgroundColor = self.buttonColor self.dragPoint = UIView(frame: CGRect(x: dragPointWidth - self.frame.size.width, y: 0, width: self.frame.size.width, height: self.frame.size.height)) + self.dragPoint.autoresizingMask=[UIViewAutoresizing.flexibleHeight] + self.dragPoint.backgroundColor = dragPointColor self.dragPoint.layer.cornerRadius = buttonCornerRadius self.addSubview(self.dragPoint) @@ -125,6 +168,8 @@ protocol SlideButtonDelegate{ if !self.buttonText.isEmpty{ self.buttonLabel = UILabel(frame: CGRect(x: 0, y: 0, width: self.frame.size.width, height: self.frame.size.height)) + self.buttonLabel.autoresizingMask=[UIViewAutoresizing.flexibleWidth, UIViewAutoresizing.flexibleHeight] + self.buttonLabel.textAlignment = .center self.buttonLabel.text = buttonText self.buttonLabel.textColor = UIColor.white @@ -144,6 +189,8 @@ protocol SlideButtonDelegate{ if self.imageName != UIImage(){ self.imageView = UIImageView(frame: CGRect(x: self.frame.size.width - dragPointWidth, y: 0, width: self.dragPointWidth, height: self.frame.size.height)) + self.imageView.autoresizingMask=[UIViewAutoresizing.flexibleHeight] + self.imageView.contentMode = .center self.imageView.image = self.imageName self.dragPoint.addSubview(self.imageView) From 5f6698c791f41a07a149a20d2caf05a924434900 Mon Sep 17 00:00:00 2001 From: Ben Guild Date: Wed, 30 Aug 2017 16:03:14 +0900 Subject: [PATCH 02/22] Adding Objective-C compatibility. --- MMSlidingButton/MMSlidingButton.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/MMSlidingButton/MMSlidingButton.swift b/MMSlidingButton/MMSlidingButton.swift index ae68cc0..1e92e88 100644 --- a/MMSlidingButton/MMSlidingButton.swift +++ b/MMSlidingButton/MMSlidingButton.swift @@ -13,7 +13,7 @@ protocol SlideButtonDelegate{ func buttonStatus(status:String, sender:MMSlidingButton) } -@IBDesignable class MMSlidingButton: UIView{ +@objc @IBDesignable class MMSlidingButton: UIView{ var delegate: SlideButtonDelegate? From 148d301dbc6247d2364d56463c8053a2ed247189 Mon Sep 17 00:00:00 2001 From: Ben Guild Date: Wed, 30 Aug 2017 16:20:29 +0900 Subject: [PATCH 03/22] Cancelling pan gestures on resize/rotation. --- MMSlidingButton/MMSlidingButton.swift | 25 +++++++++++++++++++++---- 1 file changed, 21 insertions(+), 4 deletions(-) diff --git a/MMSlidingButton/MMSlidingButton.swift b/MMSlidingButton/MMSlidingButton.swift index 1e92e88..8412d4c 100644 --- a/MMSlidingButton/MMSlidingButton.swift +++ b/MMSlidingButton/MMSlidingButton.swift @@ -83,6 +83,8 @@ protocol SlideButtonDelegate{ var unlocked = false var layoutSet = false + private var panGestureRecognizer : UIPanGestureRecognizer? + override init (frame : CGRect) { super.init(frame : frame) } @@ -100,6 +102,16 @@ protocol SlideButtonDelegate{ else { let dragPointWidthDifference=(self.frame.size.width-self.dragPoint.frame.size.width); + if (panGestureRecognizer?.state == UIGestureRecognizerState.began || panGestureRecognizer?.state == UIGestureRecognizerState.changed) { + panGestureRecognizer?.isEnabled=false // NOTE: This is to cancel any current gesture during a rotation/resize. + panGestureRecognizer?.isEnabled=true + //// + + self.dragPoint.frame.origin.x=dragPointDefaultOriginX() + dragPointWidthDifference; + + } + //// + func fixWidths() { for (_, view) in [self.dragPoint, self.dragPointButtonLabel].enumerated() { view.frame.size.width=view.frame.size.width + dragPointWidthDifference; @@ -154,11 +166,16 @@ protocol SlideButtonDelegate{ self.layer.cornerRadius = buttonCornerRadius } + func dragPointDefaultOriginX() -> CGFloat { + return dragPointWidth - self.frame.size.width; + + } + func setUpButton(){ self.backgroundColor = self.buttonColor - self.dragPoint = UIView(frame: CGRect(x: dragPointWidth - self.frame.size.width, y: 0, width: self.frame.size.width, height: self.frame.size.height)) + self.dragPoint = UIView(frame: CGRect(x: dragPointDefaultOriginX(), y: 0, width: self.frame.size.width, height: self.frame.size.height)) self.dragPoint.autoresizingMask=[UIViewAutoresizing.flexibleHeight] self.dragPoint.backgroundColor = dragPointColor @@ -199,9 +216,9 @@ protocol SlideButtonDelegate{ self.layer.masksToBounds = true // start detecting pan gesture - let panGestureRecognizer = UIPanGestureRecognizer(target: self, action: #selector(self.panDetected(sender:))) - panGestureRecognizer.minimumNumberOfTouches = 1 - self.dragPoint.addGestureRecognizer(panGestureRecognizer) + panGestureRecognizer=UIPanGestureRecognizer(target: self, action: #selector(self.panDetected(sender:))) + panGestureRecognizer!.minimumNumberOfTouches = 1 + self.dragPoint.addGestureRecognizer(panGestureRecognizer!) } func panDetected(sender: UIPanGestureRecognizer){ From e7d4ba7094296ce81efc2aad1be0b47d847e6254 Mon Sep 17 00:00:00 2001 From: Ben Guild Date: Wed, 30 Aug 2017 16:29:40 +0900 Subject: [PATCH 04/22] Guarding against negative swipes/slides. --- MMSlidingButton/MMSlidingButton.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/MMSlidingButton/MMSlidingButton.swift b/MMSlidingButton/MMSlidingButton.swift index 8412d4c..1d8c01b 100644 --- a/MMSlidingButton/MMSlidingButton.swift +++ b/MMSlidingButton/MMSlidingButton.swift @@ -224,7 +224,7 @@ protocol SlideButtonDelegate{ func panDetected(sender: UIPanGestureRecognizer){ var translatedPoint = sender.translation(in: self) translatedPoint = CGPoint(x: translatedPoint.x, y: self.frame.size.height / 2) - sender.view?.frame.origin.x = (dragPointWidth - self.frame.size.width) + translatedPoint.x + sender.view?.frame.origin.x = max(dragPointWidth - self.frame.size.width, (dragPointWidth - self.frame.size.width) + translatedPoint.x); if sender.state == .ended{ let velocityX = sender.velocity(in: self).x * 0.2 From 6c82aa40105991f9dd312f2b87b8d17b597d4306 Mon Sep 17 00:00:00 2001 From: Ben Guild Date: Wed, 30 Aug 2017 16:31:09 +0900 Subject: [PATCH 05/22] Guarding against excessive (>100%) swipes/slides. --- MMSlidingButton/MMSlidingButton.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/MMSlidingButton/MMSlidingButton.swift b/MMSlidingButton/MMSlidingButton.swift index 1d8c01b..230fca6 100644 --- a/MMSlidingButton/MMSlidingButton.swift +++ b/MMSlidingButton/MMSlidingButton.swift @@ -224,7 +224,7 @@ protocol SlideButtonDelegate{ func panDetected(sender: UIPanGestureRecognizer){ var translatedPoint = sender.translation(in: self) translatedPoint = CGPoint(x: translatedPoint.x, y: self.frame.size.height / 2) - sender.view?.frame.origin.x = max(dragPointWidth - self.frame.size.width, (dragPointWidth - self.frame.size.width) + translatedPoint.x); + sender.view?.frame.origin.x = min(0, max(dragPointWidth - self.frame.size.width, (dragPointWidth - self.frame.size.width) + translatedPoint.x)); if sender.state == .ended{ let velocityX = sender.velocity(in: self).x * 0.2 From a6597623665bb2ccc00021fb1c4dba0c58ec5150 Mon Sep 17 00:00:00 2001 From: Ben Guild Date: Wed, 30 Aug 2017 16:38:54 +0900 Subject: [PATCH 06/22] Adding Objective-C compatibility to delegate protocol. --- MMSlidingButton/MMSlidingButton.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/MMSlidingButton/MMSlidingButton.swift b/MMSlidingButton/MMSlidingButton.swift index 230fca6..bdd3320 100644 --- a/MMSlidingButton/MMSlidingButton.swift +++ b/MMSlidingButton/MMSlidingButton.swift @@ -9,7 +9,7 @@ import Foundation import UIKit -protocol SlideButtonDelegate{ +@objc protocol SlideButtonDelegate{ func buttonStatus(status:String, sender:MMSlidingButton) } From 6dc66e2a649706bb5f990d1464da78f93081feab Mon Sep 17 00:00:00 2001 From: Ben Guild Date: Wed, 30 Aug 2017 16:52:33 +0900 Subject: [PATCH 07/22] Cleaning up delegate with "unlocked()" method based on implementation. --- MMSlidingButton/MMSlidingButton.swift | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/MMSlidingButton/MMSlidingButton.swift b/MMSlidingButton/MMSlidingButton.swift index bdd3320..5a54c50 100644 --- a/MMSlidingButton/MMSlidingButton.swift +++ b/MMSlidingButton/MMSlidingButton.swift @@ -10,7 +10,11 @@ import Foundation import UIKit @objc protocol SlideButtonDelegate{ + @available(*, unavailable, message: "Update to \"unlocked()\" delegate method") func buttonStatus(status:String, sender:MMSlidingButton) + + func unlocked(slidingButton: MMSlidingButton) + } @objc @IBDesignable class MMSlidingButton: UIView{ @@ -262,7 +266,7 @@ import UIKit self.imageView.isHidden = true self.dragPoint.backgroundColor = self.buttonUnlockedColor self.dragPointButtonLabel.textColor = self.buttonUnlockedTextColor - self.delegate?.buttonStatus(status: "Unlocked", sender: self) + self.delegate?.unlocked(slidingButton: self) } } } @@ -278,7 +282,6 @@ import UIKit self.dragPoint.backgroundColor = self.dragPointColor self.dragPointButtonLabel.textColor = self.dragPointTextColor self.unlocked = false - //self.delegate?.buttonStatus("Locked") } } } From f93f3e11f04bc7da8e7e09e84969703e3eeae39a Mon Sep 17 00:00:00 2001 From: Ben Guild Date: Wed, 30 Aug 2017 17:20:04 +0900 Subject: [PATCH 08/22] Adding option for "offsetButtonTextByDragPointWidth". --- MMSlidingButton/MMSlidingButton.swift | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/MMSlidingButton/MMSlidingButton.swift b/MMSlidingButton/MMSlidingButton.swift index 5a54c50..499f1f9 100644 --- a/MMSlidingButton/MMSlidingButton.swift +++ b/MMSlidingButton/MMSlidingButton.swift @@ -45,6 +45,12 @@ import UIKit } } + @IBInspectable var offsetButtonTextByDragPointWidth: Bool = false { + didSet{ + setStyle() + } + } + @IBInspectable var imageName: UIImage = UIImage() { didSet{ setStyle() @@ -187,8 +193,16 @@ import UIKit self.addSubview(self.dragPoint) if !self.buttonText.isEmpty{ + var dragPointX : CGFloat = 0; + + if offsetButtonTextByDragPointWidth + { + dragPointX=dragPointWidth; + + } + //// - self.buttonLabel = UILabel(frame: CGRect(x: 0, y: 0, width: self.frame.size.width, height: self.frame.size.height)) + self.buttonLabel = UILabel(frame: CGRect(x: dragPointX, y: 0, width: self.frame.size.width - dragPointWidth, height: self.frame.size.height)) self.buttonLabel.autoresizingMask=[UIViewAutoresizing.flexibleWidth, UIViewAutoresizing.flexibleHeight] self.buttonLabel.textAlignment = .center From 8ed3d3fd84ae18e7d4dd9e51ef2c9d0aa2d530b6 Mon Sep 17 00:00:00 2001 From: Ben Guild Date: Wed, 30 Aug 2017 19:47:56 +0900 Subject: [PATCH 09/22] Fixing retain cycle in original code, yikes. --- MMSlidingButton/MMSlidingButton.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/MMSlidingButton/MMSlidingButton.swift b/MMSlidingButton/MMSlidingButton.swift index 499f1f9..a775e28 100644 --- a/MMSlidingButton/MMSlidingButton.swift +++ b/MMSlidingButton/MMSlidingButton.swift @@ -19,7 +19,7 @@ import UIKit @objc @IBDesignable class MMSlidingButton: UIView{ - var delegate: SlideButtonDelegate? + weak var delegate: SlideButtonDelegate? @IBInspectable var dragPointWidth: CGFloat = 70 { didSet{ From e3cae1bb092bcda436d7b743b1927c1071967b83 Mon Sep 17 00:00:00 2001 From: Ben Guild Date: Thu, 31 Aug 2017 14:52:16 +0900 Subject: [PATCH 10/22] Adding the ability to optionally/separately define the "unlocking" text that appears while swiping. Falls back on primary value if empty. --- MMSlidingButton/MMSlidingButton.swift | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/MMSlidingButton/MMSlidingButton.swift b/MMSlidingButton/MMSlidingButton.swift index a775e28..4fe3ba2 100644 --- a/MMSlidingButton/MMSlidingButton.swift +++ b/MMSlidingButton/MMSlidingButton.swift @@ -85,6 +85,8 @@ import UIKit @IBInspectable var buttonUnlockedColor: UIColor = UIColor.black var buttonFont = UIFont.boldSystemFont(ofSize: 17) + @IBInspectable var optionalButtonUnlockingText: String = "" + var dragPoint = UIView() var buttonLabel = UILabel() @@ -162,9 +164,20 @@ import UIKit } + private func dragPointButtonText() -> String { + if (optionalButtonUnlockingText.count > 0) { + return optionalButtonUnlockingText + + } else { + return buttonText + + } + + } + func setStyle(){ self.buttonLabel.text = self.buttonText - self.dragPointButtonLabel.text = self.buttonText + self.dragPointButtonLabel.text = dragPointButtonText() self.dragPoint.frame.size.width = self.dragPointWidth self.dragPoint.backgroundColor = self.dragPointColor self.backgroundColor = self.buttonColor @@ -214,7 +227,7 @@ import UIKit self.dragPointButtonLabel = UILabel(frame: CGRect(x: 0, y: 0, width: self.frame.size.width, height: self.frame.size.height)) self.dragPointButtonLabel.textAlignment = .center - self.dragPointButtonLabel.text = buttonText + self.dragPointButtonLabel.text = dragPointButtonText() self.dragPointButtonLabel.textColor = UIColor.white self.dragPointButtonLabel.font = self.buttonFont self.dragPointButtonLabel.textColor = self.dragPointTextColor @@ -291,7 +304,7 @@ import UIKit self.dragPoint.frame = CGRect(x: self.dragPointWidth - self.frame.size.width, y: 0, width: self.dragPoint.frame.size.width, height: self.dragPoint.frame.size.height) }) { (Status) in if Status{ - self.dragPointButtonLabel.text = self.buttonText + self.dragPointButtonLabel.text = self.dragPointButtonText() self.imageView.isHidden = false self.dragPoint.backgroundColor = self.dragPointColor self.dragPointButtonLabel.textColor = self.dragPointTextColor From 7f74fcd0ae9e0e2e600a054783bb8b471b3b63f7 Mon Sep 17 00:00:00 2001 From: Ben Guild Date: Thu, 31 Aug 2017 15:31:33 +0900 Subject: [PATCH 11/22] Adding debounced delegate calls for unlock threshold crossing. Some code cleanup. --- MMSlidingButton/MMSlidingButton.swift | 79 +++++++++++++++++++++------ 1 file changed, 63 insertions(+), 16 deletions(-) diff --git a/MMSlidingButton/MMSlidingButton.swift b/MMSlidingButton/MMSlidingButton.swift index 4fe3ba2..415a11b 100644 --- a/MMSlidingButton/MMSlidingButton.swift +++ b/MMSlidingButton/MMSlidingButton.swift @@ -15,6 +15,9 @@ import UIKit func unlocked(slidingButton: MMSlidingButton) + @objc optional func didEnterUnlockRegion(slidingButton: MMSlidingButton) + @objc optional func didExitUnlockRegion(slidingButton: MMSlidingButton) + } @objc @IBDesignable class MMSlidingButton: UIView{ @@ -87,13 +90,15 @@ import UIKit @IBInspectable var optionalButtonUnlockingText: String = "" + private(set) var dragPoint = UIView() + private(set) var buttonLabel = UILabel() + private(set) var dragPointButtonLabel = UILabel() + private(set) var imageView = UIImageView() + private(set) var unlocked = false + private var layoutSet = false - var dragPoint = UIView() - var buttonLabel = UILabel() - var dragPointButtonLabel = UILabel() - var imageView = UIImageView() - var unlocked = false - var layoutSet = false + private var isInsideUnlockRegion = false + private var dispatchCounterFor_isInsideUnlockRegionDidChange : UInt16 = 0 private var panGestureRecognizer : UIPanGestureRecognizer? @@ -256,24 +261,35 @@ import UIKit var translatedPoint = sender.translation(in: self) translatedPoint = CGPoint(x: translatedPoint.x, y: self.frame.size.height / 2) sender.view?.frame.origin.x = min(0, max(dragPointWidth - self.frame.size.width, (dragPointWidth - self.frame.size.width) + translatedPoint.x)); - if sender.state == .ended{ + //// + + let velocityX=(sender.velocity(in: self).x * 0.2) + + let wasInsideUnlockRegion=isInsideUnlockRegion + isInsideUnlockRegion=(((translatedPoint.x + velocityX) + self.dragPointWidth) > (self.frame.size.width - 60)) + + if (wasInsideUnlockRegion != isInsideUnlockRegion) { + isInsideUnlockRegionDidChange(newValue: isInsideUnlockRegion) - let velocityX = sender.velocity(in: self).x * 0.2 - var finalX = translatedPoint.x + velocityX - if finalX < 0{ - finalX = 0 - }else if finalX + self.dragPointWidth > (self.frame.size.width - 60){ + } + //// + + if (sender.state == .ended) { + if (isInsideUnlockRegion) { unlocked = true self.unlock() + } + //// - let animationDuration:Double = abs(Double(velocityX) * 0.0002) + 0.2 - UIView.transition(with: self, duration: animationDuration, options: UIViewAnimationOptions.curveEaseOut, animations: { + UIView.transition(with: self, duration: abs(Double(velocityX) * 0.0002) + 0.2, options: UIViewAnimationOptions.curveEaseOut, animations: { }, completion: { (Status) in - if Status{ + if Status { self.animationFinished() } - }) + + } ) + } } @@ -283,6 +299,29 @@ import UIKit } } + private func isInsideUnlockRegionDidChange(newValue : Bool) { + dispatchCounterFor_isInsideUnlockRegionDidChange += 1 + let localCopyOf_dispatchCounterFor_isInsideUnlockRegionDidChange = dispatchCounterFor_isInsideUnlockRegionDidChange + + DispatchQueue.main.asyncAfter(deadline: .now() + 0.15) { // NOTE: This is meant to "debounce" the slide threshold. + if (localCopyOf_dispatchCounterFor_isInsideUnlockRegionDidChange != self.dispatchCounterFor_isInsideUnlockRegionDidChange) { + return + + } + //// + + if (newValue) { + self.delegate?.didEnterUnlockRegion?(slidingButton: self) + + } else { + self.delegate?.didExitUnlockRegion?(slidingButton: self) + + } + + } + + } + //lock button animation (SUCCESS) func unlock(){ UIView.transition(with: self, duration: 0.2, options: .curveEaseOut, animations: { @@ -300,6 +339,14 @@ import UIKit //reset button animation (RESET) func reset(){ + if (isInsideUnlockRegion) { + isInsideUnlockRegion=false + //// + + isInsideUnlockRegionDidChange(newValue: false); + + } + UIView.transition(with: self, duration: 0.2, options: .curveEaseOut, animations: { self.dragPoint.frame = CGRect(x: self.dragPointWidth - self.frame.size.width, y: 0, width: self.dragPoint.frame.size.width, height: self.dragPoint.frame.size.height) }) { (Status) in From 2c545f1113b0afcf5155d032b8a65b49105e2a5f Mon Sep 17 00:00:00 2001 From: Ben Guild Date: Thu, 31 Aug 2017 15:36:29 +0900 Subject: [PATCH 12/22] Ensuring there's no collision between drag point and text. --- MMSlidingButton/MMSlidingButton.swift | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/MMSlidingButton/MMSlidingButton.swift b/MMSlidingButton/MMSlidingButton.swift index 415a11b..c892e09 100644 --- a/MMSlidingButton/MMSlidingButton.swift +++ b/MMSlidingButton/MMSlidingButton.swift @@ -230,8 +230,7 @@ import UIKit self.buttonLabel.textColor = self.buttonTextColor self.addSubview(self.buttonLabel) - self.dragPointButtonLabel = UILabel(frame: CGRect(x: 0, y: 0, width: self.frame.size.width, height: self.frame.size.height)) - self.dragPointButtonLabel.textAlignment = .center + self.dragPointButtonLabel = UILabel(frame: CGRect(x: dragPointWidth, y: 0, width: self.frame.size.width - (dragPointWidth * 2), height: self.frame.size.height)) self.dragPointButtonLabel.text = dragPointButtonText() self.dragPointButtonLabel.textColor = UIColor.white self.dragPointButtonLabel.font = self.buttonFont From e84af04b2f987dbb9ddf67bdc43a62df7ace10e5 Mon Sep 17 00:00:00 2001 From: Ben Guild Date: Thu, 31 Aug 2017 15:43:19 +0900 Subject: [PATCH 13/22] Adding optional inspectable "alignDragPointTextRight" property. --- MMSlidingButton/MMSlidingButton.swift | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/MMSlidingButton/MMSlidingButton.swift b/MMSlidingButton/MMSlidingButton.swift index c892e09..038e2db 100644 --- a/MMSlidingButton/MMSlidingButton.swift +++ b/MMSlidingButton/MMSlidingButton.swift @@ -71,6 +71,11 @@ import UIKit setStyle() } } + @IBInspectable var alignDragPointTextRight: Bool = false { + didSet{ + setStyle() + } + } @IBInspectable var buttonUnlockedTextColor: UIColor = UIColor.white { didSet{ @@ -220,6 +225,14 @@ import UIKit } //// + var dragPointButtonLabelTextAlignment = NSTextAlignment.center + + if (alignDragPointTextRight) { + dragPointButtonLabelTextAlignment = .right + + } + //// + self.buttonLabel = UILabel(frame: CGRect(x: dragPointX, y: 0, width: self.frame.size.width - dragPointWidth, height: self.frame.size.height)) self.buttonLabel.autoresizingMask=[UIViewAutoresizing.flexibleWidth, UIViewAutoresizing.flexibleHeight] @@ -231,6 +244,7 @@ import UIKit self.addSubview(self.buttonLabel) self.dragPointButtonLabel = UILabel(frame: CGRect(x: dragPointWidth, y: 0, width: self.frame.size.width - (dragPointWidth * 2), height: self.frame.size.height)) + self.dragPointButtonLabel.textAlignment = dragPointButtonLabelTextAlignment self.dragPointButtonLabel.text = dragPointButtonText() self.dragPointButtonLabel.textColor = UIColor.white self.dragPointButtonLabel.font = self.buttonFont @@ -330,6 +344,7 @@ import UIKit self.dragPointButtonLabel.text = self.buttonUnlockedText self.imageView.isHidden = true self.dragPoint.backgroundColor = self.buttonUnlockedColor + self.dragPointButtonLabel.textAlignment = .center // NOTE: Ensures this is reset if "alignDragPointTextRight" is `true` self.dragPointButtonLabel.textColor = self.buttonUnlockedTextColor self.delegate?.unlocked(slidingButton: self) } From ffb1c43e1326eb6c3a93478976d405b87328308a Mon Sep 17 00:00:00 2001 From: Ben Guild Date: Thu, 31 Aug 2017 16:19:39 +0900 Subject: [PATCH 14/22] Assorted small fixes/tweaks. --- MMSlidingButton/MMSlidingButton.swift | 32 ++++++++++++++++++++------- 1 file changed, 24 insertions(+), 8 deletions(-) diff --git a/MMSlidingButton/MMSlidingButton.swift b/MMSlidingButton/MMSlidingButton.swift index 038e2db..a29f40b 100644 --- a/MMSlidingButton/MMSlidingButton.swift +++ b/MMSlidingButton/MMSlidingButton.swift @@ -227,7 +227,7 @@ import UIKit var dragPointButtonLabelTextAlignment = NSTextAlignment.center - if (alignDragPointTextRight) { + if (!unlocked && alignDragPointTextRight) { dragPointButtonLabelTextAlignment = .right } @@ -276,20 +276,26 @@ import UIKit sender.view?.frame.origin.x = min(0, max(dragPointWidth - self.frame.size.width, (dragPointWidth - self.frame.size.width) + translatedPoint.x)); //// - let velocityX=(sender.velocity(in: self).x * 0.2) - let wasInsideUnlockRegion=isInsideUnlockRegion - isInsideUnlockRegion=(((translatedPoint.x + velocityX) + self.dragPointWidth) > (self.frame.size.width - 60)) + let velocityX=(sender.velocity(in: self).x * 0.2) - if (wasInsideUnlockRegion != isInsideUnlockRegion) { - isInsideUnlockRegionDidChange(newValue: isInsideUnlockRegion) + func didCrossThresholdPointX(includingVelocity: Bool) -> Bool { + var velocityXToUse=velocityX; + + if (!includingVelocity) { + velocityXToUse=0 + + } + + return (((translatedPoint.x + velocityXToUse) + self.dragPointWidth) > (self.frame.size.width - 60)); } - //// if (sender.state == .ended) { + isInsideUnlockRegion=didCrossThresholdPointX(includingVelocity: true) + if (isInsideUnlockRegion) { - unlocked = true + unlocked=true self.unlock() } @@ -303,7 +309,17 @@ import UIKit } ) + } else { + isInsideUnlockRegion=didCrossThresholdPointX(includingVelocity: false) + + } + //// + + if (wasInsideUnlockRegion != isInsideUnlockRegion) { + isInsideUnlockRegionDidChange(newValue: isInsideUnlockRegion) + } + } func animationFinished(){ From 4e84b60e3e4eae98f55438de0dc52bd6f4161bf1 Mon Sep 17 00:00:00 2001 From: Ben Guild Date: Sun, 10 Sep 2017 18:41:24 +0900 Subject: [PATCH 15/22] Updating Objective-C compatibility for Swift 4. --- MMSlidingButton/MMSlidingButton.swift | 58 +++++++++++++-------------- 1 file changed, 29 insertions(+), 29 deletions(-) diff --git a/MMSlidingButton/MMSlidingButton.swift b/MMSlidingButton/MMSlidingButton.swift index a29f40b..65ade81 100644 --- a/MMSlidingButton/MMSlidingButton.swift +++ b/MMSlidingButton/MMSlidingButton.swift @@ -20,87 +20,87 @@ import UIKit } -@objc @IBDesignable class MMSlidingButton: UIView{ +@IBDesignable class MMSlidingButton: UIView{ - weak var delegate: SlideButtonDelegate? + @objc weak var delegate: SlideButtonDelegate? - @IBInspectable var dragPointWidth: CGFloat = 70 { + @objc @IBInspectable var dragPointWidth: CGFloat = 70 { didSet{ setStyle() } } - @IBInspectable var dragPointColor: UIColor = UIColor.darkGray { + @objc @IBInspectable var dragPointColor: UIColor = UIColor.darkGray { didSet{ setStyle() } } - @IBInspectable var buttonColor: UIColor = UIColor.gray { + @objc @IBInspectable var buttonColor: UIColor = UIColor.gray { didSet{ setStyle() } } - @IBInspectable var buttonText: String = "UNLOCK" { + @objc @IBInspectable var buttonText: String = "UNLOCK" { didSet{ setStyle() } } - @IBInspectable var offsetButtonTextByDragPointWidth: Bool = false { + @objc @IBInspectable var offsetButtonTextByDragPointWidth: Bool = false { didSet{ setStyle() } } - @IBInspectable var imageName: UIImage = UIImage() { + @objc @IBInspectable var imageName: UIImage = UIImage() { didSet{ setStyle() } } - @IBInspectable var buttonTextColor: UIColor = UIColor.white { + @objc @IBInspectable var buttonTextColor: UIColor = UIColor.white { didSet{ setStyle() } } - @IBInspectable var dragPointTextColor: UIColor = UIColor.white { + @objc @IBInspectable var dragPointTextColor: UIColor = UIColor.white { didSet{ setStyle() } } - @IBInspectable var alignDragPointTextRight: Bool = false { + @objc @IBInspectable var alignDragPointTextRight: Bool = false { didSet{ setStyle() } } - @IBInspectable var buttonUnlockedTextColor: UIColor = UIColor.white { + @objc @IBInspectable var buttonUnlockedTextColor: UIColor = UIColor.white { didSet{ setStyle() } } - @IBInspectable var buttonCornerRadius: CGFloat = 30 { + @objc @IBInspectable var buttonCornerRadius: CGFloat = 30 { didSet{ setStyle() } } - @IBInspectable var buttonUnlockedText: String = "UNLOCKED" - @IBInspectable var buttonUnlockedColor: UIColor = UIColor.black - var buttonFont = UIFont.boldSystemFont(ofSize: 17) + @objc @IBInspectable var buttonUnlockedText: String = "UNLOCKED" + @objc @IBInspectable var buttonUnlockedColor: UIColor = UIColor.black + @objc var buttonFont = UIFont.boldSystemFont(ofSize: 17) - @IBInspectable var optionalButtonUnlockingText: String = "" + @objc @IBInspectable var optionalButtonUnlockingText: String = "" - private(set) var dragPoint = UIView() - private(set) var buttonLabel = UILabel() - private(set) var dragPointButtonLabel = UILabel() - private(set) var imageView = UIImageView() - private(set) var unlocked = false - private var layoutSet = false + @objc private(set) var dragPoint = UIView() + @objc private(set) var buttonLabel = UILabel() + @objc private(set) var dragPointButtonLabel = UILabel() + @objc private(set) var imageView = UIImageView() + @objc private(set) var unlocked = false + @objc private var layoutSet = false private var isInsideUnlockRegion = false private var dispatchCounterFor_isInsideUnlockRegionDidChange : UInt16 = 0 @@ -199,12 +199,12 @@ import UIKit self.layer.cornerRadius = buttonCornerRadius } - func dragPointDefaultOriginX() -> CGFloat { + fileprivate func dragPointDefaultOriginX() -> CGFloat { return dragPointWidth - self.frame.size.width; } - func setUpButton(){ + fileprivate func setUpButton(){ self.backgroundColor = self.buttonColor @@ -270,7 +270,7 @@ import UIKit self.dragPoint.addGestureRecognizer(panGestureRecognizer!) } - func panDetected(sender: UIPanGestureRecognizer){ + @objc func panDetected(sender: UIPanGestureRecognizer){ var translatedPoint = sender.translation(in: self) translatedPoint = CGPoint(x: translatedPoint.x, y: self.frame.size.height / 2) sender.view?.frame.origin.x = min(0, max(dragPointWidth - self.frame.size.width, (dragPointWidth - self.frame.size.width) + translatedPoint.x)); @@ -322,13 +322,13 @@ import UIKit } - func animationFinished(){ + fileprivate func animationFinished(){ if !unlocked{ self.reset() } } - private func isInsideUnlockRegionDidChange(newValue : Bool) { + fileprivate func isInsideUnlockRegionDidChange(newValue : Bool) { dispatchCounterFor_isInsideUnlockRegionDidChange += 1 let localCopyOf_dispatchCounterFor_isInsideUnlockRegionDidChange = dispatchCounterFor_isInsideUnlockRegionDidChange @@ -368,7 +368,7 @@ import UIKit } //reset button animation (RESET) - func reset(){ + @objc func reset(){ if (isInsideUnlockRegion) { isInsideUnlockRegion=false //// From f9545c530e7d1bbd7fd5f0a43c4e18ce20b488ce Mon Sep 17 00:00:00 2001 From: Ben Guild Date: Fri, 15 Sep 2017 17:05:19 +0900 Subject: [PATCH 16/22] Allowing font scaling to ensure that localizations don't break. --- MMSlidingButton/MMSlidingButton.swift | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/MMSlidingButton/MMSlidingButton.swift b/MMSlidingButton/MMSlidingButton.swift index 65ade81..5b0f56d 100644 --- a/MMSlidingButton/MMSlidingButton.swift +++ b/MMSlidingButton/MMSlidingButton.swift @@ -236,6 +236,8 @@ import UIKit self.buttonLabel = UILabel(frame: CGRect(x: dragPointX, y: 0, width: self.frame.size.width - dragPointWidth, height: self.frame.size.height)) self.buttonLabel.autoresizingMask=[UIViewAutoresizing.flexibleWidth, UIViewAutoresizing.flexibleHeight] + self.buttonLabel.adjustsFontSizeToFitWidth=true + self.buttonLabel.minimumScaleFactor=0.6 self.buttonLabel.textAlignment = .center self.buttonLabel.text = buttonText self.buttonLabel.textColor = UIColor.white @@ -244,6 +246,9 @@ import UIKit self.addSubview(self.buttonLabel) self.dragPointButtonLabel = UILabel(frame: CGRect(x: dragPointWidth, y: 0, width: self.frame.size.width - (dragPointWidth * 2), height: self.frame.size.height)) + + self.dragPointButtonLabel.adjustsFontSizeToFitWidth=true + self.dragPointButtonLabel.minimumScaleFactor=0.6 self.dragPointButtonLabel.textAlignment = dragPointButtonLabelTextAlignment self.dragPointButtonLabel.text = dragPointButtonText() self.dragPointButtonLabel.textColor = UIColor.white From c01f92d45da1b384d39618f3640a58dfb6e58724 Mon Sep 17 00:00:00 2001 From: Ben Guild Date: Fri, 15 Sep 2017 17:13:31 +0900 Subject: [PATCH 17/22] Fixing "alignDragPointTextRight" behavior consistency after "reset()" method call. --- MMSlidingButton/MMSlidingButton.swift | 25 ++++++++++++++++--------- 1 file changed, 16 insertions(+), 9 deletions(-) diff --git a/MMSlidingButton/MMSlidingButton.swift b/MMSlidingButton/MMSlidingButton.swift index 5b0f56d..1976259 100644 --- a/MMSlidingButton/MMSlidingButton.swift +++ b/MMSlidingButton/MMSlidingButton.swift @@ -204,6 +204,18 @@ import UIKit } + fileprivate func dragPointButtonLabelTextAlignment() -> NSTextAlignment { + var textAlignment = NSTextAlignment.center + + if (!unlocked && alignDragPointTextRight) { + textAlignment = .right + + } + + return textAlignment + + } + fileprivate func setUpButton(){ self.backgroundColor = self.buttonColor @@ -225,14 +237,6 @@ import UIKit } //// - var dragPointButtonLabelTextAlignment = NSTextAlignment.center - - if (!unlocked && alignDragPointTextRight) { - dragPointButtonLabelTextAlignment = .right - - } - //// - self.buttonLabel = UILabel(frame: CGRect(x: dragPointX, y: 0, width: self.frame.size.width - dragPointWidth, height: self.frame.size.height)) self.buttonLabel.autoresizingMask=[UIViewAutoresizing.flexibleWidth, UIViewAutoresizing.flexibleHeight] @@ -249,7 +253,7 @@ import UIKit self.dragPointButtonLabel.adjustsFontSizeToFitWidth=true self.dragPointButtonLabel.minimumScaleFactor=0.6 - self.dragPointButtonLabel.textAlignment = dragPointButtonLabelTextAlignment + self.dragPointButtonLabel.textAlignment = dragPointButtonLabelTextAlignment() self.dragPointButtonLabel.text = dragPointButtonText() self.dragPointButtonLabel.textColor = UIColor.white self.dragPointButtonLabel.font = self.buttonFont @@ -391,6 +395,9 @@ import UIKit self.dragPoint.backgroundColor = self.dragPointColor self.dragPointButtonLabel.textColor = self.dragPointTextColor self.unlocked = false + //// + + self.dragPointButtonLabel.textAlignment = self.dragPointButtonLabelTextAlignment() // NOTE: Ensures this is reset if "alignDragPointTextRight" is `true`, but must be done after "unlocked" is reset to `false`. } } } From 97e8ec83c345227f13bac1971a24bfef54e20a1e Mon Sep 17 00:00:00 2001 From: Ben Guild Date: Fri, 15 Sep 2017 17:43:09 +0900 Subject: [PATCH 18/22] Adding support for optional "buttonAttributedText" parameter. Code cleanup. --- MMSlidingButton/MMSlidingButton.swift | 49 ++++++++++++++++++++++++--- 1 file changed, 44 insertions(+), 5 deletions(-) diff --git a/MMSlidingButton/MMSlidingButton.swift b/MMSlidingButton/MMSlidingButton.swift index 1976259..f3e3197 100644 --- a/MMSlidingButton/MMSlidingButton.swift +++ b/MMSlidingButton/MMSlidingButton.swift @@ -47,6 +47,33 @@ import UIKit setStyle() } } + @objc @IBInspectable var buttonAttributedText: NSAttributedString? = nil { + didSet{ + if (buttonAttributedText != nil) + { + let buttonText_new=buttonAttributedText!.string + //// + + if (buttonText_new.count==0) { + buttonAttributedText=nil + + } else if (buttonText != buttonText_new) { + buttonText=buttonText_new + + } else { + setStyle() + + } + + } + else + { + buttonText="" + + } + + } + } @objc @IBInspectable var offsetButtonTextByDragPointWidth: Bool = false { didSet{ @@ -185,14 +212,28 @@ import UIKit } + private func configureButtonLabel() { + self.buttonLabel.textColor = self.buttonTextColor + + if (self.buttonAttributedText != nil) { + self.buttonLabel.attributedText = self.buttonAttributedText + + } else { + self.buttonLabel.text = self.buttonText + + } + + } + func setStyle(){ - self.buttonLabel.text = self.buttonText + configureButtonLabel() + //// + self.dragPointButtonLabel.text = dragPointButtonText() self.dragPoint.frame.size.width = self.dragPointWidth self.dragPoint.backgroundColor = self.dragPointColor self.backgroundColor = self.buttonColor self.imageView.image = imageName - self.buttonLabel.textColor = self.buttonTextColor self.dragPointButtonLabel.textColor = self.dragPointTextColor self.dragPoint.layer.cornerRadius = buttonCornerRadius @@ -243,10 +284,8 @@ import UIKit self.buttonLabel.adjustsFontSizeToFitWidth=true self.buttonLabel.minimumScaleFactor=0.6 self.buttonLabel.textAlignment = .center - self.buttonLabel.text = buttonText - self.buttonLabel.textColor = UIColor.white self.buttonLabel.font = self.buttonFont - self.buttonLabel.textColor = self.buttonTextColor + configureButtonLabel() self.addSubview(self.buttonLabel) self.dragPointButtonLabel = UILabel(frame: CGRect(x: dragPointWidth, y: 0, width: self.frame.size.width - (dragPointWidth * 2), height: self.frame.size.height)) From 9ba0232f482107c3ab7fd6b6610d4a77c38e1d1b Mon Sep 17 00:00:00 2001 From: Ben Guild Date: Fri, 15 Sep 2017 20:01:53 +0900 Subject: [PATCH 19/22] Code cleanup. --- MMSlidingButton/MMSlidingButton.swift | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/MMSlidingButton/MMSlidingButton.swift b/MMSlidingButton/MMSlidingButton.swift index f3e3197..386754f 100644 --- a/MMSlidingButton/MMSlidingButton.swift +++ b/MMSlidingButton/MMSlidingButton.swift @@ -321,7 +321,7 @@ import UIKit @objc func panDetected(sender: UIPanGestureRecognizer){ var translatedPoint = sender.translation(in: self) translatedPoint = CGPoint(x: translatedPoint.x, y: self.frame.size.height / 2) - sender.view?.frame.origin.x = min(0, max(dragPointWidth - self.frame.size.width, (dragPointWidth - self.frame.size.width) + translatedPoint.x)); + sender.view?.frame.origin.x = min(0, max(dragPointDefaultOriginX(), dragPointDefaultOriginX()+translatedPoint.x)); //// let wasInsideUnlockRegion=isInsideUnlockRegion @@ -426,7 +426,7 @@ import UIKit } UIView.transition(with: self, duration: 0.2, options: .curveEaseOut, animations: { - self.dragPoint.frame = CGRect(x: self.dragPointWidth - self.frame.size.width, y: 0, width: self.dragPoint.frame.size.width, height: self.dragPoint.frame.size.height) + self.dragPoint.frame = CGRect(x: self.dragPointDefaultOriginX(), y: 0, width: self.dragPoint.frame.size.width, height: self.dragPoint.frame.size.height) }) { (Status) in if Status{ self.dragPointButtonLabel.text = self.dragPointButtonText() From 2b44c003de790aaf1ace394d088349aa90a4df97 Mon Sep 17 00:00:00 2001 From: Ben Guild Date: Fri, 15 Sep 2017 20:16:08 +0900 Subject: [PATCH 20/22] Code cleanup. Enabling full drag point text alignment customization. --- MMSlidingButton/MMSlidingButton.swift | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/MMSlidingButton/MMSlidingButton.swift b/MMSlidingButton/MMSlidingButton.swift index 386754f..4530877 100644 --- a/MMSlidingButton/MMSlidingButton.swift +++ b/MMSlidingButton/MMSlidingButton.swift @@ -93,12 +93,13 @@ import UIKit } } - @objc @IBInspectable var dragPointTextColor: UIColor = UIColor.white { + @objc @IBInspectable var dragPointTextAlignment: NSTextAlignment = NSTextAlignment.center { didSet{ setStyle() } } - @objc @IBInspectable var alignDragPointTextRight: Bool = false { + + @objc @IBInspectable var dragPointTextColor: UIColor = UIColor.white { didSet{ setStyle() } @@ -241,15 +242,21 @@ import UIKit } fileprivate func dragPointDefaultOriginX() -> CGFloat { + if (invertSwipeDirection) + { + return self.frame.size.width - dragPointWidth; + + } + return dragPointWidth - self.frame.size.width; } fileprivate func dragPointButtonLabelTextAlignment() -> NSTextAlignment { - var textAlignment = NSTextAlignment.center + var textAlignment = dragPointTextAlignment - if (!unlocked && alignDragPointTextRight) { - textAlignment = .right + if (unlocked) { + textAlignment = .center } @@ -408,7 +415,7 @@ import UIKit self.dragPointButtonLabel.text = self.buttonUnlockedText self.imageView.isHidden = true self.dragPoint.backgroundColor = self.buttonUnlockedColor - self.dragPointButtonLabel.textAlignment = .center // NOTE: Ensures this is reset if "alignDragPointTextRight" is `true` + self.dragPointButtonLabel.textAlignment = self.dragPointButtonLabelTextAlignment() self.dragPointButtonLabel.textColor = self.buttonUnlockedTextColor self.delegate?.unlocked(slidingButton: self) } @@ -436,7 +443,7 @@ import UIKit self.unlocked = false //// - self.dragPointButtonLabel.textAlignment = self.dragPointButtonLabelTextAlignment() // NOTE: Ensures this is reset if "alignDragPointTextRight" is `true`, but must be done after "unlocked" is reset to `false`. + self.dragPointButtonLabel.textAlignment = self.dragPointButtonLabelTextAlignment() // NOTE: Ensures this is reset after unlock. } } } From a105b589aba97c277f8dd9fc4cbbe471ec06eabf Mon Sep 17 00:00:00 2001 From: Ben Guild Date: Fri, 15 Sep 2017 20:45:33 +0900 Subject: [PATCH 21/22] Adding ability to invert swipe direction. (right-to-left) --- MMSlidingButton/MMSlidingButton.swift | 42 +++++++++++++++++++++++---- 1 file changed, 36 insertions(+), 6 deletions(-) diff --git a/MMSlidingButton/MMSlidingButton.swift b/MMSlidingButton/MMSlidingButton.swift index 4530877..25b9648 100644 --- a/MMSlidingButton/MMSlidingButton.swift +++ b/MMSlidingButton/MMSlidingButton.swift @@ -123,6 +123,9 @@ import UIKit @objc @IBInspectable var optionalButtonUnlockingText: String = "" + @objc @IBInspectable var invertSwipeDirection: Bool = false + + @objc private(set) var dragPoint = UIView() @objc private(set) var buttonLabel = UILabel() @objc private(set) var dragPointButtonLabel = UILabel() @@ -278,7 +281,7 @@ import UIKit if !self.buttonText.isEmpty{ var dragPointX : CGFloat = 0; - if offsetButtonTextByDragPointWidth + if (offsetButtonTextByDragPointWidth && !invertSwipeDirection) { dragPointX=dragPointWidth; @@ -309,7 +312,14 @@ import UIKit self.bringSubview(toFront: self.dragPoint) if self.imageName != UIImage(){ - self.imageView = UIImageView(frame: CGRect(x: self.frame.size.width - dragPointWidth, y: 0, width: self.dragPointWidth, height: self.frame.size.height)) + var dragPointImageX : CGFloat = (self.frame.size.width - dragPointWidth); + + if (invertSwipeDirection) { + dragPointImageX=0; + + } + + self.imageView = UIImageView(frame: CGRect(x: dragPointImageX, y: 0, width: self.dragPointWidth, height: self.frame.size.height)) self.imageView.autoresizingMask=[UIViewAutoresizing.flexibleHeight] self.imageView.contentMode = .center @@ -328,7 +338,14 @@ import UIKit @objc func panDetected(sender: UIPanGestureRecognizer){ var translatedPoint = sender.translation(in: self) translatedPoint = CGPoint(x: translatedPoint.x, y: self.frame.size.height / 2) - sender.view?.frame.origin.x = min(0, max(dragPointDefaultOriginX(), dragPointDefaultOriginX()+translatedPoint.x)); + + if (invertSwipeDirection) { + sender.view?.frame.origin.x = max(0, min(dragPointDefaultOriginX(), dragPointDefaultOriginX()+translatedPoint.x)); + + } else { + sender.view?.frame.origin.x = min(0, max(dragPointDefaultOriginX(), dragPointDefaultOriginX()+translatedPoint.x)); + + } //// let wasInsideUnlockRegion=isInsideUnlockRegion @@ -340,9 +357,20 @@ import UIKit if (!includingVelocity) { velocityXToUse=0 + } + //// + + var thresholdXAmount : CGFloat = 60; + + if (invertSwipeDirection) { + return (self.frame.size.width+(translatedPoint.x + velocityXToUse)) < self.dragPointWidth + thresholdXAmount; + + } else { + thresholdXAmount = (self.frame.size.width - thresholdXAmount) + } - return (((translatedPoint.x + velocityXToUse) + self.dragPointWidth) > (self.frame.size.width - 60)); + return (((translatedPoint.x + velocityXToUse) + self.dragPointWidth) > thresholdXAmount); } @@ -409,7 +437,8 @@ import UIKit //lock button animation (SUCCESS) func unlock(){ UIView.transition(with: self, duration: 0.2, options: .curveEaseOut, animations: { - self.dragPoint.frame = CGRect(x: self.frame.size.width - self.dragPoint.frame.size.width, y: 0, width: self.dragPoint.frame.size.width, height: self.dragPoint.frame.size.height) + self.dragPoint.frame.origin.x=self.frame.size.width - self.dragPoint.frame.size.width; + }) { (Status) in if Status{ self.dragPointButtonLabel.text = self.buttonUnlockedText @@ -433,7 +462,8 @@ import UIKit } UIView.transition(with: self, duration: 0.2, options: .curveEaseOut, animations: { - self.dragPoint.frame = CGRect(x: self.dragPointDefaultOriginX(), y: 0, width: self.dragPoint.frame.size.width, height: self.dragPoint.frame.size.height) + self.dragPoint.frame.origin.x=self.dragPointDefaultOriginX(); + }) { (Status) in if Status{ self.dragPointButtonLabel.text = self.dragPointButtonText() From 0292fa90c88785feeb7afdd27511326d2ad29323 Mon Sep 17 00:00:00 2001 From: Ben Guild Date: Fri, 15 Sep 2017 21:07:30 +0900 Subject: [PATCH 22/22] Fixing rotation during active swipe gesture. --- MMSlidingButton/MMSlidingButton.swift | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/MMSlidingButton/MMSlidingButton.swift b/MMSlidingButton/MMSlidingButton.swift index 25b9648..03d4c89 100644 --- a/MMSlidingButton/MMSlidingButton.swift +++ b/MMSlidingButton/MMSlidingButton.swift @@ -160,7 +160,13 @@ import UIKit panGestureRecognizer?.isEnabled=true //// - self.dragPoint.frame.origin.x=dragPointDefaultOriginX() + dragPointWidthDifference; + if (self.invertSwipeDirection) { + self.dragPoint.frame.origin.x=dragPointDefaultOriginX() - dragPointWidthDifference; + + } else { + self.dragPoint.frame.origin.x=dragPointDefaultOriginX() + dragPointWidthDifference; + + } } //// @@ -174,8 +180,14 @@ import UIKit } func fixOffsets() { - self.dragPoint.frame.origin.x=self.dragPoint.frame.origin.x - dragPointWidthDifference; - self.imageView.frame.origin.x=self.imageView.frame.origin.x + dragPointWidthDifference; + if (self.invertSwipeDirection) { + self.dragPoint.frame.origin.x=self.dragPoint.frame.origin.x + dragPointWidthDifference; + + } else { + self.dragPoint.frame.origin.x=self.dragPoint.frame.origin.x - dragPointWidthDifference; + self.imageView.frame.origin.x=self.imageView.frame.origin.x + dragPointWidthDifference; + + } } ////