我的动画代码现在运行良好:
NSCalendar *cal = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *components = [cal components:( NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit | NSTimeZoneCalendarUnit) fromDate:[[AXDateManager sharedInstance] currentDate]];
if (forward) {
[UIView transitionWithView:self.tableView
duration:ANIMATIONDURATION
options:UIViewAnimationOptionTransitionCurlUp
animations:^{
/* maybe animation to hide your other views */
} completion:^(BOOL finished) {
/* remove the required views */
}];
[components setDay:components.day + 1];
} else {
[UIView transitionWithView:self.tableView
duration:ANIMATIONDURATION
options:UIViewAnimationOptionTransitionCurlDown
animations:^{
/* maybe animation to hide your other views */
} completion:^(BOOL finished) {
/* remove the required views */
}];
[components setDay:components.day - 1];
}
[self setupWithDate:[cal dateFromComponents:components]];
现在我想切换到从左到右的动画。但是我遇到了几个问题。我尝试过的一件事是用
复制原始tableviewNSData *archivedData = [NSKeyedArchiver archivedDataWithRootObject:self.tableView];
UITableView *tableView = [NSKeyedUnarchiver unarchiveObjectWithData:archivedData];
[tableView registerNib:[UINib nibWithNibName:@"Cell" bundle:nil] forCellReuseIdentifier:@"Cell"];
为新表设置委托和数据源,并从旧表中删除委托和数据源。但不知何故,新的一个从来没有得到正确的设置,所以它不会在其单元格上显示数据。这是我尝试使用的动画:
[UIView animateWithDuration:3
delay:0.0
options:UIViewAnimationOptionCurveEaseIn
animations:^{
CGRect oldTableRect = self.tableview.frame;
CGRect newTableRect = tableView.frame;
oldTableRect.origin.x = oldTableRect.origin.x - oldTableRect.size.width;
newTableRect.origin.x = newTableRect.origin.x - newTableRect.size.width;
self.tableView.frame = oldTableRect;
tableView.frame = newTableRect;
}
但是在上面的动画中,不知何故其中一个表从未移动过。
我想要达到的目标:
- 自我。tabelview应该向左移动离开屏幕 同时,一个新的表视图(已经加载了新的数据)将从左边出现,并占据self. tableview 的原始位置。
- 新表视图也将取代旧表视图,旧表视图将被完全移除
更多信息:
- self的UIViewController。tableview用storyboard 实例化
- self的委托和数据源。在viewdidload 中以编程方式设置tableview
最好的方法是什么?我可以实现从左到右的动画,只有一个表类似于我的原始动画卷曲上下?
编辑这就是我所尝试的。有两件事行不通。旧桌子不移到左边。新表正确移动,但没有设置数据。
if (forward) {
NSData *archivedData = [NSKeyedArchiver archivedDataWithRootObject:self.tableView];
UITableView *tableView = [NSKeyedUnarchiver unarchiveObjectWithData:archivedData];
[tableView registerNib:[UINib nibWithNibName:@"Cell" bundle:nil] forCellReuseIdentifier:@"Cell"];
// for debugging
[self.tableView setBackgroundColor:[UIColor yellowColor]];
[tableView setBackgroundColor:[UIColor redColor]];
tableView.frame = self.tableView.frame;
CGRect rect = tableView.frame;
// -20 for debugging
rect.origin.x = rect.origin.x + rect.size.width - 20;
tableView.frame = rect;
[self.view addSubview:tableView];
tableView.dataSource = self;
tableView.delegate = self;
// keep reference to old tableview
UITableView *old = self.tableView;
self.tableView = tableView;
// setup new tableview with data
[components setDay:components.day + 1];
[self setupWithDate:[cal dateFromComponents:components]];
CGRect oldTableRect = old.frame;
CGRect newTableRect = tableView.frame;
oldTableRect.origin.x = oldTableRect.origin.x - oldTableRect.size.width;
newTableRect.origin.x = newTableRect.origin.x - newTableRect.size.width;
[UIView animateWithDuration:3
delay:0.0
options:UIViewAnimationOptionCurveEaseIn
animations:^{
old.frame = oldTableRect;
self.tableView.frame = newTableRect;
}
completion:^(BOOL finished) {
[old removeFromSuperview];
}];
}
在动画块中声明和分配用于动画的变量对我来说听起来有点奇怪,试试这个:
CGRect oldTableRect = self.tableview.frame;
CGRect newTableRect = tableView.frame;
oldTableRect.origin.x = oldTableRect.origin.x - oldTableRect.size.width;
newTableRect.origin.x = newTableRect.origin.x - newTableRect.size.width;
[UIView animateWithDuration:3
delay:0.0
options:UIViewAnimationOptionCurveEaseIn
animations:^{
self.tableView.frame = oldTableRect;
tableView.frame = newTableRect;
}