gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 19:14:02 27 4
gpt4 key购买 nike

我面临着一个非常烦人的问题。这是上下文:我有一个“矩形” View ,它是主视图的 subview 。我想做的很简单,当我单击按钮时,我希望“矩形” View 在 x 轴上平移以便消失。然后,我添加一个新的 subview 并对其进行翻译,以取代之前的“矩形” View 。它工作正常,只是如果我再次按下按钮,动画将从屏幕上开始,就像 CGAffineTransformMakeTranslation 没有更改我的新“矩形” View 的框架一样。代码如下:

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];
}];
}];

最佳答案

添加第二个 View 后,您已经将其变换设置为等于CGAffineTransformMakeTranslation(-1000, 0),并且当您想要删除该 View 时,您设置了完全相同的变换 - 所以它不会有任何影响。您这里有 2 个选择:

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

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

    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];
    }];
    }];

关于iPhone/iPad - 核心动画 - CGAffineTransformMakeTranslation 不修改框架,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8590150/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com