简体   繁体   中英

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

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
  • 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
  • the new tableview shall also replace the old one and the old one shall be completely removed

A few more infos:

  • the UIViewController of self.tableview was instantiated with the storyboard
  • delegate and datasource of self.tableview were set programmatically in 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;
                         }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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