iPhone/iPad - 核心动画 - CGAffineTransformMakeTranslation 不会修改框架
我面临着一个非常烦人的问题。 这是上下文:我有一个“矩形”视图,它是主视图的子视图。 我想做的很简单,当我单击按钮时,我希望“矩形”视图在 x 轴上平移以便消失。然后,我添加一个新的子视图并对其进行翻译,以取代之前的“矩形”视图。 它工作正常,只是如果我再次按下按钮,动画将从屏幕上开始,就像 CGAffineTransformMakeTranslation 没有更改我的新“矩形”视图的框架一样。 这是代码:
UIView *rectangleView = [detailView viewWithTag:4]; //the actual frame is (20.0, 30.0, 884.0, 600.0)
[UIView animateWithDuration:0.5 animations:^{
[rectangleView setTransform:CGAffineTransformMakeTranslation(-1000, 0)];
} completion:^(BOOL finished) {
[rectangleView removeFromSuperview];
UIView *otherView = [[UIView alloc] initWithFrame:CGRectMake(1020.0, 30.0, 884.0, 600.0)];
[otherView setBackgroundColor:[UIColor purpleColor]];
[otherView setTag:4];
[detailView addSubview:otherView];
[UIView animateWithDuration:0.5 animations:^{
[otherView setTransform:CGAffineTransformMakeTranslation(-1000, 0)];
} completion:^(BOOL finished) {
[otherView release];
}];
}];
I'm facing a very annoying problem.
Here's the context : I have a "rectangle" view which is the subview of the main view.
What i'm trying to do is simple, when I click on a button, I want the "rectangle" view to translate on the x-axis in order to disappear. I then add a new subview and translate it in order to take the place of the previous "rectangle" view.
It's work fine except that if I press the button again, the animation will begin off the screen, like the CGAffineTransformMakeTranslation didn't change the frame of my new "rectangle" view.
Here's the code :
UIView *rectangleView = [detailView viewWithTag:4]; //the actual frame is (20.0, 30.0, 884.0, 600.0)
[UIView animateWithDuration:0.5 animations:^{
[rectangleView setTransform:CGAffineTransformMakeTranslation(-1000, 0)];
} completion:^(BOOL finished) {
[rectangleView removeFromSuperview];
UIView *otherView = [[UIView alloc] initWithFrame:CGRectMake(1020.0, 30.0, 884.0, 600.0)];
[otherView setBackgroundColor:[UIColor purpleColor]];
[otherView setTag:4];
[detailView addSubview:otherView];
[UIView animateWithDuration:0.5 animations:^{
[otherView setTransform:CGAffineTransformMakeTranslation(-1000, 0)];
} completion:^(BOOL finished) {
[otherView release];
}];
}];
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
添加第二个视图后,您已经将其变换设置为等于
CGAffineTransformMakeTranslation(-1000, 0)
,并且当您想要删除该视图时,您设置了完全相同的变换 - 因此它将没有影响。这里有 2 个选项:将翻译应用于视图已有的转换:
不直接应用视图位置操作变换(例如通过其 center 属性)
After your second view is added you already set its transform to be equal to
CGAffineTransformMakeTranslation(-1000, 0)
, and when you want to remove that view you set exactly the same transform - so it will have no effect. You have 2 options here:Apply translation to the transform the view already has:
Instead of applying transforms operate with view position directly (e.g. via its center property)
尝试对
center
属性进行动画处理,而不是使用仿射变换。变换不是附加的,因此您的第二个动画(当新添加的详细视图随后移出屏幕时)实际上根本不会改变视图,因为它已经应用了该平移(-1000,0)。Try animating the
center
property instead of using an affine transform. The transforms are not additive, so your second animation (when your newly added detail view is then moved off the screen) isn't actually altering the view at all, as it already has that translation (-1000,0) applied to it.