首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >有什么方法可以从NSAffineTransform中提取CGAffineTransform吗?

有什么方法可以从NSAffineTransform中提取CGAffineTransform吗?
EN

Stack Overflow用户
提问于 2013-09-03 17:51:47
回答 1查看 2K关注 0票数 2

我正在尝试将iOS代码转换为OSX代码,并且在图形移植方面遇到了一些困难。

在iOS下,我拥有以下内容:

代码语言:javascript
复制
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重写了它:

代码语言:javascript
复制
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];

但这不一样。有人知道问题出在哪里吗?!

任何形式的帮助都是非常感谢的!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-09-04 04:10:08

您的原始代码使用了三个转换,现在您已经尝试使用两个转换。只需返回三个变量(使用不同的变量来明确说明):

代码语言:javascript
复制
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:,您可以删除上述三种用法,并添加以下代码:

代码语言:javascript
复制
[pos1 appendTransform:rot];
[pos1 appendTransform:pos2];
[path transformUsingAffineTransform:pos1];

您甚至可以用一个操作,但请注意从上到第一步的顺序:

代码语言:javascript
复制
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

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/18598516

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档