2015-05-29 32 views
0

我有一個我在xib文件中創建的子視圖。它在我最終希望的屏幕中間。我正在嘗試對它進行動畫處理,以便在按下按鈕以顯示視圖時它會從底部移動到中間,並且在視圖關閉時我希望它動畫到底部。它現在的方式是從xib中間開始,然後從那裏開始動畫。我怎樣才能讓它從底部開始並移動到中間?Animate Subview to Middle

In viewDidLoad:我設置了起始幀。

CGRect newFrame = self.findFriendView.frame; 
    newFrame.origin.y += 1000; 
    self.findFriendView.frame = newFrame; 
    self.findFriendView.hidden = YES; 

這裏是按鈕顯示子視圖的方法。

- (void)addFriend { 
    self.followersTableView.userInteractionEnabled = NO; 
    self.findFriendView.hidden = NO; 

    CGRect newFrame = self.findFriendView.frame; 
    newFrame.origin.y -= 1000; 

    [UIView animateWithDuration:1.0 
        animations:^{self.findFriendView.frame = newFrame;} 
        completion:nil]; 
} 

enter image description here

+0

如何您可以設置幀,當您在使用自動佈局來項目?爲了做動畫你必須更新約束。 – Vizllx

+0

如果你在自動佈局,你需要修改_constraint_而不是_frame _... – holex

回答

0

這似乎是這樣做的一個非常奇怪的方式。爲什麼不在故事板中創建第二個視圖控制器並將其作爲模態表單呈現?

然後,你可以只做到以下幾點:

UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"iPad" bundle:nil]; 
YourNewViewController *viewController = [storyboard instantiateViewControllerWithIdentifier:@"YourNewViewControllerID"]; 

viewController.modalTransitionStyle = UIModalTransitionStyleCoverVertical; 
viewController.modalPresentationStyle = UIModalPresentationFormSheet; 
[self presentViewController:viewController animated:YES completion:nil]; 

,如果你需要的任何數據發送到新的控制器,你可以把這個新控制器的.H:

@property (nonatomic, readwrite, assign) NSDictionary * IncomingData; 

,然後創建一個NSDictionary包含在您的主視圖想要的任何數據,並與以下行(把它只是presentviewcontroller前)發送:

viewController.IncomingData = tempDict; 

編輯:

OP沒有用故事板等等,而不是:

UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"iPad" bundle:nil]; 
YourNewViewController *viewController = [storyboard instantiateViewControllerWithIdentifier:@"YourNewViewControllerID"]; 

你需要做的:

YourNewViewController = [[YourNewViewController alloc] initWithNibName:@"YourNewViewController" bundle:nil]; 
+0

我不使用故事板 – raginggoat

+0

查看更新的答案。 – Compy

+0

當我關閉新的視圖控制器後,如何重新加載視圖控制器中的表視圖我正在展示新的控制器? – raginggoat