简体   繁体   English

单个视图的iOS动画从左到右

[英]iOS animation of single View left to right

My animation code right now which works fine: 我的动画代码现在可以正常工作:

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

Now I want to switch to an animation from left to right. 现在,我想从左到右切换到动画。 But I ran into several problems. 但是我遇到了几个问题。 One of the things I tried was copying the original tableview with 我尝试的一件事是复制原始tableview与

NSData *archivedData = [NSKeyedArchiver archivedDataWithRootObject:self.tableView];
UITableView *tableView =  [NSKeyedUnarchiver unarchiveObjectWithData:archivedData];     
[tableView registerNib:[UINib nibWithNibName:@"Cell" bundle:nil] forCellReuseIdentifier:@"Cell"];

And setting up the delegate and datasource to the new table and removing the delegate and datasource from the old one. 并将委托和数据源设置到新表,并从旧表中删除委托和数据源。 But somehow the new one never get setup properly so it would not display data on its cells. 但是以某种方式,新的设备永远无法正确安装,因此不会在其单元上显示数据。 This was the animation I tried to use: 这是我尝试使用的动画:

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

But with the above animation somehow one of the tables never moved. 但是,使用上面的动画,其中一个表从未动过。

What I want to achieve: 我要实现的目标:

  • self.tabelview shall move to left off the screen self.tabelview应该移到屏幕左侧
  • at the same time a new tableview (already loaded with new data) shall come from the left and take the original position of self.tablview 同时,新的表格视图(已经加载了新数据)应从左侧出现,并占据self.tablview的原始位置
  • the new tableview shall also replace the old one and the old one shall be completely removed 新的tableview也将替换旧的viewview,并且旧的viewview将被完全删除

A few more infos: 更多信息:

  • the UIViewController of self.tableview was instantiated with the storyboard self.tableview的UIViewController已通过情节提要进行实例化
  • delegate and datasource of self.tableview were set programmatically in viewdidload self.tableview的委托和数据源在viewdidload中以编程方式设置

What is the best approach? 最好的方法是什么? Can I achieve the left to right animation with just one table similarly to my original animation with the curling up and down? 我可以只用一张桌子来实现从左到右的动画,而与原来的动画一样,可以上下卷曲吗?

EDIT So this is what I tried. 编辑所以这就是我尝试的。 Two things that do not work. 两件事不起作用。 Old table does not move to the left side. 旧表不会移到左侧。 The new table moves in correctly but does not get set up with data. 新表可以正确移动,但是无法设置数据。

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

}

declaring and assigning the variable that you use for the animation into the animation block sounds a little bit weird for me, try this: 声明并分配用于动画的变量到动画块中对我来说听起来有些奇怪,请尝试以下操作:

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

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

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