在不撤消旋转变换的情况下无法撤消缩放变换

发布于 2024-09-28 11:33:58 字数 1199 浏览 0 评论 0原文

这是上一个问题的后续。我下面的代码通过缩放和旋转正方形来制作正方形的动画。它通过进行旋转变换并向其添加缩放变换来实现此目的。效果很好。完成后,它会调用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

森林散布 2024-10-05 11:33:58

您只是缩放到相同的大小,因为您基本上是说采用当前变换并在 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);

请记住,当您添加旋转时,请以相反的顺序进行旋转,因此先旋转,然后缩放。如果您涉及翻译,这会更重要,但在这种情况下,两种方式都可能有效。

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.

CGAffineTransform xForm = [self transform];
xForm = CGAffineTransformScale(xForm,0.5, 0.5);

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文