Touch underlying view with nextResponder
Currently I am trying to recreate the Twitter (iPad) type control. Mostly because all the versions available online are not really working for the type of application I am developing, and I think I can do better of course ;) (but it is harder than I thought).
What I want:
- Flexible positioning of the view controllers I add to the control
- No need to change my current view controllers when I want to use them in my control (mainly because I use the view controllers in the iPhone and the iPad version).
- Being able to drag the view controllers around, but still keep al functionality in the view controllers.
What I currently got:
- Good positioning on the view controllers, when they are added to my control.
- Able to drag the view controllers around, according to the rules I have set (maximum x position etc)
My problem is that when I add a UITableViewController, I want to be able to drag it around AND keep the functionality of scrolling and selecting c开发者_如何转开发ells in the table.
My current solution is, that when a view controller is added to my control, I add a subview to it (with the same size of the view controller) which accepts all the touches. These touches are then send to the control, by using a delegate and calling methods on it. This works well, accept that when I add a subview to my UITableViewController, there is some weird behavior. Scrolling up and down still works in the UITableViewController, but these touches aren't detected by the View on top of the UITableViewController. Selecting cells in the table does not work anymore, because these touches are taken by the overlaying UIView.
I add the overlaying UIView to the UITableViewController like this:
SNOverlayView* layover = [[SNOverlayView alloc] initWithFrame:CGRectMake(0,0, controllerWidth, self.frame.size.height)];
layover.delegate = self;
[controller.view addSubview:layover];
[layover release];
My custom overlay UIView contains a couple of these methods, for all the touch (began, ended, cancelled and moved) methods:
- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
if ([delegate respondsToSelector:@selector(overlayTouchesBegan:touches:event:)])
[delegate overlayTouchesBegan:self touches:touches event:event];
NSLog(@"%@", self.nextResponder);
[self.nextResponder touchesBegan:touches withEvent:event];
}
From what I understand from the documentation, the call to the nextResponder, should make sure that the TableView below my view also receives the touches. But these methods only get triggered when I touch the screen once OR slide sideways. Sliding from up to down does not trigger these methods and touching a cell does not trigger the didSelectRowAtIndexPath from the UITableViewController. The self.nextResponder in this method is a UITableView (which is outputted by the NSLog in that method).
I am misunderstanding the nextResponder part? Isn't this possible when not modifying my UITableViewController?
EDIT: I updated the structure which I add to my main UIView containing all ViewControllers:
|- Root UIView (1)
|-- Containment UIView which detects touches (2)
|---- UITableViewController (3)
The problem is that now my containment view (2) does not detect touches. I will try to update my code to make it more like the structure mentioned below:
|- Root UIView (1)
|-- Containment UIView (2)
|---- UIView detecting touches (4)
|---- UITableViewController (3)
But I am not sure how I can get the UIView to detect touches. because when I add the UITableViewcontroller (3) to my containment UIView (2) it will be put on top of the UIView detecting the touches (4) and therefor 'stealing' the touches. I would rather change the order of (4) and (3).
Are you adding a subview to a UITableView? This is dangerous, because it will scroll with the cells (UITableView is a sub-class of UIScrollView), and because the UITableView adds and removes subviews itself too, so you can't control the order that the views are in.
I think your best bet would be to have a view hierarchy like this
- containerView (plain UIView, that your UIViewController loads)
|-- overlayView (catches touches)
|-- UITableView
That being said, I've got a suspicion that UIScrollViews use a different method for grabbing touches to other UIViews (i.e. not touchesBegan: etc.).
精彩评论