How to disable multitouch when having several views? - iphone

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.

Related

How do you disable touch to screen without using beginIgnoringInteractionEvents?

from a similar question/answer
You could always put a transparent UIView over top of the area you
want to "disable" tap input for, have it listen for taps, and have it
ignore them. Remove the UIView (or hide it) when you want input to be
listened to again.
Now, I can understand the strategy, but would someone enlighten me with a code?
How do you make a view
1. listen for taps
2. have it ignore them
Would it not pass touch to views behind it?
Set a plain UIView's userInteractionEnabled property to YES, but don't have any code in there to respond to touch events. The view will then 'swallow' all touches.

How to control the number of simultaneous touches on a view in iOS

I have a UIView which contains almost 10 buttons which performs different actions. The user will be able to touch or tap on any button to perform a required action. But, i am getting a problem when the user presses multiple buttons at the same time. When two buttons are pressed at the same time(simultaneously) two actions are getting performed. Some times the app is getting crashed. So, i dont want the user to tap multiple buttons simultaneously. Is there any way in which i can ask a view to recognize only single touch instead of multiple touches or at least can i increase the time gap between multiple touch gestures...
Use button.exclusiveTouch = YES; on each of your buttons. You will need to hook them up to UIButtons and set the property in viewDidLoad for example
This is a similar post, which has a nice solution. You essentially just hook up all the buttons to the same method, and have a switch statement. Hope that Helps!
Try yourView.multipleTouchEnabled = NO;
"yourView" here is the view contents all of your buttons.

handling same touch event with multiple control (parent and child)

I need to handle a touch event on my custom uiviewcotroller. I have a sub controller within the view that already handle touch event (it's a plot that handle zooming and scrolling).
I want to make a tabbar disappear when I tap the screen once. Actually it only works (even tought the tabbar doesn fade away but simply is no visible) in the areas in which the subcontrol is not present but I need it to work everywhere still handling the subcontrol events.
Make sure you're calling the superclass's event handler method in your event handler method to continue propagation of the event up the responder chain.
Also make sure the subcontrol's exclusiveTouch property is set to NO.
You might want to have a look at the event handling documentation.
Try to set userInteractionEnabled = NO in subcontrol view.
UPD: Try to add transparent button to subcontrol.

Drag & sweep with Cocoa on iPhone

I'm about to start a new iPhone app that requires a certain functionality but I'm not sure if it's doable. I'm willing to research but first I just wanted to know if I should at least consider it or not.
I haven't seen this in an app before (which is my main concern, even though I haven't seen too many apps since I don't own an iPhone), but an example would be the iPhone shortcuts panels: you can hold on an app, and then drag it to another panel, sweeping while still dragging it. But this is the core app, is it possible to reproduce something similar within a normal app?
I only need to be sure it can be done before I start digging, I don't need code examples or anything, but if you have some exact resources that you consider helpful, that would be appreciated.
Thanks!
Yes. If you have your custom UIView subclass instance inside a UIScrollView, your view controller just needs to set the UIScrollView to delay content touches and not allow it to cancel touch events.
[scrollView setCanCancelContentTouches:NO];
[scrollView setDelaysContentTouches:YES];
When the user taps and holds in the custom view, the event goes to that custom view, which can process the touch events to drag an item around, but if the user quickly swipes, it scrolls the view.
The "panel" view that you're referring to appears to be a UIPageControl view — although, perhaps, the specific incarnation of this view that Apple uses for the iPhone's home page may be customized.
Instances of generic UIView views that you might touch-and-drag will receive touch events. By overriding methods in the view, these events can be processed and passed to the page control, in order to tell it to "sweep" between pages.
If I wanted to do what you're asking about, that's how I might approach it. It seems doable to me, in any case.
Start with this: Swip from one view to the next view
Try using a UIButton that tracks the time since the state of the button changed to "highlighted". You may need to do this in order to track the dragging and move the button around:
Observing pinch multi-touch gestures in a UITableView
Check to see if the button starts overlapping one side of the screen while being dragged. If s certain amount of time elapses since the button first started overlapping the edge and then manipulate the UIScrollView so that it switches to the next page on the corresponding side of the screen
You may need to use NSTimer to keep track of how long the button is held down, etc.
In any case there's no reason why this couldn't work.
If UIButton doesn't work then perhaps try a custom subclass of UIControl (which tracks the same touch down actions etc.). If that doesn't work then use the window event intercept thing to track everything.

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.