0

I want to scale+translate a view and then animate this transformation back. But CGAffineTransformInvert returns a transformation that does something different (cannot understand the logic of it).

#import <UIKit/UIKit.h>
@interface TestView : UIView {
    UIView *view;
    CGAffineTransform transform;
}
@end


#import "TestView.h"
#import <QuartzCore/QuartzCore.h>
@implementation TestView
- (void)testAnimation:(NSString*)animationID finished:(NSNumber*)finished context:(void*)context {
    CGAffineTransform transformInverted = CGAffineTransformInvert(transform);
    [UIView beginAnimations:@"test2" context:NULL];
    [UIView setAnimationDuration:3.0];
    [UIView setAnimationCurve:UIViewAnimationCurveLinear];
    view.transform = transformInverted;
    [UIView commitAnimations];
}

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        view = [[UIView alloc] initWithFrame:CGRectMake(150, 150, 100, 100)];
        view.backgroundColor = [UIColor greenColor];
        [self addSubview:view];

        CGAffineTransform transform1 = CGAffineTransformTranslate(view.transform, -150, -150); 
        transform = CGAffineTransformScale(transform1, (float)1/2, (float)1/2); 

        [UIView beginAnimations:@"test1" context:NULL];
        [UIView setAnimationDuration:3.0];
        [UIView setAnimationCurve:UIViewAnimationCurveLinear];
        [UIView setAnimationDelegate:self];
        [UIView setAnimationDidStopSelector:@selector(testAnimation:finished:context:) ];

        view.transform = transform;

        [UIView commitAnimations];
    }
    return self;
}
4

3 回答 3

5

CGAffineTransformInvert反转变换矩阵,并返回一个矩阵,该矩阵乘以原始结果为CGAffineTransformIdentity.

如果您想反转平移/比例,您应该自己跟踪它们。

PS。矩阵非常复杂,它们不存储平移/缩放/旋转值,它们由多个值组成,当乘以坐标时,可以得到平移/缩放/旋转值,因此您不能期望反转矩阵并得到一个平移、缩放和旋转倒置的矩阵。

你可以在这里阅读更多关于它们的信息。

编辑以回答您的评论:

使用矩阵时,操作顺序很重要,如果先旋转再平移,结果将与先平移再旋转不同。

要反转应用于矩阵的操作,您应该以相反的顺序应用相反的操作,在这种情况下,要反转:

CGAffineTransform transform1 = CGAffineTransformTranslate(view.transform, x1, x2);
CGAffineTransform transform2 = CGAffineTransformScale(transform1, r1, r2);
CGAffineTransform transform3 = CGAffineTransformRotate(transform2, a);

你需要这样做:

CGAffineTransform reversed3 = CGAffineTransformRotate(transform3, -a);
CGAffineTransform reversed2 = CGAffineTransformScale(reversed3, 1/r1, 1/r2);
CGAffineTransform reversed1 = CGAffineTransformTranslate(reversed2, -x1, -x2);

[view setTransform:reversed1];

如果你之前没有修改过视图的变换,你可以这样做:

[view setTransform:CGAffineTransformIdentity];

即零旋转,零平移,比例为 1。

于 2012-05-12T01:22:32.967 回答
0

我不确定我是否理解您要做什么,但是您不应该简单地将视图的转换更改回CGAffineTransformIdentitytestAnimation 方法吗?

于 2012-05-12T00:59:27.593 回答
0

考虑下面的代码。这里我们有transformTest,然后我们在它上面应用transform3。然后我们在transform3的结果逆之上应用。最终结果非常接近 transformTest (浮点精度更低)。

如果你愿意view.transform = CGAffineTransformConcat(view.transform, transformInverted);在你的代码中这样做,它将起作用。

CGAffineTransform transformStart = CGAffineTransformIdentity;
CGAffineTransform transform1 = CGAffineTransformTranslate(transformStart, 10, 23);
CGAffineTransform transform2 = CGAffineTransformScale(transform1, 2, 10);
CGAffineTransform transform3 = CGAffineTransformRotate(transform2, M_PI_4);

CGAffineTransform invertedTransform = CGAffineTransformInvert(transform3);


// here actual test
NSLog(@"--------------- here actual test ---------------");

CGAffineTransform transformTest = CGAffineTransformMakeTranslation(2, 15);
transformTest = CGAffineTransformRotate(transformTest, M_PI_2);
transformTest = CGAffineTransformScale(transformTest, 19, 0.4);
transformTest = CGAffineTransformTranslate(transformTest, -33, -19);
NSLog(@"transformTest %@", NSStringFromCGAffineTransform(transformTest));

CGAffineTransform transformTestMultiplied = CGAffineTransformConcat(transformTest, transform3);
NSLog(@"transformTestMultiplied %@", NSStringFromCGAffineTransform(transformTestMultiplied));

CGAffineTransform transformTestMultipliedByInverse = CGAffineTransformConcat(transformTestMultiplied, invertedTransform);
NSLog(@"transformTestMultipliedByInverse %@", NSStringFromCGAffineTransform(transformTestMultipliedByInverse));

这个的输出:

2016-07-19 13:49:22.034 testUIView[45050:69431115] --------------- here actual test ---------------
2016-07-19 13:49:22.034 testUIView[45050:69431115] transformTest [1.1634144591899855e-15, 19, -0.40000000000000002, 2.4492935982947065e-17, 9.5999999999999623, -612]
2016-07-19 13:49:22.034 testUIView[45050:69431115] transformTestMultiplied [-26.870057685088803, 134.35028842544403, -0.56568542494923812, -2.8284271247461898, 889.0751503711158, -4236.611249867763]
2016-07-19 13:49:22.034 testUIView[45050:69431115] transformTestMultipliedByInverse [0, 19, -0.40000000000000002, 0, 9.5999999999999162, -612]
于 2016-07-19T20:54:10.210 回答