这是对上一个问题的跟进。我下面的代码通过缩放和旋转它来动画一个正方形。它通过进行旋转变换并向其添加缩放变换来实现。那很好用。完成后,它会调用 throbReset
。我曾经让 throbReset
只是将 self's transform
设置为 CGAffineTransformMakeScale
,这会取消缩放,但也会取消旋转。所以我尝试从当前的 transform
开始并向其添加 unscale,但现在它什么也没做(可见)。
CGColorRef color = [[colorArray objectAtIndex:colorIndex] CGColor];
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDelegate:self];
[UIView setAnimationDuration:0.5f];
[UIView setAnimationDidStopSelector:@selector(throbReset:context:)];
// [[self layer] setFillMode:kCAFillModeForwards]; // apparently not needed
CGAffineTransform xForm = CGAffineTransformMakeScale(2.0, 2.0);
xForm = CGAffineTransformRotate(xForm, M_PI / 4);
[self setTransform:xForm];
[[self layer] setBackgroundColor:color];
[UIView commitAnimations];
}
- (void)throbReset:(NSString *)animationID context:(void*)context {
NSLog(@"-%@:%s fired", [self class], _cmd);
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:2.0];
CGAffineTransform xForm = [self transform];
xForm = CGAffineTransformScale(xForm, 1.0, 1.0);
[self setTransform:xForm];
[UIView commitAnimations];
}
最佳答案
你只是缩放到相同的大小,因为你基本上是说采用当前变换并在 X 上按 1:1 缩放,在 Y 上按 1:1 缩放。你可能想在你的中执行 0.5,0.5 而不是 1.0,1.0第二种方法。
CGAffineTransform xForm = [self transform];
xForm = CGAffineTransformScale(xForm,0.5, 0.5);
请记住,当您以相反的顺序添加旋转时,先旋转再缩放。如果您涉及翻译,这将更为重要,但在这种情况下,两种方式都可能有效。
关于iphone - 在不撤消旋转变换的情况下无法撤消缩放变换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3989178/