diff --git a/README.md b/README.md index 9a2eea9..4381bb1 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,3 @@ -# DSUIKitExtensions +# UIKitExtensions A description of this package. diff --git a/Sources/DSUIKitExtensions/Extensions/UIColor.swift b/Sources/DSUIKitExtensions/Extensions/UIColor+.swift similarity index 100% rename from Sources/DSUIKitExtensions/Extensions/UIColor.swift rename to Sources/DSUIKitExtensions/Extensions/UIColor+.swift diff --git a/Sources/DSUIKitExtensions/Extensions/UITouch+.swift b/Sources/DSUIKitExtensions/Extensions/UITouch+.swift new file mode 100644 index 0000000..7ee2c47 --- /dev/null +++ b/Sources/DSUIKitExtensions/Extensions/UITouch+.swift @@ -0,0 +1,38 @@ +// Created for in 2022 +// +// Copyright (c) https://github.com/DuckingSwift + +import UIKit + +// MARK: - ControlEvent + +public 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 + } + } +}