1
votes

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.

2

2 Answers

1
votes

Finally I've found the answer to my problem. This post explains exactly the same issue and it's solutions have worked perfectly for me UIPanGestureRecognizer starting point is off . Thanks.

0
votes

Really hard to say without seeing the sublayer hittest code but it seems really odd that you are rescaling the sublayers when you rescale the parent view/layer.

You should only need to scale the parent layer and all the children will be scaled in turn.

All this manual generation of pinch and pan code is fraught with danger. It can be done but I find you end up with spaghetti.

Place your view in UIScrollView and let it do the work of pinch and pan of the perfil view.

Theres a billion examples of UIScrollView pan and zoom. Te Google and SO will know if you don't already.

I would expect to capture the panning of your sublayers in the touchBegan event of the parent view instead.

This is what I use for capturing the hit in a sublayer

- (CALayer *)hitTest:(CGPoint)thepoint
{
    //
    CGPoint cpoint = [self convertPoint:thepoint fromLayer:self.superlayer];

    if (CGRectContainsPoint([self bounds], cpoint)) {
        NSLog(@"hitme!");
        return self;
    }

    return nil;

}

And this in the touchesBegan/touchesMoved of the parent view. (Keep a weak reference to the scrollview)

// Handles the start of a touch
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{


    NSUInteger tcount = [touches count];
    if(tcount == 1)
    {

    UITouch* touch = [touches anyObject];
    CGPoint tpoint = [touch locationInView:self];
    location = [perfil convertPoint:tpoint fromLayer:view.layer];

    hitsublayer = [self hitTest:location];       
    if([hitsublayer isKindOfClass:[MySubBoxThing class]]){
        //ensure that scroll view doesn't capture the event  
        scrollview.canCancelContentTouches = NO;
    }
    else {
        hitsublayer = nil;
        //or something else to do wit not touching a sublayer.
    }

}

// Handles the continuation of a touch.
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{   
    NSUInteger tcount = [touches count];
    if (tcount == 1 && hitsublayer) {

    UITouch* touch = [touches anyObject];
    CGPoint tlocation = [touch locationInView:self];
    location = [perfil convertPoint:tpoint fromLayer:view.layer];        

    NSLog(@"new loc = %@",[NSValue valueWithCGPoint:location]);
    hitsublayer.position = location;            

    }       

}

// Handles the end of a touch event when the touch is a tap.
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    //NSLog(@"touches ended");
    scrollview.canCancelContentTouches = YES;
    hitsublayer = nil;
    //any other actions once finished drag of object


}

That code is neither complete nor bug checked but should get you close.