1

我的动画代码现在可以正常工作:

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]];

现在我想从左到右切换到动画。但是我遇到了几个问题。我尝试的一件事是复制原始的tableview

NSData *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;
 }

但是在上面的动画中,其中一张桌子从未移动过。

我想要达到的目标:

  • self.tabelview 将移到屏幕左侧
  • 同时一个新的tableview(已经加载了新数据)应该从左边出现并占据self.tablview的原始位置
  • 新的tableview也将替换旧的,旧的将被完全删除

还有一些信息:

  • self.tableview 的 UIViewController 是用故事板实例化的
  • self.tableview 的委托和数据源在 viewdidload 中以编程方式设置

最好的方法是什么?我可以只用一张桌子实现从左到右的动画,就像我原来的向上和向下卷曲的动画一样吗?

编辑 所以这就是我尝试过的。两件事不起作用。旧表不向左侧移动。新表正确移入,但未设置数据。

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];
     }];

}
4

1 回答 1

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;
                         }
于 2013-05-31T14:10:50.543 回答