我的动画代码现在正常工作:
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;
}
但是上面的动画不知怎的,其中一张桌子从未动过。
我想要实现的目标:
更多信息:
最好的方法是什么?我可以使用一张桌子来实现从左到右的动画,类似于原始动画,上下卷曲吗?
修改 所以这就是我的尝试。两件事不起作用。旧桌子不向左侧移动。新表正确移动,但没有设置数据。
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];
}];
}
答案 0 :(得分:2)
声明并将用于动画的变量分配到动画块中对我来说听起来有点奇怪,试试这个:
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;
}