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 + } + } +}