Cha*_*lis 5 objective-c cgaffinetransform ios uibezierpath
我正在创建3个矩形UIBezierPath,我想旋转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))];
}
Run Code Online (Sandbox Code Playgroud)
Dun*_*n C 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];
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4860 次 |
| 最近记录: |