Touch underlying view with nextResponder - iphone

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 cells 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.).

Related

Can a UIView know when it is on screen

I have a UIView with a lot of components enclosed in it and would like to update some of them if the view is removed or if its parent view controller is popped/pushed. Is it possible for a UIView to get this information
Similar to the method in UIViewController
-(void)viewWillAppear;
Would like something like
-(void)UIViewWillAppear;
Edit for some comments I saw:
I'll explain a bit more
But I have a special case where the UIView needed to add a "floating view" on top of itself (Imagine a zooming/panning/scrolling UISCrollView subclass with floater on top of itself) such that when it scrolled the floating view stayed in place relative to the superview. I tried recalculating the new origin of the "floater" inside of the -(void)layoutSubviews method but the re-placement was very choppy. In order to solve this choppyness problem, the custom UIView added the floating view (which in theory is its subview) as a subview for its superview (a bit of a tongue twister :) ).
Now arises a problem when the custom UIView is removed (or its containing view controller is pushed offscreen). How can the special UISCrollView remove the floating view from its superView.
You can override willMoveToSuperview: to find out when a view is inserted into a hierarchy and when it's removed. That's probably not what you want since the view can be part of a hierarchy and not be inserted by itself.
To find out if it's on screen use willMoveToWindow:. If the argument is non-nil the view just became part of a visible view hierarchy.
If you need to do something after the change use didMoveToWindow:.
UIView do not appear/disappear 'randomly' or when they want - your view controllers (or code) control this. So you should find out when you show them, and call code you need.
The UIView class reference has some kvo observing change.
By implementing -(void)willRemoveSubview:(UIView *)subview you could see the other way round.
UPDATE After reading the explanations:
I hope I understood correctly. I did something similiar time ago, but with a UITableView rather than a UIScrollView (but they are quite the same underneath).
It was like a popup detail view. I solved, as you already did, by adding the detail view to the UITableView superview, and then I added a close UIButton in the detail view, with a corresponding IBOutlet:
#interface CustomUIView : UIView
#property(nonatomic,weak) IBOutlet UIButtonView *closingButton;
-(void)closeDetail:(IBAction)action;
#end
and the action was just:
-(void)closeDetail:(IBAction)action {
// do your cleaning/change to the detail subviews
[self removeFromSuperview]; // <-- clsoe the detail view
}
sdsds

iOS: Add UIView to UITableView

I'm trying to add a UIView on top over the UITableView to mimic the iPhone Facebook style menu. I have it working fine by making the controller a UIViewController then adding a tableview however I am unable to make the menu a static menu unless the controller is a UITableView.
Is it possible to add a view ontop of a tableview and only make the tableview in the background scrollable without the view in the foreground scrolling?
Here is what I have with the subclass being UIViewController
But I am unable to make the tableview cells static via IB since it is not a subclass of UITableView Controller.
EDIT per NSJones Code:
It seems to be going somewhat in the right track. However the view still blocks the table. If I remove the view from the storyboard it will only display the table.
You can make a view hover the same way you make any real thing hover; Hold it up with something invisible.
Basically what you want to do is create a clear UIView (with user interaction disabled) that is the size of your view controller's view, and add it as a subview to your view controller's view property. That way it sits invisibly on top. then you can add a subview to that clear view and that subview won't move.
Edit:
It seems this nice clean approach won't work for you since you need your view controller to be a UITableViewController. The answer for this slightly more complex approach is to use a delegate method for UIScrollView which also works for UITableView. Apple has a fantastic demo of this concept in the WWDC2011 - Session 125 - UITableView Changes, Tips, Tricks video. If you can watch it I highly recommend it. The meat of this issue begins at about 36:10.
But to sum it up you implement the scrollViewDidScroll: delegate method. And handle the movement of the tableview by adjusting the position properties of the view. Here I am keeping an UIView property named viewToKeepStill still using this method.
-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
// CGFloat stillViewDesiredOriginY; declared ivar
CGRect tableBounds = self.tableView.bounds; // gets content offset
CGRect frameForStillView = self.viewToKeepStill.frame;
frameForStillView.origin.y = tableBounds.origin.y + stillViewDesiredOriginY; // offsets the rects y origin by the content offset
self.viewToKeepStill.frame = frameForStillView; // set the frame to the new calculation
}
Instead of adding it as a subview of the table view, add it as a subview of the superview of the table view; that way it won't scroll.
So instead of this:
[tableView addSubview:viewController.view];
Do this:
[tableView.superview addSubview:viewController.view];
Assuming you want something that is visible full-time with the table, start with a view which contains both the menu view and the UITableView. Make the table smaller so it ends where the menu view begins. The table view can work with less vertical space.
If you have your UIViewController's view to be your table view then your table is going to span over the whole screen, so you won't be able to add anything on top of it.
Why not try the following:
1) create a new UIViewController
2) add a view on top where you want your menu
3) in the space left under just drag a table view from the component library
4) don't forget to set the 2 table view delegates to be your view controller class
that's about it?

User interaction disabled for view but enabled for subviews (UIButtons)

I have a view which contains several UIButtons which overlays a UIScrollView. I want to prevent user interaction on the overlay view but not on the UIButtons that are contained within that view.
The reason why I am grouping these views into a single view is such that I can apply an alpha change to all the buttons in the view by just changing a single property. I have just noticed the IBOutletCollection in IOS 4.0 but I need to also target IOS 3.0.
Is there a more simple way to achieve this than overriding the following UIView method?
- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event;
Unfortunately if you disable user interaction with a view then user interactions with all subviews are also disabled. While not ideal, you'll just need to make IBOutlets for each of the buttons and adjust them accordingly. To avoid having to write to much additional code in the future, in your viewDidLoad you can create an NSArray and toss each of the buttons into it. Then every time you want to change one of the attributes on all of those buttons, you can just loop over the array and change them. That way if you add another button to the group you only need to update the array and the rest of the changes will automatically propagate.
Please have a look at this answer: https://stackoverflow.com/a/13414182/2082569
According to this you need to override this method in your custom UIView:
- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event {
UIView *subview = [super hitTest:point withEvent:event];
return subview == self.button ? subview : nil;
}
disable only the scroll view using
scrollView.scrollEnabled = FALSE;
when you want the user to allow usage of button.
Enable again when you want the user to scroll the view using
scrollView.scrollEnabled = TRUE;
Note : you don't need to disable the user interaction of scroll view.

determine if uiview is displayed

is there a possibility to determine if an uiview obj is going to be displayed. imagine: you have 2 uiviews in an uiscrollview. now you are going to switch per gesture from the first view to the second. the first view now is NOT in the viewport. now you are going to go back to the first view. and now I want to be notified that this view is in viewport, or is redisplayed. the same has to be for the second view. I have not found any callback or something like this.
You make sure your UiViewController overrides viewWillAppear: (before it appears this method is called) or viewDidAppear: (after this method is called).
See: http://developer.apple.com/iphone/library/documentation/uikit/reference/UIViewController_Class/Reference/Reference.html#//apple_ref/occ/instm/UIViewController/viewWillAppear:
That depends what you mean by "switch". If one view is just scrolled out of the visible area of the scrollview, but still remains attached as a subview to it, then you may want to check if the bounds of your view overlap those of the scrollviews visible area.
You could do this by using UIScrollView Delegate's scrollViewDidScroll: method to implement a check for overlaps while the user is scrolling.
If however your view is actually removed from the viewstack, then you may want to subclass UIView and implement willMoveToSuperview: to check if the view has been added to the scrollview again.

Select tablecell when touching embedded view

I have a custom tablecell with an embedded MapView showing a small area. When the user selects the cell, I want to push a new view with a larger mapview and some more information, like distance from where you are, option of what map-type etc.
If I leave a small margin around my mapview, the user can click in that margin to select the cell, but how can I make the cell selected if they click inside the mapview?
regards,
-Vegar
I think you need to override the hitTest method inherited from UIView
- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event
This method traverses the view
hierarchy by sending the
pointInside:withEvent: message to each
subview to determine which subview
should receive a touch event. If
pointInside:withEvent: returns YES,
then the subview’s hierarchy is
traversed; otherwise, its branch of
the view hierarchy is ignored. You
rarely need to invoke this method, but
you might override it to hide touch
events from subviews.
Try set MapView's userIteractionEnabled property to NO