Deselect UIButton on touch down outside - iphone

I have a UIButton that selects itself on UIControlEventTouchUpInside. It deselects itself on UIControlEventTouchUpOutside. I also want it to deselect itself when there is a touch down outside of the button. Is there a good way to do this without subclassing UIWindow and overriding -hitTest:withEvent:?
EDIT: I just found this question, which confirms my fear that there isn't a really clean way to do this.

This is not how buttons are supposed to work. They cannot be active if there is no touch active inside them. What are you really trying to implement? Maybe a UIBUtton is not the best choice here.

You can do this with your UIViewController by implementing the touchesBegan/Moved/Ended methods. If they detect a touch outside of the button, set its state back to normal. Take a look at Event Handling.

I solved this, and released the source. I didn't have to subclass UIWindow, but I did need to override -hitTest:withEvent:.

Related

How to disable multitouch when having several views?

I created my own custom view that extends UIControl. This custom view has its own touch implementation. I implemented touchesBegan, Moved, Ended, and Canceled methods in it.
In the main view controller, I created several instances of this view. So in the screen, there are a number of custom buttons.
I want to disable multitouch in my app. If I click one custom button, then other buttons shouldn't respond.
Actually, it was easy to implement this. While I held some buttons, I could just make other buttons' userInteractionEnabled property to NO until I ended touch.
But the problem is that when I tap these several buttons at the same time, two or more touchesBegan methods work simultaneously, and message passing is messed up.
I tried to set multiTouchEnabled = NO and exclusiveTouch = YES, but it still didn't work.
How can I force to disable multitouch in my app?
Thank you.
You need to set exclusiveTouch to YES, not NO (which is the default anyways). The name of the property refers to the view being the exclusive recipient of any touch events for the duration.

UIGestureRecognizer blocking buttons in table cell views

I am using a custom UIGestureRecognizer on a UITableView.
As soon as I add the GestureRecognizer to the view, the buttons inside my table cells stop working (although changing the visible state to pressed, no action is being called).
And I am using [self setCancelsTouchesInView:NO]; and no recognizer chaining or what-so-ever.
Any ideas anyone?
Best regards,
Alex
It is possible that you are running into an issue with delaysTouchesEnded. Setting it to NO may fix your problem. Have a look at the docs.
http://developer.apple.com/library/ios/#documentation/UIKit/Reference/UIGestureRecognizer_Class/Reference/Reference.html

How do I add a touch event to a subview of UIView?

I'm trying to build by very first app so excuse my ignorance. I love the sample code that Apple put together here called ViewTransitions:
http://developer.apple.com/iphone/library/samplecode/ViewTransitions/Introduction/Intro.html
I want the transition to occur after a user taps the screen once instead of the button like the code does.
Thanks!
Change the inherited class of your xib to be UIControl.
Implement the touch events in your view.

Touch events not working on UIViews inside UIScrollView

I have a series of UIViews inside a UIScrollView, and the UIViewControllers for those views are not receiving the touch events. If I take the views out of the scroll view then it works.
I have enabled userInteraction on the views but it's still not working!
This must be possible and I'd be really grateful if someone could point me in the right direction!
Thanks,
Mike
Do the views have their own touch handlers, or are you relying on the viewcontroller to get the touches? Depending on how you have set things up, the views may be handling the touches without passing through to the view controller.
I have overcome this issue by overriding the loadView method of the view controller, and setting the view's instance variable to a simple UIView subclass which passes on the touches.
Check what you are returning in scrollview delegate method view for scrollin in scroll view.
As mahboudz mentioned - check if you have any custom handler for touch event. If not, please have one. Its far more relief to do whatever you want to do with your view. Check out Apples sample app from scrollViewSuite. They have tapDetectingImageView delegate. I used the same in my app it worked great! Hope this helps!
You may find this post useful. It's an example of a pretty clean way of intercepting events.
Have touch handlers for view for which you want to receive touch events and that will work.

How do I associate events with an image view?

I have an app with an image view. When the user clicks on this view I want to run some code which will change the colour of a label and then hide this view
I have everything setup in the interface, i.e. Outlets etc, but I dont see any events available to associate
Can anyone help or point me in the direction of a good tutorial asap please?
Cheers
Paul
You shouldn't use an image view for this, you should use a UIButton. ImageViews are designed primarily to display, not allow for interaction (which is why their userInteractionEnabled flag is OFF by default.)
Take a look at the documentation for UIResponder - all UIViews, including UIImageView, inherit from it. In turn, UIResponder defines a set of methods you can override to handle the kind of event you're looking at. Start by subclassing UIImageView (call it MyImageView), then override the touchesEnded:withEvent: method.
As Ben Gottlieb said, however, this may be kind of an abuse of the UIKit framework - just make sure the user interaction you're creating makes sense and conforms to good UI practices.
The last time I needed to do this I just used a button also. You can always set up the highlighted state so that it doesn't highlight when someone taps on it if that's what you're trying to avoid.
Note that the higher level UI "actions" are implemented in UIControl, so if all you need to do is track actions like "Touch Up Inside" then it's possible to avoid the event layer, create a UIView in Interface Builder, then change the class to UIControl. You should then be able to use the connections inspector and connect any of those control actions to whatever.
If you place views inside the UIControl and the subviews have "User Interaction Enabled" unchecked (that is, user interaction is disabled) then taps and such inside the UIControl just ignore those and fall through to the UIControl. So another way to do this if you needed to for some reason would be to create a UIView, change the class to UIControl, then place one or more UIImageViews, UILabels, or whatever you want in your generic UIControl view. You can then get actions from the generic UIControl as if all that stuff wasn't inside it.