Ios 如何使用仿射变换应用围绕原点的旋转
我想用仿射变换将矩形旋转45度。我知道我必须移动上下文,使矩形位于原点,应用旋转,然后向后移动。我想完全理解它是如何工作的,所以我从一个小测试开始,围绕原点(父视图坐标系中的0,0)旋转它,而不是向后移动。这是转换到原点后的视图(本例中为绿色矩形): 然后我旋转10度,它看起来像这样:Ios 如何使用仿射变换应用围绕原点的旋转,ios,objective-c,Ios,Objective C,我想用仿射变换将矩形旋转45度。我知道我必须移动上下文,使矩形位于原点,应用旋转,然后向后移动。我想完全理解它是如何工作的,所以我从一个小测试开始,围绕原点(父视图坐标系中的0,0)旋转它,而不是向后移动。这是转换到原点后的视图(本例中为绿色矩形): 然后我旋转10度,它看起来像这样: - (void)drawRect:(CGRect)rect { CGContextRef context = UIGraphicsGetCurrentContext(); UIColor
- (void)drawRect:(CGRect)rect {
CGContextRef context = UIGraphicsGetCurrentContext();
UIColor * redColor = [UIColor colorWithRed:1.0 green:0.0 blue:0.0 alpha:1.0];
float x = 150;
float y = 150;
float w = 120;
float h = 40;
CGRect r = CGRectMake(x, y, w, h);
CGContextSetFillColorWithColor(context, redColor.CGColor);
CGContextFillRect(context, r);
CGContextSaveGState(context);
NSLog(NSStringFromCGRect(self.frame));
CGAffineTransform transform = CGAffineTransformIdentity;
float rad = DEGREES_TO_RADIANS(10);
CGFloat rotation = rad;
//rotate
transform = CGAffineTransformRotate(transform, rotation);
transform = CGAffineTransformTranslate(transform, -x, -y);
CGContextConcatCTM(context, transform);
r = CGRectMake(x, y, w, h);
CGContextSetFillColorWithColor(context, [UIColor greenColor].CGColor);
CGContextFillRect(context, r);
CGContextRestoreGState(context);
}
我希望它使用原点作为轴心,但这看起来原点也被平移了,所以当我应用旋转时,它看起来就像我将其应用于旧位置一样,只是平移到了其他点。我错过了什么
这是代码
- (void)drawRect:(CGRect)rect {
CGContextRef context = UIGraphicsGetCurrentContext();
UIColor * redColor = [UIColor colorWithRed:1.0 green:0.0 blue:0.0 alpha:1.0];
float x = 150;
float y = 150;
float w = 120;
float h = 40;
CGRect r = CGRectMake(x, y, w, h);
CGContextSetFillColorWithColor(context, redColor.CGColor);
CGContextFillRect(context, r);
CGContextSaveGState(context);
NSLog(NSStringFromCGRect(self.frame));
//move to the origin
CGAffineTransform transform = CGAffineTransformMakeTranslation(-x, -y);
float rad = DEGREES_TO_RADIANS(10); //with 45 it's outside of the view
CGFloat rotation = rad;
//rotate
transform = CGAffineTransformRotate(transform, rotation);
CGContextConcatCTM(context, transform);
r = CGRectMake(x, y, w, h);
CGContextSetFillColorWithColor(context, [UIColor greenColor].CGColor);
CGContextFillRect(context, r);
CGContextRestoreGState(context);
}
我发现了问题。变换以相反的顺序应用,因此它将首先旋转,然后进行平移 如果我这样改变它:
- (void)drawRect:(CGRect)rect {
CGContextRef context = UIGraphicsGetCurrentContext();
UIColor * redColor = [UIColor colorWithRed:1.0 green:0.0 blue:0.0 alpha:1.0];
float x = 150;
float y = 150;
float w = 120;
float h = 40;
CGRect r = CGRectMake(x, y, w, h);
CGContextSetFillColorWithColor(context, redColor.CGColor);
CGContextFillRect(context, r);
CGContextSaveGState(context);
NSLog(NSStringFromCGRect(self.frame));
CGAffineTransform transform = CGAffineTransformIdentity;
float rad = DEGREES_TO_RADIANS(10);
CGFloat rotation = rad;
//rotate
transform = CGAffineTransformRotate(transform, rotation);
transform = CGAffineTransformTranslate(transform, -x, -y);
CGContextConcatCTM(context, transform);
r = CGRectMake(x, y, w, h);
CGContextSetFillColorWithColor(context, [UIColor greenColor].CGColor);
CGContextFillRect(context, r);
CGContextRestoreGState(context);
}
然后它就起作用了:
为什么投票被否决?