0
votes

I have a view with two subviews:

  1. A UIImageView
  2. A small Custom View which is like a clock with hands that can be rotated.

I have written the code for rotation of hands in touchesBegan and touchesMoved of the custom clock-like view.

This custom view is placed over the image and I have added two-finger zoom,rotation,panGesture to the imageView.

Now My problem is whenever I one of my two finger touches hands of this clock-view, they move and rotate which I dont want.

I want to restrict their touches only when I am doing a single finger gesture on them and not two finger gesture on the image behind it.

EDIT: Here is my code to add the gesture

UIPanGestureRecognizer *panGesture = [[[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handlePan:)] autorelease];
panGesture.maximumNumberOfTouches = 2;
panGesture.minimumNumberOfTouches = 2;
[self.imageView addGestureRecognizer:panGesture];
1

1 Answers

1
votes

The method -(NSUInteger)numberOfTouches of UIGestureRecognizer can tell you how many touches are placed on your view. Also this Event Handling Guide will may help you :)

Another way to go would be UITapGestureRecognizer, which can be configured with numberOfTouchesRequired to limit one recognizer to a special amount of fingers.

EDIT

I suggest you to use a private BOOL which locks interaction with one of the gesture recognizers, if another one is active.

With the new LLVM Compiler available in XCode 4 and later, you can declare @private variables in default categories inside your implementation (.m) file:

@interface YourClassName() {
  @private:
    BOOL interactionLockedByPanRecognizer;
    BOOL interactionLockedByGestureRecognizer;
}
@end

@implementation YourClassName
  ... your code ...
@end

Your method handling the pan interaction (I assume you'll do some kind of animation at the end to move around stuff):

- (void)handlePan:(id)sender
{
  if (interactionLockedByGestureRecognizer) return;

  interactionLockedByPanRecognizer = YES;
  ... your code ...
  [UIView animateWithDuration:0.35 delay:0.0 options:UIViewAnimationCurveEaseOut
                 animations:^{
                   [[sender view] setCenter:CGPointMake(finalX, finalY)];
                 } 
                 completion:^( BOOL finished ) {
                   interactionLockedByPanRecognizer = NO;
                 }
 ];
}

Now you just need to check inside your touchesBegan, touchesMoved and touchesEnded if interactions are locked by the UIPanGestureRecognizer:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
  if (interactionLockedByPanRecognizer) return;
  interactionLockedByGestureRecognizer = YES;
  ... your code ...
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
  if (interactionLockedByPanRecognizer) return;
  ... your code ...
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
  if (interactionLockedByPanRecognizer) return;
  ... your code ...
  interactionLockedByGestureRecognizer = NO;
}