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)
Related
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/
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 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'm trying to reproduce the buttom sheet in Apple's iOS 10 Maps app. Most of it is working. I've been looking at this SO post and Pulley on GitHub, but none of them solves my issue.
When the sheets is fully opened, it is possible to scroll the content of the sheet as a UITableView, but when the user tries to scroll down (where the UITableView's contentOffset would be negative), the gesture is dragging in the sheet instead of the UITableView. The gesture seamlessly changes from dragging the UITableView to dragging the sheet.
It is possiple disable the scrolling of the UITableView in the gesture delegate's shouldRecognizeSimultaneouslyWith, but this is only called when a gesture begins.
I can't control the panGestureRecognizer of the UITableView, so I can't just capture the gesture and determine what view it should move.
How can I change what UIGestureRecognizer should recognize touches, in the middle of a gesture?
Try
override func gestureRecognizerShouldBegin(_ gestureRecognizer: UIGestureRecognizer) -> Bool {
if gestureRecognizer == myCustomPanGesture {
return self.tableView.contentOffset == 0
}
return true
}
I have a UITableView added to UIViewController. Behind that TableView there is UIImageView like this:
The default behavior of a UITableView is to show whitespace when scrolling up and there is no more content to show. I would like to override this and switch to custom UIPanGestureRecognizer that will move the whole tableview down.
To clarify: When the tableview reaches it top I would like to move the entire tableview instead.
I have set up my PanGestureRecognizer and added it to the tableview and tried this:
func scrollViewDidScroll(scrollView: UIScrollView) {
if self.tableView.contentOffset.y == 0{ //tableview reaches its top
panGestureRecognizer.enabled = true
}else{
panGestureRecognizer.enabled = false
}
}
This does not work at all since the the scrollViewDidScroll triggers at the end of the gesture.
Any other ideas on how I can implement this?