Pass UICollectionView touch event to its parent UITableViewCell
Excellent answer by @tounaobun. Tested and it works as expected:
1) If you tap anywhere on the collection view that is not an item, then the table cell underneath will select just fine.
2) if you tap on the items in the collection view, then the table cell will not select and you can interact with the collection view normally (aka, scrolling, invoking didSelectItem, etc)
I converted to swift for reference:
Swift 3:
class TableCellCollectionView: UICollectionView {
override func hitTest(_ point: CGPoint, with event: UIEvent?) -> UIView? {
if let hitView = super.hitTest(point, with: event) {
if hitView is TableCellCollectionView {
return nil
} else {
return hitView
}
} else {
return nil
}
}
Just add this class definition to your code (I have it in a utility file) and then change the collection view's class from UICollectionView to TableCellCollectionView and you should be all set.
I want to pass touch event for a transparent custom cell to its parent view (which is a pageview controller)?
You can add one more view on top of it (pageview -> cellview -> new view on top with opacity = 0). Next add swipe gesture to new view and finally make a newView.delegate = pageviewController to capture swipe event.
@protocol NewViewControllerDelegate<NSObject>
-(void)gestureDidFire;
@end
@interface NewViewController : UIViewController
// you gesture outlet look like IBAction *swipeGestureReconizer;
@property (nonatomic, assign)id <NewViewControllerDelegate>delegate;
@end
//newViewContrlloer.m
- IBAction swipeGestureReconizer
{
if(self.delegate && [self.delegate responseToSelector:@seletor(gestureDidFire)])
{
[self.delegate gestureDidFire];
}
}
//in your page view interface
@interface yourPageView()<NewViewControllerDelegate>
{
//need instance of newView
newViewController.delegate = self;
}
-(void)gestureDidFire
{
//implement what you want
}
UISlider inside UICollectionViews leads does not receive touch events
Create a new .swift
file. Name it whatever you want and paste in this code:
import UIKit
class YourClassName: UICollectionViewCell {
override func point(inside point: CGPoint, with event: UIEvent?) -> Bool {
for subview in subviews {
if !subview.isHidden && subview.isUserInteractionEnabled && subview.point(inside: convert(point, to: subview), with: event) {
return true
}
}
return false
}
}
Then, click on your collection view cell and go to the identity inspector and where it says 'class' input the class name you created earlier. Hope this works!
EDIT
Their personal solution is below, but here is a general solution for anyone else who needs to fit this in their code :)
UIScrollView contained inside of UICollectionViewCell not passing touch event to the CollectionView cell
Thats a good question as I was having the same problem. The only way I could get the uicollectionviewcell to respond to touches (intercepted from the uiscrollview) was to set
userinteractionenabled = NO
on the scrollview.
UITableView passes touch events to superview when it shouldn't
if i understand correctly, you want the MKMapView not to react at all if a gesture is made on the UITableView (in the example, the myTableView property of myMKMapView is the UITableView in question). if so, you should implement
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch;
in your subclass of MKMapView (make a subclass if you dont have one. i've called the subclass myMKMapView in this example). Start off by making your subclass of MKMapView, myMKMapView, conform to the UIGestureRecognizerDelegate
protocol.
in the MKMapView's .h file:
@interface myMKMapView : MKMapView <UIGestureRecognizerDelegate>
After this is done, go into myMKMapView's .m file and implement the following as such:
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {
UIView *view = [self.view hitTest:[touch locationInView:self.view] withEvent:nil];
if ([view isDescendantOfView:(self.myTableView)]) {
return NO;
}
return YES;
}
This tells myMKMapView that if a gesture is performed on its myTableView property, myMKMapView should ignore it.
Touch events of Scrollview in subview of UITableviewCell in iOS
If the scroll view is a subview of the table view, you can capture the gesture and send it to its parent view like this:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
[self.nextResponder touchesBegan:touches withEvent:event];
}
Related Topics
Module 'Googlemobileads' Not Found in iOS
iOS 11 Animated Gif Display in Uiimageview
How to Check Text Field Input at Real Time
Removing Lagging Latency in Drawing Uibezierpath Smooth Lines in Swift
How to Sign Out of Apple After Being Authenticated
Hide Status Bar and Increase the Height of Uinavigationbar
Detecting iOS Version Number from User Agent Using Regular Expressions
Module Compiled with Swift 5.0.1 Cannot Be Imported by the Swift 5.1 Compiler
iOS Calculate Text Height in Tableview Cell
Adding Views. Storyboard VS. Programmatically
Physicsbody Doesn't Adhere to Node's Anchor Point
Two Buttons Inside Hstack Taking Action of Each Other
How to Ignore Certain Uitouch Points in Multitouch Sequence
Get the Expiration Date of a Provisioning Profile at Run-Time
How to Get Index(Of:) to Return Multiple Indices
Apple Watchkit Simulator Issue: Sperrorinvalidbundlenogizmobinarymessage