iOS 模态动画2

效果:


11月-06-2017 11-06-14.gif

新建一个类MJAnimationVertical基于NSObject

重写


- (NSTimeInterval)transitionDuration:(id )transitionContext {
    return 2.0f;
}
- (void)animateTransition:(id )transitionContext 
{
    //获取fromVC与toVC
    UIViewController *fromVC = [transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];
    UIViewController *toVC = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];
    
    //通过上下文获取跳转的view
    UIView *containerView = [transitionContext containerView];
    
    //将fromVC截图
    UIView *mainSnap = [fromVC.view snapshotViewAfterScreenUpdates:NO];
    
    //fromVC分割成宽度为5的view
    NSArray *outgoingLineViews = [self cutView:mainSnap intoSlicesOfWidth:5];
    
    //将分割出来的view加入到主页面,用作动画
    for (int i = 0; i 

剪切界面的方法

-(NSMutableArray *)cutView:(UIView *)view intoSlicesOfWidth:(float)width{
    
    CGFloat lineHeight = CGRectGetHeight(view.frame);
    
    NSMutableArray *lineViews = [NSMutableArray array];
    UIView *subsnapshot;
    for (int x=0; x

将剪切的line参差分不到界面
用在从fromView的消失动画上

-(void)repositionViewSlices:(NSArray *)views moveFirstFrameUp:(BOOL)startUp{
    
    BOOL up = startUp;
    CGRect frame;
    float height;
    for (UIView *line in views) {
        frame = line.frame;
        height = CGRectGetHeight(frame) * RANDOM_FLOAT(1.0, 4.0);
        
        frame.origin.y += (up)?-height:height;
        line.frame = frame;
        
        up = !up;
    }
}

将参差不齐的line统一到界面
用在toView的出现动画上

-(void)resetViewSlices:(NSArray *)views toYOrigin:(CGFloat)y{
    
    CGRect frame;
    for (UIView *line in views) {
        frame = line.frame;
        frame.origin.y = y;
        line.frame = frame;
    }
}

在两个VC之中的调用方式,跟上一文章没差

//返回一个管理动画过渡的对象
-(nullable id )animationControllerForPresentedController:(UIViewController *)presented presentingController:(UIViewController *)presenting sourceController:(UIViewController *)source
{
    if (!self.animationVertical) {
        self.animationVertical = [MJAnimationVertical new];
    }
    return self.animationVertical;
}

- (nullable id )animationControllerForDismissedController:(UIViewController *)dismissed{
    if(!self.animationVertical){
        self.animationVertical = [[MJAnimationVertical alloc] init];
    }
    return self.animationVertical;
}

你可能感兴趣的:(iOS 模态动画2)