Skip to content

Commit

Permalink
Support disabling touches via allowsHitTesting (#294)
Browse files Browse the repository at this point in the history
  • Loading branch information
bryankeller authored Jan 10, 2024
1 parent a360d71 commit ef63533
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 4 deletions.
7 changes: 6 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,12 @@ All notable changes to this project are documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [Unreleased](https://github.com/airbnb/HorizonCalendar/compare/v1.16.0...HEAD)
## [Unreleased](https://github.com/airbnb/HorizonCalendar/compare/v2.0.0...HEAD)

### Added
- Added support for disabling touch handling on SwiftUI views via the `allowsHitTesting` modifier

## [v2.0.0](https://github.com/airbnb/HorizonCalendar/compare/v1.16.0...v2.0.0) - 2023-12-19

### Added
- Added `MonthsLayout.vertical` and `MonthsLayout.horizontal` as more concise alternatives to `MonthsLayout.vertical(options: .init())` and `MonthsLayout.horizontal(options: .init())`, respectively
Expand Down
4 changes: 1 addition & 3 deletions Sources/Public/CalendarView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1016,16 +1016,14 @@ public final class CalendarView: UIView {
}

private func updateSelectedDayRange(gestureRecognizer: UIGestureRecognizer) {
let locationInScrollView = gestureRecognizer.location(in: scrollView)

// Find the intersected day
var intersectedDay: Day?
for subview in scrollView.subviews {
guard
!subview.isHidden,
let itemView = subview as? ItemView,
case .layoutItemType(.day(let day)) = itemView.itemType,
itemView.frame.contains(locationInScrollView)
itemView.hitTest(gestureRecognizer.location(in: itemView), with: nil) != nil
else {
continue
}
Expand Down
14 changes: 14 additions & 0 deletions Sources/Public/ItemViews/SwiftUIWrapperView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,20 @@ public final class SwiftUIWrapperView<Content: View>: UIView {
}
}

public override func point(inside point: CGPoint, with event: UIEvent?) -> Bool {
// `_UIHostingView`'s `isUserInteractionEnabled` is not affected by the `allowsHitTesting`
// modifier. Its first subview's `isUserInteractionEnabled` _does_ appear to be affected by the
// `allowsHitTesting` modifier, enabling us to properly ignore touch handling.
if
let firstSubview = hostingController.view.subviews.first,
!firstSubview.isUserInteractionEnabled
{
return false
} else {
return super.point(inside: point, with: event)
}
}

public override func layoutSubviews() {
super.layoutSubviews()
hostingControllerView?.frame = bounds
Expand Down

0 comments on commit ef63533

Please sign in to comment.