How do I allow my tableView to be selectable by overriding hitTest?
I have a tableview that lives outside of bounds of its superview so I need to override hitTest of the view containing the superview. However, when I pass the tableview's hittest I can only scroll and not tap on a row.
public override func hitTest(_ point: CGPoint, with event: UIEvent?) -> UIView? {
let pointInTableview = tableView.convert(point, from: self)
guard tableView.bounds.contains(pointInTableview) else {
return super.hitTest(point, with: event)
}
return tableView.hitTest(pointInTableview, with: event)
}
The datasource and delegates are set. If I manually call tableview.selectRowAt(..) I am able to receive the delegate callback.
Gif of demo
The VC View is the view that is overriding hitTest. We are trying to pass the touches to the tableview because the tableview is outside the bounds of the view it lives in.
The problem wasn't the hitTest.
My VC View had a tap gesture to close keyboards. This tap gesture was cancelling the tableview's tap gesture.
Ref: https://kakubei.github.io/2016/02/24/Tap-Gesture-and-TableView/
Related
I'm building a collectionview. Below of it I placed some buttons as shown in the picture.
What I want is to make the UICollectionView background pass taps below, so the desired buttons can receive taps.
I don't need to add Tap gesture recognizers to the background view (the problem I'm describing is just an example here), I need the buttons' actuons to be triggered directly when they're tapped.
I thought I could do this by making the background clear or disabling user interaction for the background view. While disabling it for the entire collection view works, this other way does not.
How can I make the background view of my collectionView be "invisible" so that taps go straight to the below buttons instead of going to the collectionview background?
The following is an example of my layout.
Assuming your collectionView and your buttons share the same superview, this should do the trick.
What you want to do is bypass the backgroundView and forward hits to the subviews underneath the collectionView.
Notice that we are picking the last subview with the matching criteria. That is because the last subview in the array is the closest to the user's finger.
class SiblingAwareCollectionView: UICollectionView {
override func hitTest(_ point: CGPoint, with event: UIEvent?) -> UIView? {
let hit = super.hitTest(point, with: event)
guard hit === backgroundView else {
return hit
}
let sibling = superview?
.subviews
.filter { $0 !== self }
.filter { $0.canHit }
.last { $0.point(inside: convert(point, to: $0), with: event) }
return sibling ?? hit
}
}
If you look at the documentation for hitTest(_:with:) it says:
This method ignores view objects that are hidden, that have disabled user interactions, or have an alpha level less than 0.01.
For convenience, here is an extension to ensure we are playing by the rules:
extension UIView {
var canHit: Bool {
!isHidden && isUserInteractionEnabled && alpha >= 0.01
}
}
I am trying to create a UITableViewCell which has a horizontally scrollable UIScrollView. How to make sure that when user touches on the UIScrollView, the UITableViewCell gets selected.
The UITableViewCell has a UIScrollView as a subview as well as some other subviews. When user taps on non-UIScrollView subviews, the cell gets selected as expected.
However, when user taps on the UIScrollView, nothing happens.
The UIScrollView has some sub views. The user can horizontally scroll the scrollview as expected.
Is there any way such that, when user flicks through the UIScrollView, the UIScrollView handles the touch event, but when user taps on the UIScrollView, it passes the event to the superview?
Edit -
I tried overriding touchesEnded(_:with:) as follows -
override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
superview?.touchesEnded(touches, with: event)
}
It did not work
Secondly, I tried adding a UITapGestureRecognizer to the scrollview and check if detects the taps.
It does detect the tap. However, in order to select the UITableViewCell, I need to get the respective UITableViewCell for the scrollview, find its indexPath and then using the UITableView select that indexPath.
I am hoping if there is a simpler way to perform what I am trying to achieve.
I solved the problem by adding a UITapGestureRecognizer to the scrollView.
In the function that gets called when the user taps on the scrollview, I programmatically select the row as follows -
guard let indexPath = tableView.indexPath(for: cell) else { return }
tableView.selectRow(at: indexPath, animated: true, scrollPosition: .none)
tableView(tableView, didSelectRowAt: indexPath)
I have a layout with a UIView at the top of the page and, right below it, I have a UITableView.
What I am wanting to do is to transfer the gesture interactions on the UIView to the UITableView, so when the user makes a drag up/down on the UIView, the UITableView scrolls vertically.
I tried the following code
tableView.gestureRecognizers?.forEach { uiView.addGestureRecognizer($0) }
but it removed the gestureRecognizers from the UITableView somehow :/
Obs.: the UIView cannot be a Header of the UIScrollView
That's Tricky
What is problem ?
Your top view is not allowed to pass through view behind it...
What would be possible solutions
pass all touches to view behind it (Seems to not possible or very tough practically )
Tell window to ignore touches on top view (Easy one)
Second option is better and easy.
So What you need to do is create subclass of UIView and override
override func hitTest(_ point: CGPoint, with event: UIEvent?) -> UIView?
and return nil if you found same view on hitTest action
Here Tested and working example
class PassThroughME : UIView {
override func hitTest(_ point: CGPoint, with event: UIEvent?) -> UIView? {
return super.hitTest(point, with: event) == self ? nil : self
}
}
That's it now use PassThroughME either by adding class to your view in storyboard or programmatically whatever way you have added your view
Check image i have black color view with 0.7 alpha on top still i am able to scroll
Hope it is helpful
I am trying to build similar controller to GMSPlacePicker.
I have Map View on background, then Table View with transparent header view. The problem is that all gestures (tap, pan) within header view are passed to table view. I wanna disable them, so all touches will go directly to map view.
I am able to do it If I set:
tableView.userInteractionEnabled = false
but now I am not able to scroll table view.
The question is how to disable all gesture only for header view, but keep getting them for table view.
Basically I wanna get the following behaviour: https://youtu.be/iSBbEZXDyGg
The trick was to create subclass of UITableView and override
func hitTest(point: CGPoint, withEvent event: UIEvent?) -> UIView?
ANTableView.swift
import UIKit
class ANTableView: UITableView
{
override func hitTest(point: CGPoint, withEvent event: UIEvent?) -> UIView?
{
let headerViewFrame = tableHeaderView!.convertRect(tableHeaderView!.frame, toView: self)
if CGRectContainsPoint(headerViewFrame, point) {
return nil
}
return self
}
}
You need to put your map inside the header view. Not behind it.
On top of my UICollectionViewCells in my UICollectionView I've overlaid a UIButton that intercepts touches so I can respond to touch events more granularly. The issue is that now the collection view no longer gets didSelectItemAtIndexPath messages. (For obvious reasons... the button has absorbed the touch and isn't signaling to the collection view that the item was selected.)
Is there a way to signal to the collection view that the cell was selected? I've seen similar questions but none seem to give a convincing answer.
You can override the pointInside:withEvent: message on UIView [and subclasses] and return false to continue propagating the touch event.
https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIView_Class/#//apple_ref/occ/instm/UIView/pointInside:withEvent:
class PassThroughButton: UIButton {
override func pointInside(point: CGPoint, withEvent event: UIEvent?) -> Bool {
// do something
// then continue event propigation
return false
}
}