Why does UIPinchGestureRecognizer couldn't recive Ended state? - objective-c

I have 4 gesture recognizers for 1 view.
- (void)createGestureRecognizers {
UITapGestureRecognizer *singleFingerSingleTap = [[UITapGestureRecognizer alloc]
initWithTarget:self action:#selector(handleSingleFingerSingleTap:)];
singleFingerSingleTap.numberOfTapsRequired = 1;
[self.panelController.view addGestureRecognizer:singleFingerSingleTap];
UITapGestureRecognizer *singleFingerDoubleTap = [[UITapGestureRecognizer alloc]
initWithTarget:self action:#selector(handleSingleFingerDoubleTap:)];
singleFingerDoubleTap.numberOfTapsRequired = 2;
[self.panelController.view addGestureRecognizer:singleFingerDoubleTap];
[singleFingerSingleTap requireGestureRecognizerToFail:singleFingerDoubleTap];
[singleFingerSingleTap release];
[singleFingerDoubleTap release];
UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc]
initWithTarget:self action:#selector(handlePanGesture:)];
[self.panelController.view addGestureRecognizer:panGesture];
[panGesture release];
UIPinchGestureRecognizer *pinchGesture = [[UIPinchGestureRecognizer alloc]
initWithTarget:self action:#selector(handlePinchGesture:)];
[self.panelController.view addGestureRecognizer:pinchGesture];
[pinchGesture release];}
I need to process Ended state for UIPinchGestureRecognizer, but sometimes it has last state Changed, not Ended how it should be. Does anybody know why? I try to play with setDelaysTouchesEnded: but nothing :(

Pinch gesture is gesture of 2 fingers, so you sometimes one finger gets left on screen while other is already up. So, my advice is to handle UIGestureRecognizerStateCancelled and UIGestureRecognizerStateFailed as well as UIGestureRecognizerStateEnded

It's the same issue as the UIRotationGestureRecognizer, it will trigger for each movement and will stop when you stop, but it won't tell you - hey the user removed the fingers, this movement is over.
This is because it's a continuous gesture, not a simple gesture.
You should handle UIGestureRecognizerStateEnded too.

Related

Detect The Users Taps

I want to use tap gesture recognizer for detecting the user taps .i don't have much knowledge about this .Can anyone please help me for this .Tap Gesture .
I want to set the maximum count and i want to detect the user taps count if it is equal i want to perform some operation .Please help me to do this .
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(handleTapGesture:)];
tapGesture.numberOfTapsRequired = 1;
[self.imageforcapture addGestureRecognizer:tapGesture];
The above code is simple example of tea gesture recognizer .
Thanks in advance !!!!
Init Method :
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(handleTapGesture:)];
tapGesture.numberOfTapsRequired = 1;
[self.imageforcapture addGestureRecognizer:tapGesture];
Handle Method :
- (void)handleTapGesture:(UITapGestureRecognizer*)sender {
i=i+1;
if(i==10)
{
//Prforme task here
}
}
Declare i as global variable
Use this:
UITapGestureRecognizer *tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(foundTap:)];
tapRecognizer.numberOfTapsRequired = 1;
tapRecognizer.numberOfTouchesRequired = 1;
[self.view addGestureRecognizer:tapRecognizer];
-(void)foundTap:(UITapGestureRecognizer *)recognizer
{
// Your code goes here
}
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(handleTapGesture:)];
tapGesture.numberOfTapsRequired = 1;
self.imageforcapture.userInteractionEnabled = YES;
[self.imageforcapture addGestureRecognizer:tapGesture];
First, you need to allow imageView to receive touches by adding line above.
Define global variable i.
#property NSInteger i;
in viewDidLoad set the value of i to 0
self.i = 0;
After that you need to handle that global i variable that will track taps.
- (void)handleTapGesture:(UITapGestureRecognizer*)sender {
self.i++;
if (self.i == 10) {
//Perform your expected behaviour
}
}

Having issue with number of taps in UITapGestureRecognizer

I am trying to run a method by 2 times tapping on tv remote, consider tapping not clicking, but the touch surface does't recognize the taps. Instead, clicking two times runs the doubleTapping method.
UITapGestureRecognizer *doubleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(handleTap:)];
doubleTap.allowedTouchTypes =#[[NSNumber numberWithInteger:UITouchTypeIndirect]];
doubleTap.numberOfTapsRequired = 2;
[self.view addGestureRecognizer:doubleTap];
- (void)handleTap:(UITapGestureRecognizer *)sender
{
if (sender.state == UIGestureRecognizerStateBegan)
{
// handling code
NSLog(#"2 times");
}
}
I am missing something?
I was forgot to mention the UIPressType value , now due to position of remote's surface (Up / Down / Right / Left) you can now detect user's tap direction and add numberOfTapsRequired to the action :
UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(handleTap:)];
[tapGestureRecognizer setAllowedPressTypes:#[#(UIPressTypeLeftArrow)]];
[tapGestureRecognizer setNumberOfTapsRequired:2];
[self.view addGestureRecognizer:tapGestureRecognizer];

tap gesture not recognized on uiimageview

I added two uiimageviews, one on another subview uiview (imageview1,imageview2). In the first view the top uiimageview is hidden(imageview2) and in the second view the bottom imageview is hidden(imageview1).
Allocating tap gesture:
UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(oneTap:)];
UITapGestureRecognizer *singleTap1 = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(oneTap:)];
Set user interaction for both uiimageview to YES.
[singleTap setNumberOfTapsRequired:1];
[singleTap1 setNumberOfTapsRequired:1];
// adding gesture to uiimageview
Add tap gesture recognizer and selector respectively.
[imageview1 addGestureRecognizer:singleTap];
[imageview2 addGestureRecognizer:singleTap1];
But my taps are not recognized.
Can any one tell me where the mistake is?
Try setting setUserInteractionEnabled:YES before adding gesture recognizer.
[imageview1 setUserInteractionEnabled:YES]
[imageview2 setUserInteractionEnabled:YES]
[imageview1 addGestureRecognizer:singleTap];
[imageview2 addGestureRecognizer:singleTap1];
Update:
After the comment you have made I suggest you bring your views to the top before detecting the tap event. Because parent imageView is above and catches these taps.
[yourparentview bringSubviewToFront:imageview1];
[yourparentview bringSubviewToFront:imageview2];
UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(oneTap:)];
singleTap.numberOfTapsRequired = 1;
singleTap.numberOfTouchesRequired = 1;
singleTap.delegate = self;
[imageview1 addGestureRecogniser:singleTap];
[singleTap1 release];
imageview1.userInteractionEnabled = YES; //disabled by default

Double tapping problem

Im working on an app with zooming function.
In this app I have this button. I want it to respond to tapping in several ways:
Single tap: Zoom in slightly.
Double tap: Zoom in to the max.
Ive tried several options to achieve this but none are what I want.
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(handleTapGesture:)];
tapGesture.numberOfTapsRequired = 2;
[self.view addGestureRecognizer:tapGesture];
[zoomin addTarget:self action:#selector(zoominMax) forControlEvents:UIControlEventTouchDownRepeat];
Both work on single and double tap but when I press the button once to slightly zoom and seconds later I press it again it doesn't zoom in slightly, it zooms in to the max.
It is possible to fix this with a timer and location check so that when u tap and tap again u can be sure that the location is in a similar area and the taps happened within timer range.
But is this what I really need?
Is there a simpler solution?
the solution provided by omz is not good.
where as you can do this by simply adding these lines of code like posted here. Double-tap or two single-taps?
NOTE THE MAGICAL LINE : [tapRecg requireGestureRecognizerToFail:doubleTapRecg];
ABSTRACT:
UITapGestureRecognizer *doubleTapRecg = [[UITapGestureRecognizer alloc]
initWithTarget:self
action:#selector(doubleTapped:)];
doubleTapRecg.delegate = self;
doubleTapRecg.numberOfTapsRequired = 2;
doubleTapRecg.numberOfTouchesRequired = 1;
[view addGestureRecognizer:doubleTapRecg];
UITapGestureRecognizer *tapRecg = [[UITapGestureRecognizer alloc]
initWithTarget:self
action:#selector(tapped:)];
tapRecg.delegate = self;
tapRecg.numberOfTapsRequired = 1;
tapRecg.numberOfTouchesRequired = 1;
[view addGestureRecognizer:tapRecg];
[tapRecg requireGestureRecognizerToFail:doubleTapRecg];
[doubleTapRecg release];
[tapRecg release];
You can do it with two gesture recognizers and a timer:
UITapGestureRecognizer *tapGestureRecognizer = [[[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(tap:)] autorelease];
[myView addGestureRecognizer:tapGestureRecognizer];
UITapGestureRecognizer *doubleTapGestureRecognizer = [[[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(doubleTap:)] autorelease];
doubleTapGestureRecognizer.numberOfTapsRequired = 2;
[myView addGestureRecognizer:doubleTapGestureRecognizer];
You'll have to use a slight delay in your tap: action before zooming in slightly because the first tap could be followed by a second tap:
- (void)tap:(UITapGestureRecognizer *)recognizer
{
[self performSelector:#selector(singleTap) withObject:nil afterDelay:0.25];
}
- (void)singleTap
{
//slightly zoom in...
}
- (void)doubleTap:(UITapGestureRecognizer *)recognizer
{
//Cancel the timer for the single tap action:
[NSObject cancelPreviousPerformRequestsWithTarget:self selector:#selector(singleTap) object:nil];
//zoom in to the max zoom level...
}

How to let the pageController control the UIImageView?

I want a UIImageView swipe left, the UIImageView will change the image, at this time, the pageController +1, if swipe right, the pageContoller -1, and display the previous image... ...How can I implement it? Any simple code? thz u.
Just attach a UISwipeGestureRecognizer to the UIImageView. When the gesture recognizer triggers, you would check it's direction and then change images and update the pageController accordingly.
UIGestureRecognizer *recognizer;
recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(next:)];
UISwipeGestureRecognizer *swipeLeftRecognizer = (UISwipeGestureRecognizer *)recognizer;
swipeLeftRecognizer.direction = UISwipeGestureRecognizerDirectionLeft;
[self.view addGestureRecognizer:swipeLeftRecognizer];
swipeLeftRecognizer=nil;
[recognizer release];
recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(prev:)];
UISwipeGestureRecognizer *swipeRightRecognizer = (UISwipeGestureRecognizer *)recognizer;
swipeRightRecognizer.direction = UISwipeGestureRecognizerDirectionRight;
[self.view addGestureRecognizer:swipeRightRecognizer];
swipeLeftRecognizer=nil;
[recognizer release];