2015-09-19 101 views
1

這是寫在斯威夫特2在Xcode 7的UIView動畫動畫,而是立即移回原來的位置

我有一個UIView,原本被設定爲在屏幕下方。被觸發後,它應該從屏幕底部向上滑動。這是動畫代碼:

func durationView(sender: UIButton){ 
    let heightOffset = self.durationNotificationContainerView.frame.height 

    self.durationNotificationContainerView.translatesAutoresizingMaskIntoConstraints = false 
    UIView.animateWithDuration(0.2, animations: {self.durationNotificationContainerView.frame = CGRectMake(self.durationNotificationContainerView.frame.origin.x, self.durationNotificationContainerView.frame.origin.y + heightOffset, self.durationNotificationContainerView.frame.width, self.durationNotificationContainerView.frame.height)}) 
} 

會發生什麼現在的問題是,當我按下觸發這一觀點的UIButton,它向上滑動,然後立即回落(到屏幕之外)。我還應該指出,在我的故事板中,我有一些使用AutoLayout約束手動添加的視圖,所以我不知道這是什麼在搞亂我的動畫。

+2

是的,這會導致問題,你一定不要使用自動佈局時設置框架屬性 - 你必須連接layoutconstrains作爲出口並操作它們。 – luk2302

+0

但這個動畫視圖沒有任何限制。如果我用'CGRectMake'編程創建了所有東西,它會好嗎? – Liumx31

+0

如果您的視圖沒有任何約束,那麼設置框架就可以了。給這個快速嘗試。 –

回答

0

在自動佈局,約束需要在動畫改變和更新,如下圖所示:

- (IBAction)moveAction:(id)sender { 
    self.spaceConstraint.constant += 220; 

    [UIView animateWithDuration:0.5 animations:^{ 
     [self.imageView layoutIfNeeded]; 
    }]; 
}