Skip to content

Instantly share code, notes, and snippets.

@PimCoumans
Created August 2, 2022 16:20
Show Gist options
  • Save PimCoumans/6e82ca50a27df1d768b40fd9a73940fb to your computer and use it in GitHub Desktop.
Save PimCoumans/6e82ca50a27df1d768b40fd9a73940fb to your computer and use it in GitHub Desktop.
ScrollView subclass that forces touch cancellation in UIControl views
import UIKit
/// Custom scrollView that can forces touch cancellation, even in `UIControl`s
/// - Note: Make sure to set `canCancelContentTouches` to `true`
class TouchCancellingScrollView: UIScrollView {
/// Set to `false` to allow drags in`UIControls`
var canCancelControlContentTouches: Bool = true
/// Cancels all touches, even when touch is in a `UIControl`.
/// Set `alwaysCancelsContentTouches` to `false` to not use this behaviour
override func touchesShouldCancel(in view: UIView) -> Bool {
if canCancelContentTouches && canCancelControlContentTouches && view is UIControl {
return true
}
return super.touchesShouldCancel(in: view)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment