Present Modal View with Animation Effect

2019-06-13 15:50发布

问题:

In my iPhone App I need to display a modal view with transparent background and it should appear with animation like it is appearing from center of view and its size is increasing.

similar like "drawing something" iPhone App when we click on settings button.

How do I do this?

回答1:

You can do one of 4 following transition styles:

viewController.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
viewController.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
viewController.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
viewController.modalTransitionStyle = UIModalTransitionStylePartialCurl;

[self presentModalViewController:viewController animated:YES];

If you want something that is not included in these defaults you are going to have to build your own custom animation for presenting the modal view. Like the following but obviously for the style you want.

UIModalTransitionStyle horizontal movement



回答2:

Let's say you have a viewController thats called aScoreSheet that you want to present. Try to define this method in the view controller that's going to do the presenting.

-(void) presentTransparentModalViewController: (ScoreSheet *) aViewController 
{

    scoreSheet = aViewController;
    UIView *view = aViewController.view;

view.opaque = NO;
[view.subviews enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
    UIView *each = obj;
    each.opaque = NO;
}];
    [self.view addSubview:view];

    view.center = CGPointMake(160, 800); //for iPhone

    [UIView animateWithDuration:0.9 delay:0 options:UIViewAnimationCurveEaseInOut animations:^{
         view.center = CGPointMake(160, 240);
    } completion:^(BOOL finished) {

        self.view.userInteractionEnabled=YES;
    }];

}

and then to dismiss the controller:

-(void) dismissTransparentModalViewControllerAnimated:(BOOL) animated{

if (animated) {

    [UIView animateWithDuration:0.4
                     animations:^{
                         scoreSheet.view.center = CGPointMake(scoreSheet.view.center.x, scoreSheet.view.center.y + 480);
                     } completion:^(BOOL finished) {
                         [scoreSheet.view removeFromSuperview];
                         scoreSheet = nil;
                     }];
}


}


回答3:

Not a full answer, but maybe you can take a look at this open source library:

https://github.com/Split82/HMGLTransitions

It has some custom modal transitions, maybe not exactly the one you are looking for, but you can easily add your transition by subclassing HMGLTransition.

Hope this helps