UIGestureRecognizer ending - iphone

I would like to handle the rotation gesture in my iPhone app and rotate an imageView during this. At the end of the gesture I would like to rotate to a fix position the imageView.
So ie. if I rotate the imageView from 0 radian to M_PI/2 radians, but somewhere on halfway I end with the gesture. After the end I would like to test the angle and if it close to M_PI/2 then set it to M_PI/2 otherwise to 0.
Here's my code how I tried to do this:
I create and add the recognizer to my view.
UIGestureRecognizer *recognizer = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:#selector(gestureRecognized:)];
recognizer.delegate = self;
[self addGestureRecognizer:recognizer];
[recognizer release];
Delegate methods:
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {
if (_imageView) {
return YES;
}
return NO;
}
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {
return YES;
}
Gesture recognized method:
- (void)gestureRecognized:(UIRotationGestureRecognizer *)recognizer {
_imageView.transform = CGAffineTransformMakeRotation(recognizer.rotation);
}
These methods are working, but here's the method how I tried to get the end of the gesture. This is not working:
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
NSLog(#"gesture end");
}
Also with the transform I have a little problem. Sometimes it is going back to the 0 radian.
Any suggestion is welcome. Thanks!

I've found the answer on an other forum. I hope this will help somebody else too.
Answer:
You can detect the end of the gesture by checking the UIRotationGestureRecognizer's state in your action method. If [gestureRecognizer state] == UIGestureRecognizerStateEnded then it's done. If something had instead caused it to be cancelled (for instance, a phone call coming in) then you'd see it end in UIGestureRecognizerStateCancelled instead.
If you want to apply the recognizer's rotation directly to your view rather than treat it as a delta from the previous location you can set the recognizer's rotation the its state is UIGestureRecognizerStateBegan, and then it will calculate offsets from that value and you can apply them directly. This just requires that you remember the rotation you last ended at, and then you can do something like [gestureRecognizer setRotation:lastAppliedRotation] when the recognizer's state is UIGestureRecognizerStateBegan.

If the gestureRecognizer recognizes the gesture, by default touchesEnded won't be called (touchesCancelled will be called instead. touchesEnded would only be called if the recognizer doesn't recognize the gesture). Set gestureRecognizer.delaysTouchesEnded to NO, and it should work.

Just in case this happens to you as well and it's the reason you're reading that question :
"ended" and "recognized" are the same value ( 3 ). So this code (in swift) won't work:
if (gesture.state == .recognized || gesture.state == .changed) {
}
else if (gesture.state == .ended) {
//do something else
//=> never called
}

Related

Pass pinch gesture from custom UIView to parent UIScrollView

I've got a custom UIView as a subview in a UIScrollView.
When selected, the user can resize the subview by pinching anywhere on the screen (the sub view is fairly small).
When deselected, I would like this pinch gesture to be passed on to the UIScrollView so that it can handle it as it normally does.
Here is what I'm trying.
- (IBAction)pinchInView:(UIPinchGestureRecognizer *)sender {
if (self.item.isSelected)
{
if ((sender.state == UIGestureRecognizerStateChanged) || (sender.state == UIGestureRecognizerStateEnded))
{
[self.item resizeWithScaleFactor:sender.scale];
}
} else
{
[self.scrollView setZoomScale:self.scrollView.zoomScale *= sender.scale];
}
sender.scale = 1;
}
While it does work, it seems an awkward way to go about this.
Is there a way to let the UIScrollView handle its own zooming?
I'm pretty much using the same approach with pan gestures as well.
If there is any way to make this less awkward, I'd really appreciate your help.
Provide a Delegate to the Gestures which You have added to subview. Implement following Method in delegate
-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecoznizer shouldReceiveTouch:(UITouch *)touch{
if(isSelected == true)
return YES;
else
return NO;
}
Implement the below delegate method
-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch

PAN Gesture is crashing while taking the location of touch in view in iOS

I just do a sample to check the pan gesture.
The pan gesture is detecting and working fine.
But whenever i give a secondPoint in the pan gesture like CGPoint secondPoint = [sender locationOfTouch:1 inView:self.imageView]; it is crashing.
The console is giving the message
*** Terminating app due to uncaught exception 'NSRangeException', reason: '-[UIPanGestureRecognizer locationOfTouch:inView:]: index (1) beyond bounds (1).'
When I use panGestureRecognizer.maximumNumberOfTouches = 1;
panGestureRecognizer.minimumNumberOfTouches =1; still it is crashing.
When I use panGestureRecognizer.maximumNumberOfTouches = 2;
panGestureRecognizer.minimumNumberOfTouches = 2;
then it is not entering into the
- (void)panGestureHandler:(UIPanGestureRecognizer *)sender method.
Can anyone please guide me where im going wrong.
Thanks in advance.Hoping for your help.
I tried in this way.
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
UIPanGestureRecognizer *panGestureRecognizer = [[UIPanGestureRecognizer alloc]initWithTarget:self action:#selector(panGestureHandler:)];
panGestureRecognizer.maximumNumberOfTouches = 2;
[self.imageView addGestureRecognizer:panGestureRecognizer];
}
- (void)panGestureHandler:(UIPanGestureRecognizer *)sender
{
if ([sender state] == UIGestureRecognizerStateBegan )
{
CGPoint firstPoint = [sender locationOfTouch:0 inView:self.imageView];
CGPoint secondPoint = [sender locationOfTouch:1 inView:self.imageView];
}
else if ([sender state] ==UIGestureRecognizerStateEnded )
{
}
}
I too came across this error despite the max and min number of touches being set. I'm subclassing my gesture recognizer and figure it has something to do with that. I got around it by simply checking numberOfTouches before referencing it:
if ([gestureRecognizer numberOfTouches] > 0) {
CGPoint point = [gestureRecognizer locationOfTouch:0 inView:self.superview.window];
}
Hope this helps someone!
You provided a maximumNumberOfTouches, but no minimumNumberOfTouches. I.e., the gesture can be recognized after the first touch. In this case, no second touch may exist, and your index 1 (referring the second element) exceeds the array bounds.
When the max/min touches are set, they determines whether it is a valid gesture to begin sending action but they are not the criteria for ending it. For example, if you set the max/min to 2. If a two-finger-touch is detected, the handler starts receiving the action. Leaving one finger will not end the gesture. The handler still receive action with change-state and one touch. In the end, the handler receives 0 touch and end-state.
The error is telling you that on this line:
CGPoint secondPoint = [sender locationOfTouch:1 inView:self.imageView];
index "1" is out of locationOfTouches bounds. So, as stated above, you need to make sure you set minimumNumberOfTouches
Additionally, you will want to enabled user interaction on the image view in order for it to respond to gesture recognizers.
[self.imageView setUserInteractionEnabled:YES];

Separate the Tap Event from the ImageView

I am having a View where the TapGestureRecognizer is used. I am using the TapGestureRecognizer for the Single and double tap event. So far so good. Now I have added a ImageView on Top of the View , the image view frame is imageView.frame=CGRectMake(50,290,205,100);
Now wherever I am tapping the View , my #selectors being called. I want to skip the tap events only for the ImageView . How to do it ?
I tried using the
if(recognizer.state == UIGestureRecognizerStateRecognized)
{
CGPoint point = [recognizer locationInView:recognizer.view];
}
You have to implement this check
if(!CGRectContainsPoint(image.view.frame, point))
{
//Complete your Work
}
Do this...I hope this will help you...
Whenever you tap on screen this delegate method will call..
in this method please check touch and gestureRecognizer will give some data regarding your tapping.....
Based on that you can proceed.....
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
{
NSLog(#"%#",touch.description);
NSLog(#"%#",gestureRecognizer.description);
}
You need to check for the touch point ,
if(!CGRectContainsPoint(image.view.frame, point))
{
//Do you work here
}
Do this:
if(recognizer.state == UIGestureRecognizerStateRecognized)
{
CGPoint point = [recognizer locationInView:recognizer.view];
if(CGRectContainsPoint(imageView.frame,point)
{
//igonre
}
else
{
// continue
}
}

iPhone iOS how to make UIScrollView work with UIRotationGestureRecognizer when zooming is enabled?

I'm working in iOS5, and apparently I should be able to control or at least subdue the UIScrollView's internal pinch gesture recognizer using scrollView.pinchGestureRecognizer.
However, my code does not seem to work. The recognizer does not treat my class as a delegate and does not wait for my rotation gesture recognizer to fail. What can I do to make the rotation gesture a priority, after which the pinch would be considered?
More precisely, the issue that I'm running in is that the view that is being rotated and zoomed at the same time "flies off the screen" towards the bottom left corner, never to be seen again.
-(void)setup scrollViews
{
[tempScrollView.pinchGestureRecognizer requireGestureRecognizerToFail:rotationRecognizer];
tempScrollView.pinchGestureRecognizer.delegate = self;
tempScrollView.maximumZoomScale = 4.0;
tempScrollView.minimumZoomScale = 0.25;
//
tempScrollView.delegate = self;
}
-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
return YES;
}
-(BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer
{
if([gestureRecognizer isEqual:rotationRecognizer])
{
NSLog(#"Rotation gesture");
}else {
NSLog(#"Other gesture: %#", [gestureRecognizer class]);
}
return YES;
}
- (IBAction)rotateView:(id)sender {
if([sender isKindOfClass:[UIRotationGestureRecognizer class]])
{
UIRotationGestureRecognizer* recognizer = sender;
float recognizerRotation = [recognizer rotation];
CGAffineTransform transform = CGAffineTransformMakeRotation(recognizerRotation);
activeView.transform = transform;
}
}
As far as I know, the pinchGestureRecognizer in UIScrollView is read only.
However, you might try to subclass UIScrollView and override the method addGestureRecognizer: to disable the pinchGestureRecognizer, then add your own custom pinchGestureRecognizer.

How to reliably find correct view for UIGestureRecognizer?

I have a bunch of UIViews like in the image below. The red/pink (semi-transparent) view is on top of the others.
Red has a UISwipeGestureRecognizer.
Green has as a UITapGestureRecognizer.
Blue has no recognizer.
A tap on the visible (bottom-left) part of Green trigger its recognizer.
A tap on the hidden parts of Green does not trigger its recognizer (Red blocks it).
That's the problem: I want Green to trigger. How can I do this?
In practice, the views may be in any order, any number and be subviews of each others etc. But the problem is the same:
How can I reliably find the uppermost view that can handle the gesture (tap or swipe)?
I tried with the code below. It neatly traverses all views, but it fails since it cannot know if the event is part of a swipe or a tap. So the method always returns the red view. If I remove the swipe-recognizer from Red, the code works correctly.
- (UIView*)hitTest:(CGPoint)point withEvent:(UIEvent *)event
{
UIView *hitView = [super hitTest:point withEvent:event];
if (hitView == self)
{
if (self.hasASwipeRecognizer)
return self; // What if this was a tap?
if (self.hasATapRecognizer)
return self;
else
return nil;
}
else
return hitView;
}
An alternative to adding the gesture recognizer to these views would be to add the gesture recognizers to the parent view and handle the use cases appropriately using the delegate method gestureRecognizer:shouldReceiveTouch: method.
Identify whether the particular recognizer should receive the touch and return YES. For example, if the gesture recognizer passed is a swipe recognizer then check if the touch point is within the green view and return YES. Return NO otherwise.
If there are similar gesture recognizers then I suggest that you keep a reference and verify against it.
Usage
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {
CGPoint pointInView = [touch locationInView:gestureRecognizer.view];
if ( [gestureRecognizer isMemberOfClass:[UITapGestureRecognizer class]]
&& CGRectContainsPoint(self.blueView.frame, pointInView) ) {
return YES;
}
if ( [gestureRecognizer isMemberOfClass:[UISwipeGestureRecognizer class]]
&& CGRectContainsPoint(self.greenView.frame, pointInView) ) {
return YES;
}
return NO;
}
One possible solution would be to add a tap gesture recognizer to the top red view and then whenever you get the tap, check whether the tap point intersects with the green view. If so, forward the tap to that view. If not, ignore the tap.
My solutions is:
-(void)handleGesture:(UIGestureRecognizer*)gestureRecognizer {
CGPoint touchPoint = [tapGestureRecognizer locationInView:viewUnderTest];
if ([viewUnderTest pointInside:touchPoint withEvent:nil]) {
NSLog(#"Hit done in view under test");
}
}