如何正确计算两个CGAffineTransform矩阵之间的差值(差异)?

时间:2012-05-08 15:03:19

标签: objective-c ios matrix uigesturerecognizer cgaffinetransform

我一直在玩斯坦福大学演讲/ WWDC视频,向您展示如何创建同时捏合/缩放的手势。它返回CGAffineTransform,然后您将其应用于UIView。

这一切都运行正常,但我试图让它更流畅,并为代码添加一些减速。我试图通过在每个UIGestureRecognizerStateChanged调用上存储转换矩阵来做到这一点,然后当我收到UIGestureRecognizerStateEnded时,我在最后的Changed和Ended矩阵之间存储了delta:

- (void)handleTransform:(TransformGestureRecognizer *)transformRecognizer
{
    CGAffineTransform transform = transformRecognizer.transform;

    // cancel any previous animation updates
    [UIView cancelPreviousPerformRequestsWithTarget:self selector:@selector(decelerateView:) object:transformRecognizer.view];

    if(transformRecognizer.state == UIGestureRecognizerStateBegan)
    {
        transform = CGAffineTransformConcat(transformRecognizer.view.transform, transform);
        transformRecognizer.transform = transform;
    }
    else if(transformRecognizer.state == UIGestureRecognizerStateChanged)
    {
        changingTransform = transform;
    }
    else if(transformRecognizer.state == UIGestureRecognizerStateEnded)
    {
        // get transform difference
        releaseTransformDiff.a = (transform.a - changingTransform.a);
        releaseTransformDiff.b = (transform.b - changingTransform.b);
        releaseTransformDiff.c = (transform.c - changingTransform.c);
        releaseTransformDiff.d = (transform.d - changingTransform.d);
        releaseTransformDiff.tx = (transform.tx - changingTransform.tx);
        releaseTransformDiff.ty = (transform.ty - changingTransform.ty);

        // start updating deceleration animation
        [self performSelector:@selector(decelerateView:) withObject:transformRecognizer.view afterDelay:1.0/60.0];
    }

    transformRecognizer.view.transform = transform;
}

然后,我有一个decelerateView:方法来设置减速动画:

#define DECELERATION_RATE 0.9

- (void)decelerateView:(UIView *)view
{
    releaseTransformDiff.a = releaseTransformDiff.a * DECELERATION_RATE;
    releaseTransformDiff.b = releaseTransformDiff.b * DECELERATION_RATE;
    releaseTransformDiff.c = releaseTransformDiff.c * DECELERATION_RATE;
    releaseTransformDiff.d = releaseTransformDiff.d * DECELERATION_RATE;
    releaseTransformDiff.tx = releaseTransformDiff.tx * DECELERATION_RATE;
    releaseTransformDiff.ty = releaseTransformDiff.ty * DECELERATION_RATE;

    view.transform = CGAffineTransformMake(view.transform.a + releaseTransformDiff.a, 
                                           view.transform.b + releaseTransformDiff.b, 
                                           view.transform.c + releaseTransformDiff.c, 
                                           view.transform.d + releaseTransformDiff.d, 
                                           view.transform.tx + releaseTransformDiff.tx,
                                           view.transform.ty + releaseTransformDiff.ty);
    [self performSelector:@selector(decelerateView:) withObject:view afterDelay:1.0/60.0];
}

这有时会起作用,但有时会突然停止,并且几乎从不减速我做的任何旋转旋转...我想我不是正确计算两个矩阵的三角形。这样做的正确方法是什么?

1 个答案:

答案 0 :(得分:1)

CGAffineTransformation是一个表示3 x 3矩阵的简化数据结构:

|a  b  0|
|c  d  0|
|tx ty 1|

通过将3 x 1矩阵乘以上述矩阵

来转换点
                      |a  b  0|
[x' y' 1] = [x y 1] X |c  d  0|
                      |tx ty 1|

要创建缩放仿射变换,请将a设置为缩放x轴的系数,将d设置为缩放y轴的系数。要创建平移仿射变换,请将tx设置为沿x轴移动的值,将ty设置为沿y轴移动的值。

此问题中使用的技术适用于仅仅缩放或平移变换(或这两种类型的变换的组合)的仿射变换。这是因为受影响的四个值会线性且独立地发生变化。

为角度A创建旋转仿射变换设置a到cos A,b到sin A,c到-sin A,d到cos A.由于sin和cos不是线性函数,所以在这个问题无法正常运作。更复杂的是,两个变换的组合是表示那些变换的两个矩阵的乘法。在涉及旋转时,得到的矩阵将不会与矩阵线性地不同,这将导致稍微不同的变换。

构建增量转换的方法是更改​​转换构建函数调用的输入,而不是直接使用生成的矩阵。