2013-11-20 76 views
5

我有一個UILiew作爲子視圖在其中。Autolayout動畫約束不會動畫子視圖

現在我改變了UIView的寬度與動畫(改變約束的恆定性)和呼叫layoutIfNeeded在UIView的動畫...)

的視圖是在調整​​大小正常,但的UILabel(子視圖)將字體大小更改爲「最終」大小,但不像uiview那樣正確動畫。

根據WWDC 2012會話,如果只有超視圖的常量發生變化,子視圖應該進行動畫處理。

UILabel的最小字體比例爲0,1,小於字體的大小。

回答

4

我努力通過改變其width動畫化UILabel調整大小約束。標籤有adjustsFontSizeToFitWidthminimumScaleFactor集。

我通過設置標籤'contentModeUIViewContentModeScaleAspectFit「來修復它。默認情況下,標籤爲UIViewContentModeLeft

2

我不認爲你可以通過這種方式爲文本的大小設置動畫。我認爲要做到這一點的唯一方法是創建標籤的快照視圖,將該視圖添加到標籤上,執行動畫,然後刪除快照視圖。此代碼確實將較小的文本略微向下移動,但它看起來相當不錯,當您取消隱藏標籤並刪除圖像視圖時,只有非常小的移動。包含標籤的小視圖的大小爲185x36,標籤的小視圖的每邊都有20個限制,頂部8個,底部7個。我在代碼中將這些相同的約束添加到圖像視圖中。

@interface ViewController() 
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *widthCon; // width constraint on smallView 
@property (weak,nonatomic) IBOutlet UIView *smallView; // view that the label is embedded in 
@property (weak,nonatomic) IBOutlet UILabel *label; 
@end 

@implementation ViewController 

- (IBAction)shrinkView:(id)sender { 

    UIView *snapshot = [self.label snapshotViewAfterScreenUpdates:YES]; 
    [snapshot setTranslatesAutoresizingMaskIntoConstraints:NO]; 
    [self.smallView addSubview:snapshot]; 
    [self.smallView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"|-20-[snapshot]-20-|" options:0 metrics:nil views:@{@"snapshot":snapshot}]]; 
    NSLayoutConstraint *topCon = [NSLayoutConstraint constraintWithItem:snapshot attribute:NSLayoutAttributeTop relatedBy:0 toItem:self.smallView attribute:NSLayoutAttributeTop multiplier:1 constant:8]; 
    NSLayoutConstraint *bottomCon = [NSLayoutConstraint constraintWithItem:self.smallView attribute:NSLayoutAttributeBottom relatedBy:0 toItem:snapshot attribute:NSLayoutAttributeBottom multiplier:1 constant:7]; 
    [self.smallView addConstraints:@[topCon,bottomCon]]; 
    [self.smallView layoutSubviews]; 
    self.label.alpha = 0; 
    self.widthCon.constant = 100; 
    topCon.constant = 18; 
    bottomCon.constant = 10; 
    [UIView animateWithDuration:.5 animations:^{ 
      [self.view layoutIfNeeded]; 
     } completion:^(BOOL finished) { 
      self.label.alpha = 1; 
      [snapshot removeFromSuperview]; 
     }]; 
} 

編輯後:

還有就是動畫視圖,使標籤也動畫下來,而不是立即到其最終大小去的方式。您必須使用計時器直接製作動畫約束(查看WWDC 2012視頻約31分鐘的解釋,「掌握自動佈局的最佳實踐」)。這可以使標籤的大小動畫化,但字體大小的變化很詭異,看起來不太好。如果你有一個寬度約束的觀點,即標籤是在(與標籤有約束雙方),那麼你可以這樣做:

-(IBAction)animateSizeChange:(id)sender { 
    [NSTimer scheduledTimerWithTimeInterval:.001 target:self selector:@selector(doStuff:) userInfo:Nil repeats:YES]; 
} 

-(void)doStuff:(NSTimer *) aTimer { 
    self.widthCon.constant -= .2; 
    if (self.widthCon.constant <90) [aTimer invalidate]; 
} 
+0

用定時器編輯後的代碼看起來很不幸也很跳躍..:/ –

+1

快照的想法很hacky,不應該這樣接近,你不覺得嗎? –

+0

@ Christian'fuzi'Orgler,不,我認爲這根本不算什麼。蘋果在他們自己的一些動畫中使用了這個「訣竅」。此外,還有什麼替代方案?至於你的第一個評論,是的,它是跳動的,這就是我所說的 - 我包括,只是爲了顯示一種方式,你可以動畫的視圖子視圖,有時工作得很好,如果你不想縮小文本。 – rdelmar