简体   繁体   English

如何为从iPhone导航栏下方滑动的视图设置动画?

[英]How can I animate a view sliding from under the nav bar on iPhone?

This is related to my last question . 这与我的最后一个问题有关

I have an application that I created from the "Navigation-based application" template in Xcode. 我有一个从Xcode中的“基于导航的应用程序”模板创建的应用程序。 (I have a UINavigationController with a UITableViewController subclass named RootViewController.) (我有一个UINavigationController和一个名为RootViewController的UITableViewController子类。)

The table shows a list of strings the user can interact with. 该表显示了用户可以与之交互的字符串列表。 The nav bar has the standard Edit/Done button on the left, and a Plus-sign button on the right. 导航栏在左侧具有标准的“编辑/完成”按钮,在右侧具有加号按钮。 I would like to slide a view loaded from a .xib file from under the nav bar when the user touches the plus button. 当用户触摸加号按钮时,我想从导航栏下方滑动从.xib文件加载的视图。

I attempted to implement it by doing the following: 我尝试通过执行以下操作来实现它:

NSArray *xibContents = [[NSBundle mainBundle] loadNibNamed:@"NewNameView" owner:self options:nil];
UIView *view = [xibContents objectAtIndex:0];
[self.view addSubview:view];
view.frame = CGRectMake(view.frame.origin.x, view.frame.origin.y - 44, view.frame.size.width, view.frame.size.height);
[UIView beginAnimations:@"openAdd" context:nil];
view.frame = CGRectMake(view.frame.origin.x, view.frame.origin.y + 44, view.frame.size.width, view.frame.size.height);
[UIView commitAnimations];

(The height of the view is 44 pixels.) This animates the view sliding down, but it covers the first table cell. (视图的高度为44像素。)这使视图向下滑动,但它覆盖了第一个表格单元格。 I'd like for the table view to slide down as well. 我也希望表格视图向下滑动。

The best way to do this to use directly UITableView methods. 最好的方法是直接使用UITableView方法。

//Update your data model with the new row

[self.tableView beginUpdates];

NSArray* indexPaths = [[NSArray alloc] initWithObjects:[NSIndexPath indexPathForRow:0 inSection:0], nil];
[self.tableView insertRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationTop];
[indexPaths release];

[self.tableView endUpdates];

Your new row will looks like it would come from under the Navigation bar. 您的新行看起来将像来自导航栏。

For more info on batch insert/deleting of row and sections . 有关批量插入/删除行和节的更多信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM