UIBezierPath does not spin using applyTransform method

I create 3 rectangles using UIBezierPathwhich I want to rotate 45 degrees. I use the method applyTransformand pass CGAffineTransformMakeRotation. I looked all over Google and none of the implementations I worked on work. Does anyone see what I'm doing wrong? Here is my code:

#define DEGREES_TO_RADIANS(x) (M_PI * (x) / 180.0)

@implementation BaseballDiamondView

// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    CGRect superViewFrame = self.bounds;
    [[UIColor whiteColor] setFill];

    // draw third base
    UIBezierPath *thirdBasePath = [UIBezierPath bezierPathWithRect:CGRectMake(2.0, 4.0, 7.0, 7.0)];
    [thirdBasePath fill];

    // draw second base
    UIBezierPath *secondBasePath = [UIBezierPath bezierPathWithRect:CGRectMake((superViewFrame.size.width / 2.0) - 3.0, 2.0, 7.0, 7.0)];
    [secondBasePath fill];

    // draw first base
    UIBezierPath *firstBasePath = [UIBezierPath bezierPathWithRect:CGRectMake(superViewFrame.size.width - 5.0, 4.0, 7.0, 7.0)];
    [firstBasePath fill];

    // transform the rectangles
    NSInteger degreeToRotate = 45;
    [firstBasePath applyTransform:CGAffineTransformMakeRotation(DEGREES_TO_RADIANS(degreeToRotate))];
    [secondBasePath applyTransform:CGAffineTransformMakeRotation(DEGREES_TO_RADIANS(degreeToRotate))];
    [thirdBasePath applyTransform:CGAffineTransformMakeRotation(DEGREES_TO_RADIANS(degreeToRotate))];
}
+4
source share
1 answer

You create your bezier paths, fill them, and then rotate them. Then drop the bezier trajectories.

, , , , . , , .

: , :

UIBezierPath *thirdBasePath = [UIBezierPath bezierPathWithRect:CGRectMake(2.0, 4.0, 7.0, 7.0)];

UIBezierPath *secondBasePath = [UIBezierPath bezierPathWithRect:CGRectMake((superViewFrame.size.width / 2.0) - 3.0, 2.0, 7.0, 7.0)];

UIBezierPath *firstBasePath = [UIBezierPath bezierPathWithRect:CGRectMake(superViewFrame.size.width - 5.0, 4.0, 7.0, 7.0)];
NSInteger degreeToRotate = 45;
CGAffineTransform transform = CGAffineTransformMakeRotation(DEGREES_TO_RADIANS(degreeToRotate));
[firstBasePath applyTransform: transform];
[secondBasePath applyTransform: transform];
[thirdBasePath applyTransform: transform];

[thirdBasePath fill];
[secondBasePath fill];
[firstBasePath fill];
+11

Source: https://habr.com/ru/post/1542569/


All Articles