在不撤消旋转变换的情况下无法撤消缩放变换
这是上一个问题的后续。我下面的代码通过缩放和旋转正方形来制作正方形的动画。它通过进行旋转变换并向其添加缩放变换来实现此目的。效果很好。完成后,它会调用 throbReset。我曾经使用 throbReset
只需将 self 的变换
设置为 CGAffineTransformMakeScale
即可取消缩放,但也会取消旋转。因此,我尝试从当前的 transform
开始并向其添加非缩放,但现在它没有执行任何操作(可见)。
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];
}
Here's a follow up to a previous question. My code below animates a square by scaling it and rotating it. It does this by making a rotation transform and adding a scale transform to it. That works fine. When it's done, it calls throbReset
. I used to have throbReset
just set self's transform
to a CGAffineTransformMakeScale
and that would unscale it, but would also unrotate it. So I tried starting with the current transform
and adding the unscale to it, but now it doesn't do anything (visible).
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];
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您只是缩放到相同的大小,因为您基本上是说采用当前变换并在 X 上按 1:1 缩放,在 Y 上按 1:1 缩放。您可能希望在第二种方法中执行 0.5,0.5 而不是 1.0,1.0。
请记住,当您添加旋转时,请以相反的顺序进行旋转,因此先旋转,然后缩放。如果您涉及翻译,这会更重要,但在这种情况下,两种方式都可能有效。
You are just scaling to the same size since you are basically saying take the current transform and scale it 1:1 on X and 1:1 on Y. You might want to do 0.5,0.5 instead of 1.0,1.0 in your second method.
Keep in mind when you add the rotation to do it in reverse order, so rotate then scale. This would be more important if you involved a translation but in this case would probably work either way.