2015-10-13 29 views
1

我有兩個大的視圖,視圖A和視圖B.視圖A包含一個子視圖C,它具有自動佈局約束將其定位在A的中心。我想動畫C從A移動到B,意味着從A中刪除C,將它作爲一個孩子添加到B,並將它放在B的中心位置,然後使該移動發生超過一秒。如何使用自動佈局在超級視圖之間進行動畫製作?

我遇到的問題是,當我刪除並添加視圖及其約束時,該移動立即發生:C從A的中心跳到B的中心。是否有一個簡單的解決方案錯過了?我的事件順序現在的問題是:

  • 開始動畫塊
  • c.removeFromSuperview()
  • b.addSubview(c)
  • b.addConstraints(...)

回答

1

能夠繪製佈局過程中,你改變你的約束條件之後。

[UIView animateWithDuration:0.25 
      animations:^{ 
        [self.view layoutIfNeeded]; 
       }]; 

,如果你仍然可以看到跳躍,試圖從當前的SuperView(在其容器視圖的當前幀),以新的SuperView轉換點,以新上海華加子視圖這個翻譯的框架(如果必要的約束) ,然後將約束更改爲適當的值(以及生動描述)

+0

我不調用'現在右layoutIfNeeded';它似乎正確地更新。就像我說的那樣,現在整個事情都在一個動畫塊中,但是因爲我認爲它失敗了,因爲我沒有激活約束 - 我從字面上添加新的約束,因爲當視圖從其中移除時,以前的會丟失上海華。 – FizzBuzz

+0

問題在於你沒有調用layoutifededed,並且系統在系統在您的更新之後下一次繪製視圖之前執行它,並且系統在沒有動畫的情況下執行它,請嘗試使用layoutIfNeeded動畫。 – ogres

+0

謝謝;我剛剛嘗試過,它似乎仍在跳躍。不過,如果我將它設置爲偏離中心的偏移量(例如多個0.9),它會直接跳到中心,然後將其設置爲0.9。 – FizzBuzz

0

如果您將添加額外的巨大視圖D,該怎麼辦? D將覆蓋(A和B)視圖。你應該這樣做

  • 創建d
  • C.removeFromSuperview()
  • 到d將C與約束,這將代替c在同一地點爲A
  • 變化約束上的中心(C應該移動到B的中心動畫)從d
  • 刪除C和它添加到B(C的更新約束)

像這樣

- (IBAction)MAGIC:(id)sender 
{ 
    UIView* view = [[UIView alloc] initWithFrame:self.view.bounds]; 
    view.backgroundColor = [UIColor clearColor]; 
    [self.view addSubview:view]; 

    CGRect fromRect = [self.top convertRect:self.white.frame toView:view]; 

    [self.white removeFromSuperview]; 
    [view addSubview:self.white]; 
    NSLayoutConstraint* topConstraint = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|-(top)-[_white]" 
                       options:0 
                       metrics:@{@"top":@(CGRectGetMinY(fromRect))} 
                       views:NSDictionaryOfVariableBindings(_white)].lastObject; 
    NSLayoutConstraint* leftConstraint = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|-(left)-[_white]" 
                       options:0 
                       metrics:@{@"left":@(CGRectGetMinX(fromRect))} 
                        views:NSDictionaryOfVariableBindings(_white)].lastObject; 

    [view addConstraints:@[topConstraint, leftConstraint]]; 
    CGRect bottomViewFrameInView = [self.bottom convertRect:self.bottom.bounds toView:view]; 
    [view layoutIfNeeded]; 

    topConstraint.constant = CGRectGetMidY(bottomViewFrameInView) - CGRectGetHeight(self.white.bounds)/2; 
    leftConstraint.constant = CGRectGetMidX(bottomViewFrameInView) - CGRectGetWidth(self.white.bounds)/2; 

    [UIView animateWithDuration:0.5 
        animations:^{ 
         [view layoutIfNeeded]; 
        } 
        completion:^(BOOL finished) { 
         [self.white removeFromSuperview]; 
         [self.bottom addSubview:self.white]; 
         NSLayoutConstraint* topConstraint = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|-(top)-[_white]" 
                            options:0 
                            metrics:@{@"top":@(CGRectGetMidY(self.bottom.bounds) - CGRectGetHeight(self.white.bounds)/2)} 
                             views:NSDictionaryOfVariableBindings(_white)].lastObject; 
         NSLayoutConstraint* leftConstraint = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|-(left)-[_white]" 
                             options:0 
                             metrics:@{@"left":@(CGRectGetMidX(self.bottom.bounds) - CGRectGetWidth(self.white.bounds)/2)} 
                             views:NSDictionaryOfVariableBindings(_white)].lastObject; 
         [self.bottom addConstraints:@[topConstraint, leftConstraint]]; 
         [view removeFromSuperview]; 
        }]; 
} 

enter image description here

相關問題