Last active
October 29, 2023 14:16
-
-
Save saoudrizwan/43ba7adad04ae28bbf8bd96431f7749b to your computer and use it in GitHub Desktop.
Using a long press gesture recognizer, you can recreate a 'touch up inside' button effect on any view.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let longPress = UILongPressGestureRecognizer(target: self, action: #selector(upgradeAlertViewOtherUpgradesLongPressHandler)) | |
longPress.minimumPressDuration = 0 | |
var longPressGRStartPoint: CGPoint? | |
var didCancelLongPressGR = false | |
func viewTouched(sender: UILongPressGestureRecognizer) { | |
let currentPoint = sender.location(in: self.view) | |
switch sender.state { | |
case .began: | |
print("touch began") | |
longPressGRStartPoint = sender.location(in: self.view) | |
didCancelLongPressGR = false | |
highlightView() | |
case .changed: | |
if didCancelLongPressGR { return } | |
guard longPressGRStartPoint != nil else { return } | |
//print("touches changed") | |
let distance = hypotf(Float(currentPoint.x - longPressGRStartPoint!.x), Float(currentPoint.y - longPressGRStartPoint!.y)) | |
//print(distance) | |
if distance > 55 { | |
didCancelLongPressGR = true | |
unhighlightView(shouldTakeAction: false) | |
} | |
case .ended: | |
if didCancelLongPressGR { return } | |
print("touches ended") | |
unhighlightView(shouldTakeAction: true) | |
default: | |
break | |
} | |
} | |
func highlightView() { | |
// update ui | |
print("view highlighted") | |
} | |
func unhighlightView(shouldTakeAction: Bool) { | |
// update ui | |
print("view unhighlighted") | |
if !shouldTakeAction { return } | |
// do the action | |
print("view action taken") | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
convert to RxSwift https://gist.github.com/sk-chanch/b1b6ddc1adca6634397888d4b455ddd8