UIButton in CALayer - iphone

How can I add a UIButton in a CALayer and hookup the touch event?

A CALayer is not an event responder, so trying to hook it up to a touch event handler will do nothing.
If you want a button that actually works on top of a CALayer, put that CALayer into a UIView (which is a subclass of UIResponder), and add a UIButton to that view (so it can get added to the event response chain).

In iOS, all UIViews own and draw themselves through a CGLayer. You probably want to create a UIView for your button to go in. Everything you can do with raw CGLayers, you can do with UIViews.

Related

Touches not being recognized from webView on top of uiview

Currently I have subclassed webview, and added on top of another uiview. I have tried to debug touches event in subclass of webview, or another class where the webview is being added to uiview, but apparently, non of the touches event gets called, unless i set webview as [self.webView setUserInteractionEnabled:YES]; however, after doing this I cannot scroll or get any touch events to work. Any help here will be appreciated.
Thanks
UIWebView does a really good job of eating touches.. what I've done in this case is use a clear UIView to overlay the UIWebView (it is also possible to do an underlay if the WebView's userInteractionEnabled is off.
As long as your UIView and UIWebView have the same frames, you should be able to get the touches object from UIView and use its coords to do whatever you want to do to your UIWebView.
-D

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.

Why does touchesBegan stop working when UIImageView in placed inside a UIScrollView?

UIView -> UIImageView
I know I have things somewhat working ok since I can tap on my UIImageView and see an NSLog() statement in my touchesBegan method.
.
UIView -> UIScrollView -> UIImageView
I drag that same UIImageView into a UIScrollView and touchesBegan no longer gets called when I tap on my UIImageView. (I haven't changed anything else. All the same connections, methods, and code remains unchanged.)
Why does touchesBegan no longer work? And what can I do to get it working again?
Add uitapgesture to get event
Code is
UITapGestureRecognizer *ges11=[[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(Handeltap:)];
[imagename addGestureRecognizer:ges11];
Create one action name "Handeltap" U will get called there.
by default UIImageView don't handle user gestures.
set UIImageView instance's userInteractionEnabled to YES
Have a look at the documentation for UIScrollView.
Because a scroll view has no scroll bars, it must know whether a touch signals an intent to scroll versus an intent to track a subview in the content. To make this determination, it temporarily intercepts a touch-down event by starting a timer and, before the timer fires, seeing if the touching finger makes any movement. If the timer fires without a significant change in position, the scroll view sends tracking events to the touched subview of the content view. If the user then drags their finger far enough before the timer elapses, the scroll view cancels any tracking in the subview and performs the scrolling itself. Subclasses can override the touchesShouldBegin:withEvent:inContentView:, pagingEnabled, and touchesShouldCancelInContentView: methods (which are called by the scroll view) to affect how the scroll view handles scrolling gestures.
I'd also recommend reading the Scroll View Programming Guide.

User Interaction on a UIImageView

I have a number of UIImageView which have buttons on top of them.
I would like to enable user interaction on the UIImageView behind these buttons.
I see the option in IB, but would like to know how to trigger some code when the UIImageView is actually touched.
How does one do this and how is it set to enabled and disabled in the code rather than IB?
Thanks
how to trigger some code when the UIImageView is actually touched.
You have two options:
Create an instance of UITapGestureRecognizer (or another gesture recognizer), specifying a target and an action method. Then add the gesture recognizer to the image view with -[UIView addGestureRecognizer:]. Works in OS 3.2+.
Subclass UIImageView and override the -touches... methods. Make sure the image views you create are instances of your custom subclass.
See the documentation for details.
how is it set to enabled and disabled in the code rather than IB
Simple: imageView.userInteractionEnabled = YES;

Events are not Visible on UIImageView

I have a UIImageView, and a referencing outlet. However, altough I've set User Interaction of the ImageView, I cannot see any event in IB. Is it normal or am I missing something?
UIImageView is not a kind of UIControl, so you cannot set events to it. You have to use a UIButton with background, or subclass UIImageView and override the -touchesBegan: methods.