How to apply a view's gestures transform values to shapelayer

100 Views Asked by At

I have created a shape layer animation using bezier paths. So, I added a shape layer as a sublayer to view and drew on it. Pan, pinch and rotation gestures added to the view with the required delegate methods. The transform values from gestures applied to the layer, not for the view. I have found some issues when applying gestures

  1. When applying the gestures to the view, shape layer doesn't rotate but layer moves randomly.
  2. when applying pinch, layer zoom in/out from layer's top-left corner.

Please help me to fix these two issues. And I want to rotate and pinch from layer's center point.

The following code segments were used to handle gestures

-(void)handlePanGesture:(UIPanGestureRecognizer*)recognizer{

    if (recognizer.state == UIGestureRecognizerStateBegan || recognizer.state == UIGestureRecognizerStateChanged){
        CGPoint translation = [recognizer translationInView:recognizer.view];
        CGAffineTransform transform = CGAffineTransformTranslate(recognizer.view.transform, translation.x, translation.y);
        _pathOfShape = CGPathCreateCopyByTransformingPath(_shapeLayer.path, &transform);
        _shapeLayer.path = [UIBezierPath bezierPathWithRect:CGPathGetBoundingBox(_pathOfShape)].CGPath;

        [recognizer setTranslation:CGPointZero inView:self];
    }

}

-(void)handlePinchGesture:(UIPinchGestureRecognizer*)recognizer{

    if (recognizer.state == UIGestureRecognizerStateBegan || recognizer.state == UIGestureRecognizerStateChanged)
    {
        CGFloat scale = [recognizer scale];
        CGAffineTransform transform =CGAffineTransformScale(recognizer.view.transform, scale, scale);
        _pathOfShape = CGPathCreateCopyByTransformingPath(_shapeLayer.path, &transform);
        _shapeLayer.path = [UIBezierPath bezierPathWithRect:CGPathGetBoundingBox(_pathOfShape)].CGPath;

        [recognizer setScale:1.0];
    }

}

-(void)handleRotationGesture:(UIRotationGestureRecognizer*)recognizer{

    if (recognizer.state == UIGestureRecognizerStateBegan || recognizer.state == UIGestureRecognizerStateChanged)
    {
        CGFloat rotation = [recognizer rotation];
        CGAffineTransform transform = CGAffineTransformRotate(recognizer.view.transform, rotation);
        _pathOfShape = CGPathCreateCopyByTransformingPath(_shapeLayer.path, &transform);
        _shapeLayer.path = [UIBezierPath bezierPathWithRect:CGPathGetBoundingBox(_pathOfShape)].CGPath;

        [recognizer setRotation:0];
    }

}
0

There are 0 best solutions below