User Interaction on a UIImageView - iphone

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;

Related

Can a Tap Gesture Recognizer in IB be used on a UILabel and/or UIImageView

I have an xib with a single UIView containing a bunch of images and labels. If I drag a Tap Gesture Recognizer onto the parent view, it works well, but I need to add code to determine the position of the tap so I can respond properly to the label tapped.
I thought it would be a lot easier if I could instead drag Gesture Recognizers to each label and/or image and wire IBActions to each where I respond respond appropriately. Unfortunately I cannot get this to work. I can only get the delegate methods to execute from the parent view.
What I did was drag a Tap Gesture Recognizer to a label, then wired the view controller as the TGR's delegate, and wired an IBAction method to handle the tap. In addition to using the IBAction method I tried without succes assigning the target and action method in the gestureRecognizerShouldBegin:gestureRecognizer delegate method.
Shouldn't this work, or am I just wishfully thinking?
Thanks for any help.
John
On UIImages and UILabels, userInteractionEnabled is set to NO by default. You can use a gesture recognizer with both of them, but first you have to reset this property.
In Interface Builder or Storyboard, on the label or the image, check the box for userInteractionEnabled.
Or, in code, just do as follows (Objective-C):
myLabel.userInteractionEnabled = YES;
myImage.userInteractionEnabled = YES;
(Swift):
myLabel.userInteractionEnabled = true
myImage.userInteractionEnabled = true

Disabling/Enabling IBOutlet determines if I get touches or not?

I'm trying to overlay a transparent image over my app, with the purpose of explaining the controls of that view. When the UIImageView is first touched it should simply disappear. My MainViewController has the touchesBegan/Ended methods implemented and they work fine until I connect the UIImageView to an IBOutlet in MainViewController. Simply stop responding to events. What am I doing wrong?
-MainView
--ScrollView
--OtherView
--UIImageView (this is the overlay)
UIImageViews do not have user interaction turned on by default. If you want it on, either set the property via IB, User Interaction Enabled or via code...
[myImageView setUserInteractionEnabled:TRUE];

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.

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.