簡體   English   中英

iPhone / iPad - 核心動畫 - CGAffineTransformMakeTranslation不會修改框架

[英]iPhone/iPad - Core Animation - CGAffineTransformMakeTranslation doesn't modify the frame

我面臨一個非常煩人的問題。 這是上下文:我有一個“矩形”視圖,它是主視圖的子視圖。 我想要做的很簡單,當我點擊一個按鈕時,我希望“矩形”視圖在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) ,並且當您想要刪除該視圖時,您將設置完全相同的變換 - 因此它將不起作用。 這里有2個選項:

  1. 將轉換應用於視圖已具有的轉換:

     CGAffineTransform newTransform = CGAffineTransformConcat(rectangleView.transform, CGAffineTransformMakeTranslation(-1000, 0)); [rectangleView setTransform:newTransform]; 
  2. 而不是應用變換直接操作視圖位置(例如通過其中心屬性)

     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)。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM