From a5915f138d74b0e94550daaa3378947517cd4972 Mon Sep 17 00:00:00 2001 From: Filipp Krasnovid Date: Fri, 18 Nov 2022 18:51:14 +0300 Subject: [PATCH] UITouch+ControlEvent --- .../UIKit/UITouch/UITouch+ControlEvent.swift | 32 +++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 Sources/UIKit/UITouch/UITouch+ControlEvent.swift diff --git a/Sources/UIKit/UITouch/UITouch+ControlEvent.swift b/Sources/UIKit/UITouch/UITouch+ControlEvent.swift new file mode 100644 index 0000000..a0bec3e --- /dev/null +++ b/Sources/UIKit/UITouch/UITouch+ControlEvent.swift @@ -0,0 +1,32 @@ +import UIKit + +extension UITouch { + /// Convert UITouch to UIControl.Event + var toControl: UIControl.Event? { + guard let view = self.view else { return nil } + let isInside = view.bounds.contains(location(in: view)) + let wasInside = view.bounds.contains(previousLocation(in: view)) + switch phase { + case .began: + guard isInside else { return nil } + return tapCount > 1 ? .touchDownRepeat : .touchDown + case .moved: + if isInside && wasInside { + return .touchDragInside + } else if isInside && !wasInside { + return .touchDragEnter + } else if !isInside && wasInside { + return .touchDragExit + } else if !isInside && !wasInside { + return .touchDragOutside + } + return nil + case .ended: + return isInside ? .touchUpInside : .touchUpOutside + case .cancelled: + return .touchCancel + default: + return nil + } + } +}