如何使用CGAffineTransform刻度并同时翻转

时间:2014-01-27 23:35:08

标签: ios cgaffinetransform cgaffinetransformscale

我正在尝试使用CGAffineTransform转换点数组。缩放工作完美,但点的左上角为(0,0)而不是左下角。像这样...

Upside down graph

通常我会简单地使用CGAffineTransformMakeScale(1,-1);,但因为我已经在执行缩放,所以无法将其添加到现有变换中。

这是我当前的代码

- (CGAffineTransform)transformFromValueToScreen
{
    CGRect fromRect = self.valueSpace;
    CGRect viewRect = self.screenSpace;

    CGSize scales = CGSizeMake(viewRect.size.width/fromRect.size.width,
                               viewRect.size.height/fromRect.size.height);

    CGAffineTransform transform = CGAffineTransformMakeScale(scales.width, 
                                                             scales.height);

    return transform;
}

1 个答案:

答案 0 :(得分:5)

我假设fromRect.originCGPointZero

您需要一个复合变换,将原点移动到左下角,翻转Y轴,然后应用所需的缩放。

您可以使用修改变换的函数进行复合变换,如下所示:

- (CGAffineTransform)transformFromValueToScreen {
    CGRect fromRect = self.valueSpace;
    CGRect viewRect = self.screenSpace;

    CGSize scales = CGSizeMake(viewRect.size.width/fromRect.size.width,
                               viewRect.size.height/fromRect.size.height);

    CGAffineTransform transform = CGAffineTransformIdentity;

    // Move origin from upper left to lower left.
    transform = CGAffineTransformTranslate(transform, 0, fromRect.size.height);

    // Flip the sign of the Y axis.
    transform = CGAffineTransformScale(transform, 1, -1);

    // Apply value-to-screen scaling.
    transform = CGAffineTransformScale(transform, scales.width, scales.height);

    return transform;
}