2017-05-12 42 views
2

我試圖運行一個例子來測試這個函數。我在storyBoard中有一個標籤,並且在視圖控制器中引用了標籤的底部約束。當我使用按鈕時,我希望標籤隨動畫一起移動,但它在沒有它的情況下移動。下面是按鈕的代碼:AnimateWithDuration無法正常工作

- (IBAction)sd:(id)sender { 
[UIView animateWithDuration:0.5f animations:^{ 
    self.constraint.constant += 50; 
}]; 
} 

我知道如何在迅速的使用它,但我的目標C有問題,我知道這將是隻是一個小錯誤...任何幫助嗎?

回答

2

這不是用Autolayout約束UI組件動畫的正確方法。你應該先更新約束,那麼動畫塊中調用layoutIfNeeded

self.constraint.constant += 50; 
[UIView animateWithDuration:0.5f animations:^{ 
    [self.view layoutIfNeeded]; 
}]; 
+0

謝謝,它解決了問題 –

1

使用此

self.constraint.constant += 50; 
    [UIView animateWithDuration:0.5f 
     animations:^{ 
      [self.view layoutIfNeeded]; 
     }]; 
0

試試這個setNeedsLayout有助於在某些情況下。

self.constraint.constant += 50; 
[UIView animateWithDuration:0.5f 
       animations:^{ 
         [self.view setNeedsLayout]; 
        } 
        completion:^(BOOL finished) { 

        }];