3

How to rotate around a certain CGPoint from the Given UIView.

The following images defines a CAShapeLayer around which there were four UIView's as top, left, bottom and right:

enter image description here

I have added UIRotationGesture to self.view through which I am rotating CAShapeLayer, so I want to move point of the it to move as well along with the CAShapeLayer like shown in the following image:

enter image description here

The following defines the code that I am using to achieve the same but having some issues with it:

- (void)handleRotation:(UIRotationGestureRecognizer *)recognizer
{
    if(recognizer.state == UIGestureRecognizerStateBegan)
    {
        CGPoint touch1 = [recognizer locationOfTouch:0 inView:_imgBackground];
        CGPoint touch2 = [recognizer locationOfTouch:1 inView:_imgBackground];
        UIBezierPath *bezierpath = [UIBezierPath bezierPath];
        bezierpath.CGPath = shapeLayer.path;
    }
    else if(recognizer.state == UIGestureRecognizerStateChanged)
    {
        CGPathRef path = createPathRotatedAroundBoundingBoxCenter(shapeLayer.path, rotationGesture.rotation);
        shapeLayer.path  = path;
        CGPathRelease(path);
        rotationGesture.rotation = 0;
    }
    else if (recognizer.state == UIGestureRecognizerStateEnded)
    {
        NSMutableArray *rotatingLayerPoints = [NSMutableArray array]; // will contain all the points of object that is been rotated
            CGPathApply(shapeLayer.path, (__bridge void * _Nullable)(rotatingLayerPoints), CGPathApplier);

            degreeOfRotation = DEGREES_TO_RADIANS([self getDegreeFromStartPoint:[[rotatingLayerPoints objectAtIndex:0] CGPointValue] endPoint:[[rotatingLayerPoints objectAtIndex:1] CGPointValue]]); // getting degree of first line's movement
            CGRect frame = CGPathGetBoundingBox(shapeLayer.path);
            CGPoint rotationPoint = CGPointMake(frame.origin.x + (frame.size.width/2), frame.origin.y + (frame.size.height/2));// The point we are rotating around
            CGFloat minX   = CGRectGetMinX(topPoint.frame);
            CGFloat minY   = CGRectGetMinY(topPoint.frame);
            CGFloat width  = CGRectGetWidth(topPoint.frame);
            CGFloat height = CGRectGetHeight(topPoint.frame);
            CGPoint anchorPoint =  CGPointMake((rotationPoint.x-minX)/width,
                                       (rotationPoint.y-minY)/height);
            topPoint.layer.anchorPoint = anchorPoint;
            topPoint.layer.position = rotationPoint;

        topPoint.transform = CGAffineTransformMakeRotation(degreeOfRotation);


    }
}


void CGPathApplier (void *info, const CGPathElement *element) // used to get points from CGPath
{
    NSMutableArray *bezierPoints = (__bridge NSMutableArray *)info;

    CGPoint *points = element->points;
    CGPathElementType type = element->type;

    switch(type)
    {
        case kCGPathElementMoveToPoint: // contains 1 point
            [bezierPoints addObject:[NSValue valueWithCGPoint:points[0]]];
            break;

        case kCGPathElementAddLineToPoint: // contains 1 point
            [bezierPoints addObject:[NSValue valueWithCGPoint:points[0]]];
            break;

        case kCGPathElementAddQuadCurveToPoint: // contains 2 points
            [bezierPoints addObject:[NSValue valueWithCGPoint:points[0]]];
            [bezierPoints addObject:[NSValue valueWithCGPoint:points[1]]];
            break;

        case kCGPathElementAddCurveToPoint: // contains 3 points
            [bezierPoints addObject:[NSValue valueWithCGPoint:points[0]]];
            [bezierPoints addObject:[NSValue valueWithCGPoint:points[1]]];
            [bezierPoints addObject:[NSValue valueWithCGPoint:points[2]]];
            break;

        case kCGPathElementCloseSubpath: // contains no point
            break;
    }
}
Bhargav Soni
  • 1,067
  • 1
  • 9
  • 22

0 Answers0