Bad_APPZ
Bad_APPZ

Reputation: 432

View Transition Animations

When you call:

[self dismissViewControllerAnimated:Yes.....];

it does the opposite of whatever animation was used to present the view.

Ex: If you call:

UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Storyboard" bundle:nil];
View * myView = (View *) [storyboard instantiateViewControllerWithIdentifier:@"myViewName"];
[self myView animated:YES completion:nil];

It brings the view up from the bottom, and dismissViewControllerAnimated: would dismiss the view using the reverse animation, so the view would be sliding down.

Is there any way to Present a view with the sliding down animation? Any Help is greatly appreciated!

Upvotes: 0

Views: 539

Answers (2)

rdelmar
rdelmar

Reputation: 104082

Sure, you can do your own transition like so:

 -(IBAction)presentNext:(id)sender {

    UIViewController *next = [self.storyboard instantiateViewControllerWithIdentifier:@"Next"];
    [self.view.window insertSubview:next.view belowSubview:self.view];
    CGAffineTransform t;
    __block NSInteger xOffset, yOffset;
    switch (self.interfaceOrientation) {
        case 1:
            xOffset = 0;
            yOffset = self.view.frame.size.height;
            break;
        case 3:
            t = CGAffineTransformMakeRotation(90 * M_PI / 180);
            next.view.transform = t;
            xOffset = -self.view.frame.size.width;
            yOffset = 0;
            break;
        case 4:
            t = CGAffineTransformMakeRotation(-90 * M_PI / 180);
            next.view.transform = t;
            xOffset = self.view.frame.size.width;
            yOffset = 0;
            break;
        default:
            break;
    }

    next.view.frame = self.view.frame;
    [UIView animateWithDuration:1 animations:^{
        self.view.center = CGPointMake(self.view.center.x + xOffset, self.view.center.y + yOffset);
    } completion:^(BOOL finished) {
        [self presentViewController:next animated:NO completion:^{
            [self.view removeFromSuperview];
        }];
    }];
}

Upvotes: 0

Paresh Navadiya
Paresh Navadiya

Reputation: 38239

Use CATransaction for animation.

Note: the below code depends on UIInterfaceOrientation for Presenting a view with the sliding down animation in all orienation.

[CATransaction begin];
CATransition *transition;
transition = [CATransition animation];
transition.type = kCATransitionPush;
if(viewOrientation == UIInterfaceOrientationLandscapeLeft)
    [transition setSubtype:kCATransitionFromLeft];
else if (viewOrientation == UIInterfaceOrientationLandscapeRight)
    [transition setSubtype:kCATransitionFromRight];
else if (viewOrientation == UIInterfaceOrientationPortrait)
    [transition setSubtype:kCATransitionFromLeft];
else 
    [transition setSubtype:kCATransitionFromRight];

transition.duration = 0.0;
[self.view.layer addAnimation:transition forKey:nil]; // add animation in layer of UIView.
//Present your View here
[CATransaction commit];

EDIT : In these methods set current orientation.So Add UIInterfaceOrientation viewOrientation; in .h file. These method may change according to iOS version

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
   viewOrientation = interfaceOrientation;
   return YES; //return according to your requirement
}

-(void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
  viewOrientation = toInterfaceOrientation;
}

Upvotes: 1

Related Questions