Skip to content

Commit

Permalink
Merge pull request #1 from Priva28/v1.1
Browse files Browse the repository at this point in the history
add option to ignore touches that reach the bottom of a swiftui scrol…
  • Loading branch information
Priva28 authored Feb 6, 2023
2 parents a8f584e + ab94fd4 commit e7b8134
Showing 1 changed file with 18 additions and 1 deletion.
19 changes: 18 additions & 1 deletion Sources/HostingPassthrough/HostingPassthrough.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ open class HostingParentController: UIViewController {
/// If the touches land on the base view of the HostingParentController, they will be forwarded to this view if it is not nil.
public var forwardBaseTouchesTo: UIView?

/// If the touches land on the bottom of a SwiftUI scroll container (*not* the content), pass through these touches to the UIKit layer underneath.
public var ignoreTouchesOnSwiftUIScrollView = false

override public func loadView() {
let capturer = HostingParentView()
view = capturer
Expand All @@ -19,14 +22,16 @@ open class HostingParentController: UIViewController {
let capturer = view as! HostingParentView
capturer.makeBackgroundsClear = makeBackgroundsClear
capturer.forwardBaseTouchesTo = forwardBaseTouchesTo
capturer.ignoreTouchesOnSwiftUIScrollView = ignoreTouchesOnSwiftUIScrollView
}
}

/// Use HostingParentView instead of UIView in places where you aren't adding a UIHostingController to a view controller. Otherwise use HostingParentController instead.
open class HostingParentView: UIView {
private var hostingViews: [UIView] = []
public var forwardBaseTouchesTo: UIView?
public var makeBackgroundsClear: Bool = true
public var makeBackgroundsClear = true
public var ignoreTouchesOnSwiftUIScrollView = false

override public func hitTest(_ point: CGPoint, with event: UIEvent?) -> UIView? {
guard let hitTest = super.hitTest(point, with: event) else { return nil }
Expand Down Expand Up @@ -84,6 +89,18 @@ open class HostingParentView: UIView {
} else {
return forwardBaseTouchesTo.hitTest(point, with: event)
}

// if we are hitting the back of a scroll view, it's possible you might want to pass this touch through to the uikit layer underneath. scrolling is still possible when touching items, just not the bottom of the scroll view.
} else if String(describing: view).contains("HostingScrollView"), view.isUserInteractionEnabled, ignoreTouchesOnSwiftUIScrollView {
view.isUserInteractionEnabled = false

guard let hitBehindScrollView = super.hitTest(point, with: event) else { return nil }

DispatchQueue.main.async {
view.isUserInteractionEnabled = true
}

return checkBehind(view: hitBehindScrollView, point: point, event: event)
} else {
return view
}
Expand Down

0 comments on commit e7b8134

Please sign in to comment.