Why is my window subview showing, but not handling any touches? - iphone

I have my UIWindow structured in Interface Builder as:
window
drawingView (custom UIView)
toolbar (UIToolbar)
Where the drawingView handles touches using touchesBegan etc. However, when I try and add 'someView' (a custom UIView) which has UIButtons on it as a subview of window, the buttons don't receive any of the touches.
I create 'someView' with:
[[[UIApplication sharedApplication] keyWindow] addSubview:someView];
'someView' displays fine on top of the drawingView, but the touches don't seem to register at all, and get passed through to drawingView. Why is that?
Another odd thing is that the backgroundColor of someView always seems to be clear, even if I set it to something else programatically or in IB.
Also, when I create someView using
[[UIPopoverController alloc] initWithContentViewController:someView];
it works fine, handling touches and all. The reason I want to add it as a subview is because I want a more general way of adding someView, that is works on the iphone as well.

Make sure the userInteractionEnabled flag is set on all parents of the view that needs to receive touches.

I've figured out a fix (in a hackish kind of way).
I had to add 'someView' as a subview of drawingView, which appears on top of drawView when shown, and allows me to programatically set up someView with UIButtons, which receives touches.
I think it's because drawView is an OpenGL view, that overrides layerClass which does something funky with how the views are arranged to obtain touches. I think.

Related

How to pass a 'tap' to UIButton that is underneath UIView with UISwipeGestureRecognizer?

I have a UIButton underneath a (transparent) UIView. The UIView above has a UISwipeGestureRecognizer added to it, and that is its only purpose - to detect certain swipe gestures. I want all other touches to be ignored by that UIView, and passed to other views (such as my UIButton underneath). Currently, the UIView above seems to be detecting the tap (for example), doing nothing (as it should be), and not letting the UIButton underneath get a chance to respond.
I would prefer not to implement my own swipe recognizer, if possible. Any solutions / advice? I basically just want to know how to tell a UIView to pay attention to only a certain type of added gesture recognizer, and ignore (and thus let through to views behind) all other touches.
Have you set:
mySwipeGesture.cancelsTouchesInView = NO;
to allow the touches to be sent to the view hierarchy as well as the gesture?
Additionally, ensure that the view on top is:
theTransparentView.opaque = NO;
theTransparentView.userInteractionEnabled = YES;
I've had pretty good success attaching gestures to the parent view without needing to create a transparent subview on top for the gesture. Are you sure you need to do that?
I must have just been in a funk yesterday - I woke up with a simple solution today. Add the UISwipeGesture to a view which is a superview to both the UIView and the UIButton. Then, when processing those swipes, figure out where the swipe originated, and whether that point is in the frame of where I used to have the UIView. (As I mentioned, the only reason for the existence of the UIView was to define a target area for these swipe gestures.)
Can't you put your button on top of the view and add gesture recognisers to that button too?
In the end, your UIButton inherits form UIView via UIControl. Therefore there is practically nothing that you could do with a view but not with a button.
In my case, I fixed it by not using a button, but rather a UITapGestureRecognizer. My pan gesture recognizer was added to the background view, and the tap gesture was added to a view above it.

Drag UIView without disabling its child controls iPhone/iPad

I have a UIView control (white rectangle in image)
Moreover I am able to drag that control...
And when I press the button I load a subview which is another nib that I created and I placed random controls in it to illustrate my point...
If you guys are interested in finding out how I placed that nib file in that UIView control take a look at this question. I don't thing you have to read it in order to understand my question.
Anyways the problem when loading that nib file is that I can no longer drag the top UIView. Because of this I changed:
for:
in the UIView of the subview. In otherwords the UIView of the nib file that I am placing in the UIView that has the white background.
and when I did that I was able to drag the control but the controls inside the subview no longer works. I have also tried placing the touchesMoved method in the subview instead but when I do that the application behaves strange. Plus the purpose of placing the nib file in a UIView control was to avoid repeating the same drag funcionality on several nib files.
I actually need to create an application like a power point presentation and I need to change the slide as the user slides the UIView and if it's cords are less than x for example then I load the next slide (nib file) in that uiview controller. Maybe there is a simpler way of doing what I need but if I get this drag to work I am done cause I would just have to do that functionality just once.
You should leave the UserInteractionEnabled flag on for your subview if you want it to respond to events.
One way to achieve this would be to do your dragging using a UIGestureRecognizer.
UIPanGestureRecognizer is perfect for this (UIGestureRecognizer at apple)
Basically you'd attach the gesturerecognizer to the view which you want to pan then adjust it's position in the callbacks it provides.
UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc]
initWithTarget:self action:#selector(handlePanGesture:)];
panGesture.minimumNumberOfTouches = 1;
[draggableSubview addGestureRecognizer:panGesture];
[panGesture release];
Then in the handlePanGesture method you figure out how far the user panned using the translationInView method of the recognizer which it gets passed and translate the subview accordingly.

how does a subview of a scrollview know the scrollview is being scrolled

I placed a few uiimageview objects into a scrollview. How can the imageview know the scrollview is being scrolled? Since the imageview is a subview of the scrollview i can't set the scrollview delegate to the imageview.
I want to create something similar to the apps view on the iphone. Where you can hold down an app and then drag it, but if you hold and move your finger too far to the left or right the action is stopped and scrolling takes over.
"Since the imageview is a subview of the scrollview i can't set the scrollview delegate to the imageview."
And why not?
You have viewcontroller which shows the particular scrollview correct? this viewcontroller should be the delegate of the scrollview, and viewcontroller should also hold pointers (either explicitly in an array or through tag of some sort) to the uiimageviews.
Then, whenever scrollview notifies viewcontroller (through scrollviewdidscroll type of delegate method), implement your logic to update uiimageview.
To implement the exact touch sequence, you can subclass UIGestureRecognizer or write your own touch handler.. take a look at Apple's documentation on Event Handling Guide :
http://developer.apple.com/library/ios/#documentation/EventHandling/Conceptual/EventHandlingiPhoneOS/Introduction/Introduction.html#//apple_ref/doc/uid/TP40009541
It doesn't, it just draws and draws. The scrollView makes sure you only see the part you really want to see
You might want to look at three20, they have a class that is very similar to the springboard. -- Used in facebook.app
Thanks Everyone, but I found out that UIScrollView sends the touchesCancelled message to the subview when scrolling starts.

How to detect tap on uiview with lots of controls?

My problem is about tap detection.
I have a uiviewcontroller and there are some controls on uiview (labels, buttons, tableview, imageview, etc..)
When I tap the uibutton I display a small uiview (200x150), if the user taps the uibuttons in smallview I hide the smallview.
But I can't hide the uiview if the user taps the background.
I tried this code..
-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
//NSLog(#"Touches began.");
[self hideShareView];
}
It doesn't work if I tap the another button in the uiviewcontrols view.
I just want my uiviewcontrol's uiview to react first.
I think its about firstResponder but I dont know how to set it first.
edit: i want it to work like a uiPopover in ipad.
I believe that the correct approach is to add a new transparent UIView when you display your "smallview". You should add a UITapGestureRecognizer to that UIView, in order to trigger the desired selector when the tap is detected. Also, you must ensure that the views are arranged properly, with your smallview being the one at the top, the transparent UIView being immediately below and the rest of the view hierarchy below the transparent UIView.
Finally, you should ensure to remove the transparent UIView from your view hierarchy at the same time that you remove your smallview.
Does that make sense?
Please have a look at this question (how-to-make-a-superview-intercept-button-touch-events), it does seem highly related.
try your hands with bringing your small view (i.e. shareview) to front or sent your main view behind your small view.
If it still doesn't work & you don't want your main view to perform any action when smallview is opened then try
[<YOUR_MAIN_VIEW> setUserInteractionEnabled:NO];
, but MAKE SURE you can do this only when you don't want your main view to perform any action when smallview is opened

UIView Subview Does not autoresize on orientation change

In my iPhone app, I have a view controller with two views (essentially, a front & back view). The front view is the main UIView, and the back view is a secondary UIView which is added as a subview using [self.view addSubview:backView] when showing the back and [backView removeFromSuperview] when hiding it. However, when the orientation changes, I have the following issue: the main UIView (frontView) rotates & all of its elements resize properly, but the secondary/subview UIView (backView) does not rotate & all of its elements do not resize properly. Does anyone have suggestions on how to make the secondary UIView autoresize properly according to the rules I have set in Interface Builder?
In the end, the solution I found was simply to separate my UIViews into separate UIViewControllers, and make sure that any views that I wanted to be able to rotate only had one UIView.
If I understand correctly, at the time of rotation 'backView' has been removed from it's superview, yeah? If so, that's the cause of the problem. The autoresize property determines how the view resizes relative to it's superview. If it doesn't have a superview it won't resize.
Perhaps using [backView setHidden:YES] instead of [backView removeFromSuperview] will be sufficient for your needs.
I had the same problem, here is how I fixed it based on imaginaryboy's
suggestions (thanks!)
Add the backview to the viewcontroller at viewDidLoad and hide it at the same time. Show it when needed, Hide it again. Set the resizing of the backview to UIViewAutoresizingFlexibleWidth in IB (or code I guess, I used IB)
Not that this is the same problem, but I found a similar problem when adding 2 subviews in my application:didFinishLaunchingWithOptions method. Since your reference above is using [self.view addSubview:view], I would understand that to mean that self is not your UIWindow. When adding an additional view controller to your App Delegate window (UIWindow), the second view controller will NOT receive any rotation events and will never rotate. Only the first view controller added to UIWindow will rotate. See:Technical Q&A QA1688 I believe this also affects views added after the first view where the first view is later removed from the superview.
I ended up following the suggestion I read elsewhere to use separate views for each orientation, thereby eliminating the need to worry about resizing behavior. As always, YMMV.
Or; if you want to avoid an additional controller, you can achieve the same effect by setting view.frame in willRotateToInterfaceOrientation:: like so
if(UIInterfaceOrientationIsLandscape([[UIApplication sharedApplication] statusBarOrientation])) ;//set stubborn view.frame for landscape orientation
else ; //set stubborn view.frame for portrait orientation
Although it feels like a hack; it's simple.