How do I do a Fade/No transition between view controllers

前端 未结 10 775
时光取名叫无心
时光取名叫无心 2020-12-07 08:48

Is it possible to do a fade in and fade out transition between View Controllers in Storyboard. Or without transition.

If it\'s possible, what\'s the code for it?

10条回答
  •  甜味超标
    2020-12-07 09:14

    For creating Custom Segue create subclass of UIStoryboard segue. For example:

    // MCFadeSegue.h
    #import 
    
    @interface MCFadeSegue : UIStoryboardSegue
    
    @end
    
    // MCFadeSegue.m
    #import 
    #import "MCFadeSegue.h"
    
    @implementation MCFadeSegue
    
    - (void)perform
    {
      CATransition *transition = [CATransition animation];
      transition.duration = 0.5;
      transition.type = kCATransitionFade;
    
      [[[[[self sourceViewController] view] window] layer] addAnimation:transition
          forKey:kCATransitionFade];
    
      [[self sourceViewController]
          presentViewController:[self destinationViewController]
          animated:NO completion:NULL];
    }
    
    @end
    

    Then in MainStoryboard.storyboard choose segue and set Style:Custom and Class:MCFadeSegue.

提交回复
热议问题