I am creating 3 rectangles using UIBezierPath
, which I want to rotate 45 degrees. I use the applyTransform
method and pass CGAffineTransformMakeRotation
. I have looked all over Google, and none of the implementations that I have dound worked. Does anyone see what I am 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))];
}
You create your bezier paths, fill them, then rotate them. Then discard the bezier paths.
The fact that you rotate them is meaningless because you don't rotate them until after you fill them. Change the order so you create your paths, rotate them, then fill them.
Note that since you rotate all your paths by the same amount, you could create a single affine transform and apply that same transform to all 3 paths:
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];
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With