I have a problem with a uipangesturerecognizer when I zoom in the screen.
My App have a uiviewcontroller with a uiview.
In this view I have a calayer that is almost half of the entire screen bounds.
This calayer have several sublayers that the user can drag (uipangesture), add (uitapgesture) and remove (doubletapgesture). In order to drag more accurately I added to the uiview a uipinchgesture method to zoom in and out.
At this point everything works good except when I zoomed in and I try to move (pangesture) a sublayer. Sometimes works but usually not (without zooming in always works fine).
I don't know if there is something wrong in my codes or may be this is not the way to do it. Is there anything that I miss?
I'm under sdk 4.2.
these are my codes:
- (id)initWithFrame:(CGRect)frame { [super initWithFrame:frame]; (…) perfil = [[CALayer alloc] init]; //the calayer of the uiview [perfil setBounds:CGRectMake(0, 0, wide, heigth/2)]; [[self layer] addSublayer:perfil]; //I add the sublayers to the "peril" layer when the user touch an "edit button" and that works fine //UIGsture Recognizers UITapGestureRecognizer *singleTapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleSingleTapFrom:)]; UITapGestureRecognizer *doubleTapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleDoubleTapFrom:)]; UIPinchGestureRecognizer *pinchGestureRecognizer = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(handlePinchFrom:)]; UIPanGestureRecognizer *panGestureRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handlePanFrom:)]; [panGestureRecognizer setMinimumNumberOfTouches:1]; [panGestureRecognizer setMaximumNumberOfTouches:1]; [singleTapRecognizer setNumberOfTapsRequired:1]; [singleTapRecognizer setCancelsTouchesInView:NO]; [doubleTapRecognizer setNumberOfTapsRequired:2]; [self addGestureRecognizer:singleTapRecognizer]; [self addGestureRecognizer:doubleTapRecognizer]; [self addGestureRecognizer:pinchGestureRecognizer]; [self addGestureRecognizer:panGestureRecognizer]; [singleTapRecognizer requireGestureRecognizerToFail:doubleTapRecognizer]; singleTapRecognizer.delegate = self; doubleTapRecognizer.delegate = self; pinchGestureRecognizer.delegate = self; panGestureRecognizer.delegate = self; //Some other different stuff here }
- (void) handlePanFrom: (id)sender { CGPoint point = [(UIPanGestureRecognizer*)sender locationInView:self]; boxLayer = [[self perfil] hitTest:point]; [self becomeFirstResponder]; if([(UIPanGestureRecognizer*)sender state] == UIGestureRecognizerStateBegan) { numberOfSublayer = 1000; //declared as a int in UIView.h for (CALayer *elements in [perfil sublayers]) { if([boxLayer isEqual:elements]) { numberOfSublayer = [[perfil sublayers] indexOfObject:elements]; break; } } } (…) //Some more operations once I found the sublayer }
- (void) handlePinchFrom: (id)sender { if([(UIPinchGestureRecognizer*)sender state] == UIGestureRecognizerStateEnded) { lastScale = 1.0; return; } if ([(UIPinchGestureRecognizer*)sender state] == UIGestureRecognizerStateBegan || [(UIPinchGestureRecognizer*)sender state] == UIGestureRecognizerStateChanged) { CGFloat currentScale = [[[sender view].layer valueForKeyPath:@"transform.scale"] floatValue]; const CGFloat kMaxScale = 4.0; const CGFloat kMinScale = 1.0; CGFloat newScale = 1 - (lastScale - [(UIPinchGestureRecognizer*)sender scale]); newScale = MIN(newScale, kMaxScale / currentScale); newScale = MAX(newScale, kMinScale / currentScale); CGAffineTransform transform = CGAffineTransformScale([[sender view] transform], newScale, newScale); [sender view].transform = transform; if ([perfil sublayers]) { for (CALayer *elements in [perfil sublayers]) //Here I try to adjust the zoom to the sublayers { //Here is one first approach //CGAffineTransform transformELS = CGAffineTransformScale([elements affineTransform], 7/(newScale+6), 7/(newScale+6)); //[elements setAffineTransform:transformELS]; //Here is a second approach [elements setBounds:CGRectMake(0, 0, elements.bounds.size.width*7/(newScale+6), elements.bounds.size.height*7/(newScale+6))]; } } lastScale = [(UIPinchGestureRecognizer*)sender scale]; (…) //Some other different stuff here }
Thank you very much.