Skip to content

Instantly share code, notes, and snippets.

@sk-chanch
Last active March 31, 2021 11:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sk-chanch/b1b6ddc1adca6634397888d4b455ddd8 to your computer and use it in GitHub Desktop.
Save sk-chanch/b1b6ddc1adca6634397888d4b455ddd8 to your computer and use it in GitHub Desktop.
RxSwift ObservableType UILongPressGestureRecognizer LongPressGesture inspired from https://gist.github.com/saoudrizwan/43ba7adad04ae28bbf8bd96431f7749b.
//
// LongPressGestureRecognizerExtension.swift
// AppStarterKit
//
// Created by Chanchana Koedtho on 31/3/2564 BE.
//
import Foundation
import RxSwift
var longPressGRStartPoint:UInt8 = 0
var didCancelLongPressGR:UInt8 = 0
enum LongTapGestureButton {
case touchUpInside
case highlighted
case unhighlighted(_ shouldTackAction:Bool)
}
extension ObservableType where Element == UILongPressGestureRecognizer{
func asButtonEvent()->Observable<LongTapGestureButton?>{
return asObservable().flatMap{ sender -> Observable<LongTapGestureButton?> in
let currentPoint = sender.location(in: sender.view)
switch sender.state {
case .began:
objc_setAssociatedObject(Element.self,
&longPressGRStartPoint,
sender.location(in: sender.view),
.OBJC_ASSOCIATION_RETAIN_NONATOMIC)
objc_setAssociatedObject(Element.self,
&didCancelLongPressGR,
false,
.OBJC_ASSOCIATION_RETAIN_NONATOMIC)
return .just(.highlighted)
case .changed:
if (objc_getAssociatedObject(Element.self, &didCancelLongPressGR) as? Bool) ?? false {
return .just(.none)
}
let currentLongPress = objc_getAssociatedObject(Element.self, &longPressGRStartPoint) as? CGPoint
guard currentLongPress != nil else {
return .just(.none)
}
let distance = hypotf(Float(currentPoint.x - currentLongPress!.x), Float(currentPoint.y - currentLongPress!.y))
if distance > 55 {
objc_setAssociatedObject(Element.self,
&didCancelLongPressGR,
true,
.OBJC_ASSOCIATION_RETAIN_NONATOMIC)
return .just(.unhighlighted(false))
}
return .just(.none)
case .ended:
if (objc_getAssociatedObject(Element.self, &didCancelLongPressGR) as? Bool) ?? false {
return .just(.none)
}
return .just(.unhighlighted(true))
default:
return .just(.none)
}
}
}
}
@sk-chanch
Copy link
Author

sk-chanch commented Mar 31, 2021

Use

 view.rx.longPressGesture(){tap, _ in
            tap.minimumPressDuration = 0
        }
        .asButtonEvent()
        .subscribe(onNext:{[weak self] sender in
            
            
            
            switch sender {
            case .highlighted:
                 // update ui
                
            case .unhighlighted(let isAction):
                // update ui
                
             
                if !isAction { return }
                // do the action
             
                
            default:
                return
            }
        }).disposed(by: disposeBag)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment