2016-03-08 29 views
0

這裏是我的問題,我想動畫無法移動的UITextView編程

我想讓我的用戶名欄中顯示出來的我的登錄屏幕,這是一個基本的屏幕,2 UITextView的和2個按鈕(登錄並註冊)當應用程序啓動和動畫,所以這裏的屏幕是我的代碼:

override func viewWillAppear(animated: Bool) { 
    self.navigationController?.navigationBarHidden = true 
    print (usernameField.center.x) 

    usernameField.center.x -= view.bounds.width 
    print (view.bounds.width) 
    print (usernameField.center.x) 

} 

這裏是輸出:

300.0 
375.0 
-75.0 

所以我usernameField的最後x爲-75.0,但在所有的情況下,它以我的應用程序爲中心。

我認爲這是因爲我在Storyboard中應用了約束條件? (建議該中心按鈕和TextView的約束)

感謝您的幫助

編輯: 消除限制並沒有解決我的問題..

+0

嘗試做所有的幀相關的計算中viewDidLayoutSubviews。順便說一句,如果你使用約束不要嘗試改變視圖的框架,因爲這些改變被忽略或產生意想不到的行爲。 – azimov

回答

1

您將需要保持參照約束與最近的鄰居/邊緣保持領先的空間。然後爲此約束的常量屬性設置適當的值。然後進行動畫製作,您需要在動畫塊中調用layoutIfNeeded方法中的 。

最初確保您的視圖超出界限並朝向視圖的左側。

有約束的出口說 -

@IBOutlet weak var constraint : NSLayoutConstraint! 

override func animateIn(){ 
    //Do any pending layout 
    self.view.layoutIfNeeded() 
    constraint.constant += 100 //<-- set appropriate value for your case 
    //Animate the constraint change to bring in the button into the screen 

    UIView.animateWithDuration(0.2, animations: { 
    self.view.layoutIfNeeded() 
}) 

} 

參見: How do I animate constraint changes?