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
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 want to have a scrollview that covers the entire screen. However I want to know if it is possible to only allow scrolling to be detected from part of the scrollview. For example you have a full screen scroll view the top half of the screen should detect scrolling but the bottom half should not. I know if you change the alpha to 0 the scrollview doesn't scroll anymore, would a possible solution be to change the alpha of part of the scrollview? is that even possible? any thoughts or ideas?
You can subclass from UIScrollView and override method touchesShouldBegin.
You should check at what point touches and allow it or not.
class ScrollView: UIScrollView {
override func touchesShouldBegin(_ touches: Set<UITouch>, with event: UIEvent?, in view: UIView) -> Bool {
if (touches.first?.location(in: self).y)! > self.bounds.height / 2 {
// In bottom part
return true
}
// In top part
return false
}
}
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
}
}
I'm creating a subclass of UIView to use in a project, and will handle touches on the main view. I'd like so that when the touch (on the main view) is dragged and contacts the special UIViews, they change their background color. Using UIView's default "touchesMoved" function only detects touches that originate on the specific view. I could set up the main view to check each instance of the custom UIView, but that would be contrary to encapsulation and lead to messy code.
Any ideas?
I'm not sure this is what your after, but if I put the following code in my view controller, whose view has several RDView views in it, the color of those views will change color when I drag from the main view into the RDView.
override func touchesMoved(touches: NSSet, withEvent event: UIEvent) {
var touched = self.view.hitTest((touches.anyObject() as UITouch).locationInView(self.view), withEvent: event)
if touched is RDView {
touched?.backgroundColor = UIColor.redColor()
}
}