Stopping a UIGestureRecognizer from calling selector - iphone

I'm adding in a few UIGestureRecognizers with a target and selector. I'll just talk about one since the other will be the same i'm sure.
I've added a UIPinchGestureRecognizer
UIPinchGestureRecognizer *pinch = [UIPinchGestureRecognizer new];
[pinch addTarget:self action:#selector(pinchGestureDetected:)];
[self.view setMultipleTouchEnabled:YES];
[self.view addGestureRecognizer:pinch];
Now my goal here is to simply call this method once when I receive a pinch gesture. But obvoiusly it continues to call it as the person pinches. I'm using it as part of a page navigation and will be updating the view when a pinch in is detected.
So in my -(void)pinchGestureDetected:(UIPinchGestureRecognizer)pinch method I'll be calling another method. Kinda like ... and this is a little sudo
-(void)pinchGestureDetected:(UIPinchGestureRecognizer)pinch
{
if (pinch.scale > 1) layoutViewWithMoreDetail;
else layoutViewWithLessDetail;
}
So I don't want it to keep calling this method or the layout method will continue to be called. I want one layout / pinch gesture.
Is there a way I can stop detecting the pinch once it has determined the scale?? Something along the way of ...
-(void)pinchGestureDetected:(UIPinchGestureRecognizer)pinch
{
if (pinch.scale > 1)
{
layoutViewWithMoreDetail;
stop receiving pinch gestures till this.gesture is finished;
}
Would I impliment the GestureDelegate??
-(void)pinchGestureDetected:(UIPinchGestureRecognizer)pinch
{
if (pinch.scale > 1 && pinching == NO )
{
layoutViewWithMoreDetail;
pinching = YES;
}
and then in the delegate for Gesture ended ... pinching = NO;
Thanks for any help

UPinchGestureRecognizer is a continuous gesture - use if(pinch.state == UIGestureRecognizerStateBegan) to detect whether or not the event is just now starting.
However, this results in a low threshold for triggering the event. An alternate method is to quicky disable and enable a gesture when it has been triggered to your satisfaction, like so:
-(void)pinchGestureDetected:(UIPinchGestureRecognizer)pinch
{
if (pinch.scale > 1)
{
//do your stuff here
pinch.enabled = NO;
pinch.enabled = YES;
}
}
This is because if you look at the documentation, it states:
enabled
A Boolean property that indicates whether the gesture recognizer is enabled.
#property(nonatomic, getter=isEnabled) BOOL enabled
Discussion
Disables a gesture recognizers so it does not receive touches. The default value is YES. If you change this property to NO while a gesture recognizer is currently recognizing a gesture, the gesture recognizer transitions to a cancelled state.

Related

UIGestureRecognizerStateBegan calling twice in iOS 6

Giving thanks in advance, I would like to share the strange behaviour of UILongPressGestureRecognizer.
UIView *v = [UIView alloc] initWithFrame:CGRectMake(0,0,20,20)];
UILongPressGestureRecognizer *longpressGesture1 = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:#selector(tapped:)];
[v addGestureRecognizer:longpressGesture1];
and here the delegate to handle the gesture recogniser.
-(IBAction)tapped:(UILongPressGestureRecognizer *) gesture
{
switch ([gesture state]) {
case UIGestureRecognizerStateBegan:
{
NSLog(#"Long Tap detacted.");
}
break;
case UIGestureRecognizerStateChanged:
{
NSLog(#"UIGestureRecognizerStateChanged");
}
break;
case UIGestureRecognizerStateEnded:
{
NSLog(#"Long Tap ended.");
}
break;
}
}
This piece of code is working perfectly as expected in iOS 4 and 5, but in ios 6 with retina display when we perform the long tap, UIGestureRecognizerStateBegan is being called twice for a single long tap resulting in a application crash.
Any help is greatly welcome.
UILongPressGestureRecognizer is a continuous event recognizer. You have to look at the state to see if this is the start, middle or end of the event and act accordingly.
Its calling two times because you are pressing and removing your finger.
First call is indicating you that there is an Long tap detected i.e. UIGestureRecognizerStateBegan
Second call is indicating you that there is end of that tap i.e. UIGestureRecognizerStateBegan
There are three state of tap
UIGestureRecognizerStateBegan
UIGestureRecognizerStateChanged
UIGestureRecognizerStateEnded
If you will drag your finger then it will called multiple time that will indicate that there is some changes in its state.
Follow UILongPressGestureRecognizer Class Reference for more

How to create touch events for MKMapView?

How to create Touch Events for MKMapView.
I'm using UIViewController and adding MKMapView on that using interface builder.
Now I need to handle touch events for that map.....
I tried by writing UITouch Delegate methods
But I failed...It is not getting called.
Please post a solution how to handle touch events on MKMapView.....
Thanks in advance...
If you are happy with an iOS 4 and above solution, I've used UIGesture recognisers and never had a problem.
Here's an example for a long pressure gesture (tap and hold):
// Long press gesture recogniser
UILongPressGestureRecognizer *longPressGesture = [[UILongPressGestureRecognizer alloc]
initWithTarget:self
action:#selector(handleLongPressGesture:)];
[self.view addGestureRecognizer:longPressGesture];
[longPressGesture release];
And then you can handle the even in your handleLongPressGesture: method:
-(void)handleLongPressGesture:(UILongPressGestureRecognizer*)sender
{
if (sender.state == UIGestureRecognizerStateEnded || sender.state == UIGestureRecognizerStateChanged)
return;
else {
// Your app logic here...
}
}

detecting long tap on iPhone

I am working on an iPhone app which requires me to check if the button has been tapped & held pressed for 6 seconds & then fire an action which is playing some sort of sound.
How should I detect this 6 second tap?
On the other hand the user can also keep on tapping button for 6 seconds & then the same action should fire.
What should I do with multiple taps, how would I know that all the taps fall under the 6 second bracket?
For a six second long press, use a UILongPressGestureRecognizer with its minimumPressDuration property set to 6.
Write your own gesture recognizer (say, LongTappingGestureRecognizer) for continuous tapping for a given period; it shouldn't be too tricky. Give it a property like UILongPressGestureRecognizer's minimumPressDuration (say, minimumTappingDuration) and a property (say, maximumLiftTime) that determines how long a finger can be lifted off before it's not considered to be a long tapping gesture.
When it first receives touchesBegan:withEvent:, record the time.
When it receives touchesEnded:withEvent:, start an NSTimer (the lift timer) that sends the gesture recognizer a cancel message (e.g. cancelRecognition) after maximumLiftTime.
When it receives touchesBegan:withEvent: when there's a start time, cancel the lift timer (if any).
The cancelRecognition will transition to the failed state.
There are various strategies for handling recognizing when the end of the gesture is reached, after minimumTappingDuration. One is to check in both the touchesBegan:withEvent: and touchesEnded:withEvent: handlers if the difference between the current time and the start time is >= minimumTappingDuration. The problem with this is that it will take longer than minimumTappingDuration to recognize the gesture if the user is tapping slowly and hir finger is down when the minimumTappingDuration is reached. Another approach is to start another NSTimer (the recognition timer) when the first touchesBegan:withEvent: is received, one that will cause transition to the recognized state and that is cancelled in cancelRecognition. The tricky thing here is what to do if the finger is lifted when the timer fires. The best approach might be a combination of the two, ignoring the recognition timer if the finger is lifted.
There's more to the details, but that's the gist. Basically, it's a long press recognizer that lets the user lift hir finger off the screen for brief periods. You could potentially use just the tapping recognizer and skip the long press recognizer.
I realize this is quite dated question, however answer should be pretty simple.
In your View controller viewDidLoad:
//create long press gesture recognizer(gestureHandler will be triggered after gesture is detected)
UILongPressGestureRecognizer* longPressGesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:#selector(gestureHandler:)];
//adjust time interval(floating value CFTimeInterval in seconds)
[longPressGesture setMinimumPressDuration:6.0];
//add gesture to view you want to listen for it(note that if you want whole view to "listen" for gestures you should add gesture to self.view instead)
[self.m_pTable addGestureRecognizer:longPressGesture];
[longPressGesture release];
Then in your gestureHandler:
-(void)gestureHandler:(UISwipeGestureRecognizer *)gesture
{
if(UIGestureRecognizerStateBegan == gesture.state)
{//your code here
/*uncomment this to get which exact row was long pressed
CGPoint location = [gesture locationInView:self.m_pTable];
NSIndexPath *swipedIndexPath = [self.m_pTable indexPathForRowAtPoint:location];*/
}
}
Here is my solution.
- (IBAction) micButtonTouchedDownAction {
self.micButtonTimer = [NSTimer scheduledTimerWithTimeInterval:0.5 target:self selector:#selector(micButtonAction:) userInfo:nil repeats:YES];
self.micButtonReleased = FALSE;
}
- (IBAction) micButtonTouchedUpInsideAction {
self.micButtonReleased = TRUE;
}
- (IBAction) micButtonTouchedUpOutsideAction {
self.micButtonReleased = TRUE;
}
- (void) micButtonAction:(NSTimer *)timer {
[self.micButtonTimer invalidate];
self.micButtonTimer = nil;
if(self.micButtonReleased) {
NSLog(#"Tapped");
}
else {
NSLog(#"Touched");
}
}

How to Recognize a Hold Button {iPhone SDK}

Hi i have a Button i want hold this button to write something but i don't know how can i recognize hold button , can you help me ? thank you
TouchDownInside event triggered, start a NStimer.
TouchUpInside event triggered, cancel the timer.
Make the timer call your method to execute if the user holds the button : the timer delay will be the amount of time required to recognize hold.
You can also use UILongPressGestureRecognizer.
In your initialization method (e.g. viewDidLoad), create a gesture recognizer and attach it to your button:
UILongPressGestureRecognizer *gesture = [[UILongPressGestureRecognizer alloc]
initWithTarget:self
action:#selector(myButtonLongPressed:)];
// you can control how many seconds before the gesture is recognized
gesture.minimumPressDuration = 2;
// attach the gesture to your button
[myButton addGestureRecognizer:gesture];
[gesture release];
The event handler myButtonLongPressed: should look like this:
- (void) myButtonLongPressed:(UILongPressGestureRecognizer *)gesture
{
// Button was long pressed, do something
}
Note that UILongPressGestureRecognizer is a continuous event recognizer.
While the user is still holding down the button, myButtonLongPressed: will be called multiple times.
If you just want to handle the first call, you can check the state in myButtonLongPressed::
if (gesture.state == UIGestureRecognizerStateBegan) {
// Button was long pressed, do something
}

How to disable multitouch?

My app has several buttons which trigger different events. The user should NOT be able to hold down several buttons. Anyhow, holding down several buttons crashes the app.
And so, I'm trying to disable multi-touch in my app.
I've unchecked 'Multiple Touch' in all the xib files, and as far as I can work out, the properties 'multipleTouchEnabled' and 'exclusiveTouch' control whether the view uses multitouch. So in my applicationDidFinishLaunching I've put this:
self.mainViewController.view.multipleTouchEnabled = NO;
self.mainViewController.view.exclusiveTouch = YES;
And in each of my view controllers I've put this in the viewDidLoad
self.view.multipleTouchEnabled = NO;
self.view.exclusiveTouch = YES;
However, it still accepts multiple touches. I could do something like disable other buttons after getting a touch down event, but this would be an ugly hack. Surely there is a way to properly disable multi-touch?
If you want only one button to respond to touches at a time, you need to set exclusiveTouch for that button, rather than for the parent view. Alternatively, you could disable the other buttons when a button gets the "Touch Down" event.
Here's an example of the latter, which worked better in my testing. Setting exclusiveTouch for the buttons kind-of worked, but led to some interesting problems when you moved your finger off the edge of a button, rather than just clicking it.
You need to have outlets in your controller hooked up to each button, and have the "Touch Down", "Touch Up Inside", and "Touch Up Outside" events hooked to the proper methods in your controller.
#import "multibuttonsViewController.h"
#implementation multibuttonsViewController
// hook this up to "Touch Down" for each button
- (IBAction) pressed: (id) sender
{
if (sender == one)
{
two.enabled = false;
three.enabled = false;
[label setText: #"One"]; // or whatever you want to do
}
else if (sender == two)
{
one.enabled = false;
three.enabled = false;
[label setText: #"Two"]; // or whatever you want to do
}
else
{
one.enabled = false;
two.enabled = false;
[label setText: #"Three"]; // or whatever you want to do
}
}
// hook this up to "Touch Up Inside" and "Touch Up Outside"
- (IBAction) released: (id) sender
{
one.enabled = true;
two.enabled = true;
three.enabled = true;
}
#end
- (void)viewDidLoad {
[super viewDidLoad];
for(UIView* v in self.view.subviews)
{
if([v isKindOfClass:[UIButton class]])
{
UIButton* btn = (UIButton*)v;
[btn setExclusiveTouch:YES];
}
}
}
- (void)viewDidLoad {
[super viewDidLoad];
for(UIView* v in self.view.subviews)
{
if([v isKindOfClass:[UIButton class]])
{
UIButton* btn = (UIButton*)v;
[btn setExclusiveTouch:YES];
}
}
}
This code is tested and working perfectly for me.there is no app crash when pressing more than one button at a time.
Your app crashes for a reason. Investigate further, use the debugger, see what's wrong instead of trying to hide the bug.
Edit:
OK, ok, I have to admit I was a bit harsh. You have to set the exclusiveTouch property on each button. That's all. The multipleTouchEnabled property is irrelevant.
To disable multitouch in SWIFT:
You need first to have an outlet of every button and afterwards just set the exclusive touch to true.Therefore in you viewDidLoad() would have:
yourButton.exclusiveTouch = true.
// not really necessary but you could also add:
self.view.multipleTouchEnabled = false
If you want to disable multi touch throughout the application and don't want to write code for each button then you can simply use Appearance of button. Write below line in didFinishLaunchingWithOptions.
UIButton.appearance().isExclusiveTouch = true
Thats great!! UIAppearance
You can even use it for any of UIView class so if you want to disable multi touch for few buttons. Make a CustomClass of button and then
CustomButton.appearance().isExclusiveTouch = true
There is one more advantage which can help you. In case you want to disable multi touch of buttons in a particular ViewController
UIButton.appearance(whenContainedInInstancesOf: [ViewController2.self]).isExclusiveTouch = true
Based on neoevoke's answer, only improving it a bit so that it also checks subviews' children, I created this function and added it to my utils file:
// Set exclusive touch to all children
+ (void)setExclusiveTouchToChildrenOf:(NSArray *)subviews
{
for (UIView *v in subviews) {
[self setExclusiveTouchToChildrenOf:v.subviews];
if ([v isKindOfClass:[UIButton class]]) {
UIButton *btn = (UIButton *)v;
[btn setExclusiveTouch:YES];
}
}
}
Then, a simple call to:
[Utils setExclusiveTouchToChildrenOf:self.view.subviews];
... will do the trick.
This is quite often issue being reported by our testers. One of the approach that I'm using sometimes, although it should be used consciously, is to create category for UIView, like this one:
#implementation UIView (ExclusiveTouch)
- (BOOL)isExclusiveTouch
{
return YES;
}
Pretty much simple you can use make use of ExclusiveTouch property in this case
[youBtn setExclusiveTouch:YES];
This is a Boolean value that indicates whether the receiver handles touch events exclusively.
Setting this property to YES causes the receiver to block the delivery of touch events to other views in the same window. The default value of this property is NO.
For disabling global multitouch in Xamarin.iOS
Copy&Paste the code below:
[DllImport(ObjCRuntime.Constants.ObjectiveCLibrary, EntryPoint = "objc_msgSend")]
internal extern static IntPtr IntPtr_objc_msgSend(IntPtr receiver, IntPtr selector, bool isExclusiveTouch);
static void SetExclusiveTouch(bool isExclusiveTouch)
{
var selector = new ObjCRuntime.Selector("setExclusiveTouch:");
IntPtr_objc_msgSend(UIView.Appearance.Handle, selector.Handle, isExclusiveTouch);
}
And set it on AppDelegate:
public override bool FinishedLaunching(UIApplication app, NSDictionary options)
{
...
SetExclusiveTouch(true); // setting exlusive to true disables the multitouch
...
}
My experience is that, by default, a new project doesn't even allow multitouch, you have to turn it on. But I suppose that depends on how you got started. Did you use a mutlitouch example as a template?
First of all, are you absolutely sure multitouch is on? It's possible to generate single touches in sequence pretty quickly. Multitouch is more about what you do with two or more fingers once they are on the surface. Perhaps you have single touch on but aren't correctly dealing with what happens if two buttons are pressed at nearly the same time.
I've just had exactly this problem.
The solution we came up with was simply to inherit a new class from UIButton that overrides the initWithCoder method, and use that where we needed one button push at a time (ie. everywhere):
#implementation ExclusiveButton
(id)initWithCoder: (NSCoder*)decoder
{
[self setExclusiveTouch:YES];
return [super initWithCoder:decoder]
}
#end
Note that this only works with buttons loaded from nib files.
I created UIView Class Extension and added this two functions. and when i want to disable view touch i just call [view makeExclusiveTouch];
- (void) makeExclusiveTouchForViews:(NSArray*)views {
for (UIView * view in views) {
[view makeExclusiveTouch];
}
}
- (void) makeExclusiveTouch {
self.multipleTouchEnabled = NO;
self.exclusiveTouch = YES;
[self makeExclusiveTouchForViews:self.subviews];
}
If you want to disable multitouch programmatically, or if you are using cocos2d (no multipleTouchEnabled option), you can use the following code on your ccTouches delegate:
- (BOOL)ccTouchesBegan:(NSSet *)touches
withEvent:(UIEvent *)event {
NSSet *multiTouch = [event allTouches];
if( [multiTouch count] > 1) {
return;
}
else {
//else your rest of the code
}
Disable all the buttons on view in "Touch Down" event and enable them in "Touch Up Inside" event.
for example
- (void) handleTouchDown {
for (UIButton *btn in views) {
btn.enable = NO;
}
}
- (void) handleTouchUpInside {
for (UIButton *btn in views) {
btn.enable = Yes;
}
------
------
}
I decided this problem by this way:
NSTimeInterval intervalButtonPressed;
- (IBAction)buttonPicturePressed:(id)sender{
if (([[NSDate date] timeIntervalSince1970] - intervalButtonPressed) > 0.1f) {
intervalButtonPressed = [[NSDate date] timeIntervalSince1970];
//your code for button
}
}
I had struggled with some odd cases when dragging objects around a view, where if you touched another object at the same time it would fire the touchesBegan method. My work-around was to disable user interaction for the parent view until touchesEnded or touchesCancelled is called.
override func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent?) {
// whatever setup you need
self.view.userInteractionEnabled = false
}
override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) {
// whatever setup you need
self.view.userInteractionEnabled = true
}
override func touchesCancelled(touches: Set<UITouch>?, withEvent event: UIEvent?) {
// whatever setup you need
self.view.userInteractionEnabled = true
}
A Gotcha:
If you are using isExclusiveTouch, be aware that overriding point(inside:) on the button can interfere, effectively making isExclusiveTouch useless.
(Sometimes you need to override point(inside:) for handling the "button not responsive at bottom of iPhone screen" bug/misfeature (which is caused by Apple installing swipe GestureRecognizers at the bottom of the screen, interfering with button highlighting.)
See: UIButton fails to properly register touch in bottom region of iPhone screen
Just set all relevant UIView's property exclusiveTouch to false do the trick.