I have a custom UITableViewCell that has a UIScrollView in it that covers the entire cell. I want to be able to use the scroll view to scroll a label in the cell (which is working) but the scroll view seems to be 'stealing' the cell's tap event. So I was wondering:
How do you pass a touch event from a UIScrollView to its parent UITableViewCell?
In the UISCrollViewController, have you tried passing the touchesEnded on to super, assuming a scroll is not in progress?
- (void)touchesEnded:(NSSet *)aTouches withEvent:(UIEvent *)anEvent
{
if(![self isDragging])
{
[super touchesEnded:aTouches withEvent:anEvent];
}
}
Related
I was trying to implement a container view controller design into my app. However I was told that I need to support iOS 4.3 devices, so the official view controller API introduced in iOS 5 is not an option at the moment.
In order to achieve a similar behaviour I used a hack. Resized the view for my RootViewController and the added a subview to it that it's outside the view's bounds. For example: RootView has bounds 0,0,320,480. Now I resized it to 0,0,320,430 and included a subview at 0,430,320,60. This works since I do all calculations using the ApplicationFrame giving me stable frames on which to work. But the problem I'm facing right now is that the subview which is out of the bounds of the view is not receiving touch events. The maskToBounds = NO property helps me with the display. But touches? Anybody know how to do that?
Whenever you want the subview to receive touch events in such cases, you can do the following:
1- Create a new class that inherits from UIView and override hitTest:withEvent: to allow subviews to intercept touches:
#interface CustomView : UIView
#end
#implementation CustomView
- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event {
/// Check if the point is inside the subview
CGPoint newPoint = [subview convertPoint:point fromView:self];
if ([subview pointInside:newPoint withEvent:event]) {
/// Let the subview decide the return value
return [subview hitTest:newPoint withEvent:event];
}
/// Default route
return [super hitTest:point withEvent:event];
}
#end
2- Change the class of root view to our CustomView (from the right panel in Xcode > Identity Inspector > Custom Class).
And we're done!
I'm creating a custom dropdown menu inside a cell of a UITableView adding this menu as a subview of the cell content view.
When I click on the menu inside the cell I enlarge the view to show all the menu items: now the menu view is over the tableView and all cells.
So, when I select on of the items inside my custom view, the TouchesBegan it's triggered only in the part of the view that's over the UITableView cell in which I call the menu and not in all the view that I enlarged.
If I touch the view in part that is over another cell, the TouchesBegan it's triggered in the cell of the table view and not in the view on top of all cells.
Any suggestion?
Views typically don't respond to touch events on views or portions of views drawn outside its bounds. There are a few possible options to fix the issue:
Enlarge the cell when the dropdown menu is visible to fit the entire menu, but this might negatively impact the visual effect depending on your design.
Add the menu on the controller instead of the cell view. If you got this route, you can use the UIScrollViewDelegate events from the table view to reposition the menu as the user scrolls the table. Remember to account for the case where the menu is displayed on the last cell and might overdraw the controller's bounds if always drawn in downward direction.
Override hitTest:withEvent: and pointInside:withEvent on both your menu view and its superview to inform the OS that you respond to events even outside its normal bounds.
The following worked for me:
Subclass your cell and connect the root view of your layout with the cell:
#interface CustomCell : UITableViewCell
#property (weak, nonatomic) IBOutlet UIView *rootView;
#end
Override hitTest of the cell like this:
#implementation CustomCell
- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event {
BOOL res = [self.rootView pointInside:point withEvent:event];
if(res){
return [self.rootView hitTest:point withEvent:event];
}
return [super hitTest:point withEvent:event];
}
#end
Now touchesBegin/Move/End/Cancel will be delivered to the view and it's hierarchy whenever touch is inside that view
I have tried searching over the internet about this problem but not able to come up with the solution that solves my problem.
I have make a subclass of UITableViewCell in which I added UIScrollView as a subview, now I want scroll gestures to be listened by scrolView and single tap gesture is listened by UItableView. Also using this layout is against the HIG of Apple
This is an old question but since I didn't really find a good answer I figured I'd offer one. This approach captures the tap events on both the UIScrollView (contained 'within' the table cell) AND tap events on the UITableView cells directly, and passes them along to the UITableViewDelegate. It also allows you to scroll your UIScrollView content without passing the scroll events along. Doesn't require any subclassing.
Within ViewDidLoad, instantiate a UITapGestureRecognizer on the UITableView
// Capture taps on scrollView fields and pass along to tableView
let tap = UITapGestureRecognizer(target: self, action: "tapIntercept:")
tableView.addGestureRecognizer(tap)
Create a callback function for the selector above
// Get the location of the table cell beneath the scrollView and pass the event off to the tableView
func tapIntercept(recognizer: UIGestureRecognizer) {
let point = recognizer.locationInView(tableView)
if let indexPath = tableView.indexPathForRowAtPoint(point) {
tableView(tableView, didSelectRowAtIndexPath: indexPath)
}
}
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];
}
I've got a rootView. Now I add a subview to my rootView by using the method addSubView:. After that the subview is added consisting of a view containing several buttons. Now what I want is, to be able to press both, the buttons on my rootView and the buttons on my subView. However when I turn off user interaction of my subView, I can't press its buttons any more. However if I let it on, I cant press the buttons of my rootView.
Can anyone help me?
For your "subview," subclass UIView and override the the hitTest method like this:
- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event
{
UIView *subview = [super hitTest:point withEvent:event];
if ( subview != self )
return subview;
else
return nil;
}
This will cause the buttons and other views within your "subview" to respond to events, but the view itself will act as if it's not there.
I have UILabels animating on my mainView and I want to have a custom UIView pop onto the main view when a button is pressed. The labels animating in the background continue to animate and the problem that I'm having is that the labels animate on top of my custom UIView.
Does anyone know how I can ensure that my custom UIView is the front-most view so that the UILabels animate behind it?
You may want to look at
- (void)bringSubviewToFront:(UIView *)view
in the parent view.
#implementation UIView (moveToFront)
- (void) moveToFront { [[self superview] bringSubviewToFront:self]; }
#end