christopher.online
christopher.online

Reputation: 2774

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:

A few more infos:

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

}

Upvotes: 1

Views: 3402

Answers (1)

Manu
Manu

Reputation: 788

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

Upvotes: 2

Related Questions