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

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.

Related

touch events on UITextField rightView

I have several text fields which compromise a registration form . When the user hit submit and made invalid inputs the wrong fields are marked with an icon as rightView.
The rightView i use is a custom UIImageView subclass which has it's:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
overwritten to display a alert to tell the user what exactly is wrong with this field.
The problem is that the touchesBegan method is never called. Why is the view not receiving touch events when I touch it?
you need to enable user interaction of images to TRUE
[imageView setUserInteractionEnabled:YES];
You should check whether your view is on the upper level of views (that it's not overlayed by another transparrent view)
And also check for view's user interaction set to ENABLED
Also check that you tap directly on the view, this method is called only when you tap within your view's bounds
I also recommend you to read a bit about UITapGestureRecognizer class may be this would be more convenient way to use...

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

fire event when touch up inside uiview and outside button

I have a view overlayed to the iPhone's camera view. In this view I have some uibuttons that are displayed in positions depending of values from accelerometer/compass.
I need to fire an event when the user touch up inside the view but he doesn't touch up inside the uibuttons, I mean, the opposite action of touch up inside the uibuttons.
Anyone knows a kind to do this?
Thanks!
If you implement the UIResponder touchesBegan:withEvent: method..
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
...on your view, you'll be able to tell when the user has pressed outside of the buttons.
See the UIResponder Class Reference for more information.
you may e.g. assign one event handler (IBAction) to UiVeiw and its UIButtons and then compare sender parameter:
-(IBAction) viewClicked :(id) sender {
if (id == myView) ...
}
I tried your options but it doesn't work. The view that I tried to fire the event was in a TabBar item in a camera view, so I think the responders chain doesn't work with it.
Finally, I solved the problem doing the overlay view the first responder with:[viewOverlay.view becomeFirstResponder];

Scrolling Problem on using UIWebView in UITableViewCell

I created the instance of the UILabel and added it as the subView of the contentView which is embedded in the table view cell.
Then I created web view (from UIWebView) and added it as the subView on the previously created label. (web view is smaller than label)
And then scrolling over the label,
I found that if touch occurs inside of the web view, scrolling of the table view doesn't work well. It seems that web view captures all the touch events so that its parent (label) doesn't get any touches.
But if touch occurs in the label outside of the web view, scrolling of the table view works well.
I know that if I set setUserInteractionEnabled of web view to 'NO', this problem will be fixed. But web view has some important links that should not be disabled.
Is there any way to make scrolling of the table view work well without disabling user interaction of the web view?
Thanks in advance.
Isn't it a problem caused by your webview trying to scroll ? try
[[[myWebView subviews] lastObject] setScrollingEnabled:NO];
have you tried - (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event returning the tableView?

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