我正在尝试将iOS代码转换为OSX代码,并且在图形移植方面遇到了一些困难。
在iOS下,我拥有以下内容:
NSRect rect = NSMakeRect(centerX.doubleValue - width.doubleValue / 2,
centerY.doubleValue - height.doubleValue / 2,
width.doubleValue, height.doubleValue);
UIBezierPath * path = nil;
switch (self.shape)
{
case HSTargetShapeInvisible:
path = [UIBezierPath bezierPath];
break;
case HSTargetShapeOval:
path = [UIBezierPath bezierPathWithOvalInRect:rect];
break;
case HSTargetShapeRectangle:
path = [UIBezierPath bezierPathWithRect:rect];
break;
default:
break;
}
CGAffineTransform translate = CGAffineTransformMakeTranslation(-1 * centerX.doubleValue,
-1 * centerY.doubleValue);
[path applyTransform:translate];
CGAffineTransform rotate = CGAffineTransformMakeRotation(rotation.doubleValue / 180 * M_PI);
[path applyTransform:rotate];
translate = CGAffineTransformMakeTranslation(centerX.doubleValue, centerY.doubleValue);
[path applyTransform:translate];这是有效的perfectly...then,我用这种方式为OSX重写了它:
NSRect rect = NSMakeRect(centerX.doubleValue - width.doubleValue / 2,
centerY.doubleValue - height.doubleValue / 2,
width.doubleValue, height.doubleValue);
NSBezierPath * path = nil;
switch (self.shape)
{
case HSTargetShapeInvisible:
path = [NSBezierPath bezierPath];
break;
case HSTargetShapeOval:
path = [NSBezierPath bezierPathWithOvalInRect:rect];
break;
case HSTargetShapeRectangle:
path = [NSBezierPath bezierPathWithRect:rect];
break;
default:
break;
}
NSAffineTransform *rot = [NSAffineTransform transform];
NSAffineTransform *pos = [NSAffineTransform transform];
[pos translateXBy:-1*centerX.doubleValue yBy:-1*centerY.doubleValue];
[rot rotateByRadians:rotation.doubleValue / 180 * M_PI];
[pos translateXBy:centerX.doubleValue
yBy:centerY.doubleValue ];
[rot appendTransform:pos];
[path transformUsingAffineTransform:rot];但这不一样。有人知道问题出在哪里吗?!
任何形式的帮助都是非常感谢的!
发布于 2013-09-04 04:10:08
您的原始代码使用了三个转换,现在您已经尝试使用两个转换。只需返回三个变量(使用不同的变量来明确说明):
NSAffineTransform *pos1 = [NSAffineTransform transform];
[pos1 translateXBy:-1*centerX.doubleValue yBy:-1*centerY.doubleValue];
[path transformUsingAffineTransform:pos1];
NSAffineTransform *rot = [NSAffineTransform transform];
[rot rotateByRadians:rotation.doubleValue / 180 * M_PI];
[path transformUsingAffineTransform:rot];
NSAffineTransform *pos2 = [NSAffineTransform transform];
[pos2 translateXBy:centerX.doubleValue yBy:centerY.doubleValue];
[path transformUsingAffineTransform:pos2];如果您希望只使用一个transformUsingAffineTransform:,您可以删除上述三种用法,并添加以下代码:
[pos1 appendTransform:rot];
[pos1 appendTransform:pos2];
[path transformUsingAffineTransform:pos1];您甚至可以用一个操作,但请注意从上到第一步的顺序:
NSAffineTransform *compound = [NSAffineTransform transform];
[compound translateXBy:centerX.doubleValue yBy:centerY.doubleValue];
[compound rotateByRadians:rotation.doubleValue / 180 * M_PI];
[compound translateXBy:-1*centerX.doubleValue yBy:-1*centerY.doubleValue];
[path transformUsingAffineTransform:compound];HTH
https://stackoverflow.com/questions/18598516
复制相似问题