UIBezierPath不使用applyTransform方法旋转

时间:2014-05-29 22:52:30

标签: ios objective-c cgaffinetransform uibezierpath

我使用UIBezierPath创建3个矩形,我想旋转45度。我使用applyTransform方法并传递CGAffineTransformMakeRotation。我已经遍布谷歌了,而且我所做的任何实现都没有。有谁看到我做错了什么?这是我的代码:

#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))];
}

1 个答案:

答案 0 :(得分:11)

您创建贝塞尔曲线路径,填充它们,然后旋转它们。然后丢弃bezier路径。

旋转它们的事实毫无意义,因为在填充它们之前,它们不会旋转它们。更改顺序,以便创建路径,旋转它们,然后填充它们。

请注意,由于您将所有路径旋转相同的量,因此可以创建单个仿射变换并将相同的变换应用于所有3个路径:

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];