iPhone/iPad - 核心动画 - CGAffineTransformMakeTranslation 不会修改框架

发布于 2024-12-22 10:34:09 字数 959 浏览 2 评论 0原文

我面临着一个非常烦人的问题。 这是上下文:我有一个“矩形”视图,它是主视图的子视图。 我想做的很简单,当我单击按钮时,我希望“矩形”视图在 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 技术交流群。

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

发布评论

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

评论(2

¢好甜 2024-12-29 10:34:09

添加第二个视图后,您已经将其变换设置为等于CGAffineTransformMakeTranslation(-1000, 0),并且当您想要删除该视图时,您设置了完全相同的变换 - 因此它将没有影响。这里有 2 个选项:

  1. 将翻译应用于视图已有的转换:

    CGAffineTransform newTransform = CGAffineTransformConcat(rectangleView.transform, CGAffineTransformMakeTranslation(-1000, 0));
    [rectangleView setTransform:newTransform];
    
  2. 不直接应用视图位置操作变换(例如通过其 center 属性)

    UIView *rectangleView = [detailView viewWithTag:4]; //实际帧是(20.0, 30.0, 884.0, 600.0)
    CGAffineTransform tf = CGAffineTransformMakeTranslation(-1000, 0);
    [UIView animateWithDuration:0.5 动画:^{
        [矩形视图setCenter:CGPointApplyAffineTransform(矩形视图.center,tf)];
    } 完成:^(BOOL 完成) {
        [矩形视图从超级视图中删除];
        UIView *otherView = [[UIView alloc] initWithFrame:CGRectMake(1020.0, 30.0, 884.0, 600.0)];
        [otherView setBackgroundColor:[UIColor PurpleColor]];
        [其他视图设置标签:4];
        [detailView addSubview:otherView];
        [UIView animateWithDuration:0.5 动画:^{
            [otherView setCenter: CGPointApplyAffineTransform(otherView.center, tf)];
        } 完成:^(BOOL 完成) {
            [其他查看发布];
        }];
    }];
    

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:

  1. Apply translation to the transform the view already has:

    CGAffineTransform newTransform = CGAffineTransformConcat(rectangleView.transform, CGAffineTransformMakeTranslation(-1000, 0));
    [rectangleView setTransform:newTransform];
    
  2. Instead of applying transforms operate with view position directly (e.g. via its center property)

    UIView *rectangleView = [detailView viewWithTag:4]; //the actual frame is (20.0, 30.0, 884.0, 600.0)
    CGAffineTransform tf = CGAffineTransformMakeTranslation(-1000, 0);
    [UIView animateWithDuration:0.5 animations:^{
        [rectangleView setCenter: CGPointApplyAffineTransform(rectangleView.center, tf)];
    } 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 setCenter: CGPointApplyAffineTransform(otherView.center, tf)];
        } completion:^(BOOL finished) {
            [otherView release];
        }];
    }];
    
近箐 2024-12-29 10:34:09

尝试对 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.

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