iPhone - designing my own viewController transition

后端 未结 1 482
广开言路
广开言路 2021-01-31 23:58

My app have a bunch of viewControllers and I am trying to design my own animation transition from one viewController to the other.

I have found this

- (v         


        
相关标签:
1条回答
  • 2021-02-01 00:24

    If you want to design your own transition you could create a category to UIViewController and write the animations yourself. There are only basic animations already available.

    This is how you could do the interface:

    *.h file:

    #import <UIKit/UIKit.h>
    
    @interface UIViewController(Transitions)
    
    - (void) presentViewController:(UIViewController *)viewController withPushDirection: (NSString *) direction;
    - (void) dismissViewControllerWithPushDirection:(NSString *) direction;
    
    @end
    

    *.m file

    #import "UIViewControllerWithTransitions.h"
    #import <QuartzCore/QuartzCore.h>
    
    @implementation UIViewController(Transitions)
    
    - (void) presentViewController:(UIViewController *)viewController withPushDirection: (NSString *) direction {
    
        [CATransaction begin];
    
        CATransition *transition = [CATransition animation];
        transition.type = kCATransitionPush;
        transition.subtype = direction;
        transition.duration = 0.25f;
        transition.fillMode = kCAFillModeForwards;
        transition.removedOnCompletion = YES;
    
        [[UIApplication sharedApplication].keyWindow.layer addAnimation:transition forKey:@"transition"];        
        [[UIApplication sharedApplication] beginIgnoringInteractionEvents];
        [CATransaction setCompletionBlock: ^ {
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(transition.duration * NSEC_PER_SEC)), dispatch_get_main_queue(), ^ {
                [[UIApplication sharedApplication] endIgnoringInteractionEvents];        
            });
        }];
    
        [self presentViewController:viewController animated:NO completion:NULL];
    
        [CATransaction commit];
    
    }
    
    - (void) dismissViewControllerWithPushDirection:(NSString *) direction {
    
        [CATransaction begin];
    
        CATransition *transition = [CATransition animation];
        transition.type = kCATransitionPush;
        transition.subtype = direction;
        transition.duration = 0.25f;
        transition.fillMode = kCAFillModeForwards;
        transition.removedOnCompletion = YES;
    
        [[UIApplication sharedApplication].keyWindow.layer addAnimation:transition forKey:@"transition"];        
        [[UIApplication sharedApplication] beginIgnoringInteractionEvents];
        [CATransaction setCompletionBlock: ^ {
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(transition.duration * NSEC_PER_SEC)), dispatch_get_main_queue(), ^ {
                [[UIApplication sharedApplication] endIgnoringInteractionEvents];        
            });
        }];
    
        [self dismissViewControllerAnimated:NO completion:NULL];
    
        [CATransaction commit];
    
    }
    
    @end
    

    and this is a sample call:

    [self presentViewController: myVC withPushDirection:@"fromRight"]; 
    
    0 讨论(0)
提交回复
热议问题