我想缩放+翻译视图,然后将此转换动画回来。但是CGAffineTransformInvert返回一个不同的转换(无法理解它的逻辑)。
#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;
}
答案 0 :(得分:5)
CGAffineTransformInvert
反转变换矩阵,并返回一个矩阵,乘以CGAffineTransformIdentity
中的原始结果。
如果您想反转翻译/比例,您应该自己跟踪它们。
PS。矩阵非常复杂,它们不存储平移/缩放/旋转值,它们由几个值组成,当乘以坐标时会给出平移/缩放/旋转值,因此您不能期望反转矩阵并得到一个矩阵,其平移,缩放和旋转。
您可以阅读有关他们的更多信息here。
编辑回答你的意见:
使用矩阵时,操作顺序很重要,如果旋转然后翻译,结果将与翻译然后旋转时的结果不同。
要反转应用于矩阵的操作,您应该以相反的顺序应用相反的操作,在这种情况下,要反过来:
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的刻度。
答案 1 :(得分:0)
我不确定我理解你要做什么,但是你不应该只是在testAnimation方法中将视图转换回CGAffineTransformIdentity
吗?
答案 2 :(得分: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]