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



我正面临一个非常恼人的问题。这是context:我有一个矩形视图它是主视图的子视图。我想做的很简单,当我点击一个按钮时,我想要"矩形"视图在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];
    }];
}];

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

  1. 对视图已有的转换应用转换:

    CGAffineTransform newTransform = CGAffineTransformConcat(rectangleView.transform, CGAffineTransformMakeTranslation(-1000, 0));
    [rectangleView setTransform:newTransform];
    
  2. 不直接对视图位置进行变换操作(例如通过其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];
        }];
    }];
    

尝试将center属性动画化而不是使用仿射变换。转换不是附加的,所以你的第二个动画(当你新添加的细节视图被移出屏幕时)实际上根本没有改变视图,因为它已经应用了转换(-1000,0)。

最新更新