iPad - UIImageView respond to touch - ipad

In MonoTouch how do I get a UIImage or UIImageView to fire off a delegate or something when clicked?

Exactly as Dimitris pointed out, you'll want to subclass UIImageView, and also ensure that the userInteractionEnabled flag is set to true.
I should point out that in most cases where people intend to have a UIImageView respond to interaction, these problems can just as easily be fixed by creating a UIButton instance with an image instead.

You have to subclass it (the UIImageView) and override one of TouchesBegan, TouchesMoved or TouchesEnded methods, according to what you want to do.

Related

Using UIImageView instead of UIButton is not good?

I often UIImageView when you tap some actions will be excused instead of UIButton. Because if you use UIButton, it isn't excused until you release your finger. But if you use UIImageView, the action will be excused the moment you touch UIImageView. But I'm wondering whether it is good way to make "UIButton" or not because when I use UIImageView, I set the tag of UIImageView, use touchesBegan, and check which UIImageView you tap. I'm not sure the way it good or not.
Do you use UIButton when you want to do some actions when you touch the object? And what do you think about it?
(I'm sorry I'm a beginner iOS engineer so it might be idiot question.)
Because if you use UIButton, it isn't excused until you release your
finger.
This is true if you have a function that is triggered at the event touchUpInside. If you use the event touchUpDown, the function will get triggered right after the user tapped the button. See here all the events: UIButton events. What's the difference?
I can easily be done using UIButton, you just need to use different controlEvents, for instance like so
yourButton.addTarget(self, action: #selector(yourSelector), for: .touchDown)
Try it from .xib or .storyboard file. I think it will help you.

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.

Using a UIView as a button

I'm trying to use a UIView I've created in Storyboard as a button. I assumed it would be possible to use a UIButton, setting the type to custom. However I was unable to add subviews to a custom UIButton in Storyboard.
As such I've just spent the last hour reinventing the wheel by making my own custom gesture recoginizers to reimplement button functionality.
Surely this isn't the best way of doing it though, so my question - to more experienced iOS developers than myself - is what is the best way to make a custom button?
To be clear it needs to:
Use the UIView I've created as it's hittable area.
Be able to show a
different state depending on whether is currently highlighted or not
(i.e. touch down).
Perform some action when actually tapped.
Thank you for your help.
You can use a UIButton, set the type to custom, and then programmatically add your subviews...
Change your UIView into a UIControl in the storyboard. Then use the method [controlViewName addTarget:self action:#selector(*click handler method*) forControlEvents:UIControlEventTouchDown];. click handler method is a placeholder for the method name of your handler. Use this method except change out the UIControlEventTouchDown for UIControlEventTouchInside and UIControlEventTouchDragExit to call a method when the user finishes their click and drags their finger out of the view respectively. I used this for something I'm working on now and it works great.
In Touch down you will want to: highlight all subviews
In Touch up inside you will want to: unhighlight all subviews and perform segue or do whatever the button is supposed to do
In Touch Drag Exit you will want to: unhighlight all subviews
See second answer by LiCheng in this similiar SO post.
Subclass UIControl instead. You can add subviews to it and it can respond to actions
Why are you implementing your own GestureRecognizer? I recommend using the UIView so you can add subviews in the interface builder and adding a UITapGestureRecognizer. You can even do this graphically since you don't care about IOS4 support.

Custom UIView to detect user touches

I'm developing an iOS 4.0 application.
I want to add a custom UIView over an image (UIImageView) to detect when user touches on some image's parts. I don't want to subclass UIImageView because I'm using SDWebImage package (or maybe I can subclass UIImageView+WebCache class to handle touches. If you tell me how).
This UIImageView will be inside an UIScrollView.
My idea is to detect when a user touches the image and notify viewController where user has touched. I don't need to detect moves or swipes or multitouches. Only one finger touch.
I'm creating my own UIView subclass to detect that using methods touchesBegan: touchesEnd:, etc. But I'm not sure if this is the best way to do that.
What do you think? Do you know a better approach?
You can add tapGestureRecognizer to your imageView in viewDidLoad, and then in handler method use CGPoint gestureLocation = [gestureRecognizer locationInView:self.imageView]; to check that tap was in selected area.

UITextField subview of UIImageView is not responding to touch events

I have a UITextField that responds to touch events when it is added as a subview of self.view. When I add it as a subview of a UIImageView it does not respond to touch events any more. I make it a firstResponder and the field is active and takes keyboard input still. Any ideas what is wrong?
The docs for UIImageView state:
New image view objects are configured to disregard user events by default. If you want to handle events in a custom subclass of UIImageView, you must explicitly change the value of the userInteractionEnabled property to YES after initializing the object.
Try setting imgView.userInteractionEnabled = YES
If this doesn't work, I would try adding the UIImageView and the UITextField in a UIView instead. Anyway, this approach seems cleaner.

Resources