iOS subview not forwarding touches. - ios

I have tried this a few different ways but none of them seem to work. I have a UIView subclass that has some buttons in it. I tried adding the subview to my ViewControllers however it will not respond to touches unless I set it to "initWithFrame:self.view.frame" but then it takes All of the touches and does not pass them to the view controller. I also tried adding it directly to the window so it's on top of all of the view however I have the same issue, either it will not accept touches or it takes them all.
here is how I add it when it takes all touches for itself and does not pass them on.
ControlView *cont = [[ControlView alloc]initWithFrame:self.window.frame];
[self.window addSubview:cont];
I'm so confused as to how to fix this and I have a lot more important tasks to work on but I have been stuck for two days with this stupid subview/touch issue.

How are you handling touches? UIGestureRecognizer?
You should make sure that your uiview subclass has userInteractionEnabled = YES. That's the default but maybe it got turned off somewhere along the line.

Related

iOS - Superview gesture recognizer getting called when not wanted, can't cancel child view touch event

I have a super view that has a UITapGestureRecognizer on it. It allows touches within the view because there are clickable items within the view.
When these items are clicked on, I want to take a specific action, not the generic one that covers the entire superview. Unfortunately in my TouchDown event of my child control I don't know how to stop the event here. I know I could create a kludge flag, but this seems like the wrong way to go.
Any advice?
James
OK I got a solution. Totally my problem. I was playing around with trying to get all touches to work and at one point I had set cancelTouchesInView = true on the UITapGestureRecognizer superview. While this didn't stop the other touches from happening, for whatever reason the touches carried through to the superview as well. I understand that this explanation probably makes no sense, but that's what did it. Still trying to wrap my head around how iOS does touch.

Making a UIView receive events after showing it (setting hidden = false)

I already tested the solution of setting up the alpha property from 0 to 1. However, this seems to be performance intensive operation.
What I want to do to make the UIView receive the touch events after showing it back by setting hidden = false
Is there a way to do that? I thought calling .becomeFirstResponder would solve this with no luck.
Yes, The first thing is mention by #wltrup that you have to enable userInteraction property of that view by this:
view.userInteractionEnabled = true;
and another is you have to add proper gesture recognizer on that UIView to perform specific events.

How to detect touch while presenting views with gesture recognizers?

I have been developing collage like application. In which user can add images, they can scale, move and rotate them. Whenever, I am trying to drag an image which is on top but added earlier than the one behind it, it is not receiving the gesture, rather than the one behind it which is added later gets detected and brought into front. How to resolve this issue?
I am Using this to remove the gesture in second view...
[imageView1 removeGestureRecognizer:GestureInView2];
This are the delegate method of UIGestureRecognizer you can use for your different requirement.
touchesBegan:withEvent:
touchesMoved:withEvent:
touchesEnded:withEvent:
I didn't understand your issue perfectly, But.
I think you have to refer for more detail according to your requirement - https://developer.apple.com/library/ios/documentation/EventHandling/Conceptual/EventHandlingiPhoneOS/GestureRecognizer_basics/GestureRecognizer_basics.html.
I have done it. We need to change the index of subviews if you want to make them receive touches. Changing zposition alone will not help to make the view to receive touches.

Touch events on subclass of UIView as a subview of UIScrollView

I have implemented my own custom subclass of UIView and overridden the drawRect: method.
In my custom view I also want the handle touches, so I also overridden touchesBegan, touchesMoved and touchesEnded.
This works fine but if the number of views on the screen increases then I have to use a UIScrollView as the root view of my UIViewController.
Once my custom UIView becomes the subview of UIScrollView, then it does not receive the touch events. Even though I move my finger within my custom UIView, the scroll view gets scrolled (all my touch events go to the UIScrollView).
How do I solve this problem?
There are several approaches you could try:
Try setting the below properties on the UIScrollView:
scrollView.delaysContentTouches = NO;
scrollView.canCancelContentTouches = NO;
See similar SO questions/answers here, here.
Implement hitTest:withEvent:. See here, here.
Use a UIGestureRecognizer. See here, here.
I would personally recommend using a UIGestureRecognizer, but it depends on your specific situation (any of these options may work fine for you).
Have a look at this response from another question: https://stackoverflow.com/a/4629821/193254
You'll have to subclass the scrollview too, and implement that hitTest: method.

disable scrolling in a UITableView (iPhone SDK 3.0)

I'm trying to disable scrolling in a UITableView when editing a UITextField embedded in a UITableViewCell.
This is just to prevent the cell from being scrolled out of sight when edited (and also to avoid some related cell "Recycling" problems).
While googling around I've seen that somebody suggested the obvious:
tableView.scrollEnabled = NO:
or even
tableView.userInteractionEnabled = NO;
This does not work though (at least for me... iPhone SDK 3.0, tried on simulator)
I set these properties to NO, I even check by logging that the properties are set to NO, but the UITableView keeps on responding normally to touch events.
And it also happily scrolls.
I wouldn't be that worried if somebody on the net were not claiming that this actually works.
Am I missing something?
Or is the only alternative subclassing UITableView to make a functionality available in its superclass (UIScrollView) work again?
Did you try using
self.tableView.scrollEnabled = NO;?
I've often tried that code from the web didn't work, simply because of a lack of the prefix self. I just tried this out without a problem.
I don't know if this work when turning it on and off dynamically. It does at least work for permanent settings when initializing the object...
If you're using UITableViewController, you also have a tableView property, with no casting needed. This works for me:
self.tableView.scrollEnabled = NO;
Let me know if that works for you.
Did you try on storyboard unselect scrolling enabled?
I tried:
[(UIScrollView*)[self view] setScrollingEnabled:NO];
and it worked ([self view] is my view of the current view controller, i.e., a UITableView).
The thing is, I get a warning:
'UIScrollView' may not respond to '-setScrollingEnabled:'
In all honesty, the property is "scrollEnabled", but it works nonetheless with the aforementioned code!
So, the "right" way to do things, should be:
[(UIScrollView*)[self view] setScrollEnabled:NO];
Why it also works the other way, is confusing me...
None of these answers worked in my case. Table view kept scrolling ever though every scrollView was disabled.
Finally, I've found solution in here, claiming that UITableViewController does this "for me" whenever keyboard hides the UITextView being edit.
Solution is to inherit from UIViewController instead of UITableViewController and implement the required table functionality myself.
if you want to scroll only if its content is not visible then set:
yourTableview.alwaysBounceVertical = NO;
Here if your content is visible then your tableview will not scroll

Resources