IOS live animation survey notes

Source: Internet
Author: User

IOS live animation survey notes
Preface

Due to project requirements, I have been investigating the live animation of iOS recently. For me, this is a field that has not been too involved before. Therefore, it is necessary to record the survey process and some of my own understandings.

Why do we need to customize an animation?

If you are interested in the interface Design and interaction changes of Material Design and some recent well-known apps (such as kuaidi and No. 1 vehicle), you will find a new trend: smooth page transition. The purpose is to make users feel as little as possible the hardware switching between pages, so that the App experience is smoother. IOS native has two types of common pass: Push/Pop and Present, which are obviously not in line with the current popular trend. Therefore, the significance of custom pass animation is embodied.

Transition-Introduction to custom transfer in iOS

I will not repeat the previous blog's detailed introduction to the custom pass-through. For details, refer to iOS7's custom View Controller switching effect (PS: thanks to the author ). I have also downloaded the demo of the author. He encapsulates a separate class for each transfer animation, and then implements a Transfer switching proxy in UIViewController to return the corresponding animation effect in the proxy. I agree with this topic for encapsulating a separate class for a pass animation, but I don't think it is ideal to implement a pass switching proxy in UIViewController, so I modified the implementation later. The final result is that in UIViewController, you only need to call an interface to implement the custom pass effect.

My design analysis

First, I encapsulated a single-instance mode MBTransition base class for two reasons:

In an App, only one event exists at the same time. After a singleton is implemented, the passing object does not need to depend on a certain UIViewController.

The. m file then provides several proxies for this class to implement the live animation.

# Pragma mark UINavigationControllerDelegate methods // Push/Pop: // parameter of the custom pass proxy //: // navigationController: navigation // operation: navigation operation: Push/Pop/None, it can be used to control which navigation operation to use custom pass // fromVC: UIViewController that executes the Push operation // toVC: UIViewController to be pushed-(id
  
   
) NavigationController :( UINavigationController *) navigationController animationControllerForOperation :( UINavigationControllerOperation) operation fromViewController :( UIViewController *) fromVC toViewController :( UIViewController *) toVC {return self ;} // when Present is used, the custom pass proxy // parameter: // presented: UIViewController of the Present // presenting: The UIViewController of the Present is being executed. // source: the UIViewController that initiates the Present (PS: there is a difference between executing the Present and initiating the UIViewController of the Present. If the source is a UIViewController under a UINavigationController, then the presenting is the UINavigationController, if the source is not in a control such as UINavigationController or UITabbarController, presenting is the source itself)-(id
   
    
) AnimationControllerForPresentedController :( UIViewController *) presented presentingController :( UIViewController *) presenting sourceController :( UIViewController *) source {return self;} // when Dismiss is used, the field proxy is defined. // parameter: // dismissed: The UIViewController-(id
    
     
) AnimationControllerForDismissedController :( UIViewController *) dismissed {return self;} # pragma mark-UIViewControllerContextTransitioning // proxy for customizing the animation effect, this proxy is also the core of the animation effect. // parameter: // transitionContext: context information at the time of the transfer-(void) animateTransition :( id
     
      
) TransitionContext {} // the proxy of the time when the animation is played // parameter: // transitionContext: context information at the time of the animation-(NSTimeInterval) transitionDuration :( id
      
        ) TransitionContext {return self. duration ;}
      
     
    
   
  

Through the above several proxies, we can know that the proxies of the pass animation during Push/Pop and Present are different, so I set up an enumeration of the pass type, used to control the interaction in which a custom pass is available:

Typedef enum TransitionType {TransitionTypePush, // Push/Pop transfer TransitionTypePresent // Present transfer} TransitionType;

Then- (NSTimeInterval)transitionDuration:(id )transitionContext The proxy returnsself.durationSo we need to add a variable in the. h file to save the duration of the Transition Animation:

@property (nonatomic, assign) NSTimeInterval duration;

Next, let's analyze(void)animateTransition:(id )transitionContext What we do in proxy:

PasstransitionContextObtain the two uiviewcontrollers that are switched during the transfer.

// Here fromVC and toVC represent the transition from fromVC to toVC. // For example, if fromVC is A and toVC is B, and B is Pop to, fromVC is the B interface, and toVC is the interface UIViewController * fromVC = [transitionContext viewControllerForKey: Secret]; UIViewController * toVC = [transitionContext viewControllerForKey: UITransitionContextToViewControllerKey]

PasstransitionContextObtain the UIView of the switchover.

// All switching animations are implemented based on the container. * container = [transitionContext containerView];

PasstransitionContextObtain the duration of the pass

NSTimeInterval duration = [self transitionDuration:transitionContext];

FinallytransitionContextObtain the fromVC and toVC saved when switching to the toVC and Push/Present at the time of the transfer. You can see whether the current execution is Push/Present or Pop/Dismiss, this allows you to customize different animation effects for Push/Present and Pop/Dismiss.

- (BOOL)isReversed:(UIViewController *)fromVC ToVC:(UIViewController *)toVC{    return !([self.fromVC class] == [fromVC class] && [self.toVC class] == [toVC class]);}

Therefore, we need to add two member variables in the. h file to save fromVC and toVC when pushing/Present:

@property (nonatomic, weak) UIViewController *fromVC;@property (nonatomic, weak) UIViewController *toVC;

The next step is the specific transfer animation part. In fact, it is to combine the fromVC view, toVC view, and container for some animation effects, because there is no difference with normal animation, so I will not describe this part in detail.

Finally, the interface provided for external calls is as follows:

- (void)setTransitionWithFromViewController:(UIViewController *)fromVC ToViewController:(UIViewController *)toVC TransitionType:(TransitionType)type Duration:(NSTimeInterval)duration{    self.fromVC = fromVC;    self.toVC = toVC;    self.duration = duration;    if (type == TransitionTypePush) {        self.fromVC.navigationController.delegate = self;    }else if (type == TransitionTypePresent){        self.fromVC.transitioningDelegate = self;        self.toVC.transitioningDelegate = self;    }}

What the above code snippet does is to save some parameters, and then set the corresponding proxy according to TransitionType.

Features if you want to implement other custom pass-through, you only need to inherit MBTransition and then override (void)animateTransition:(id )transitionContext Proxy. You only need to call one interface to implement custom transfer, reducing code coupling. Interactive switching Animation

Interactive Animation mainly refers to the process in which users can control the entire animation through gestures. I have not studied this part yet...

When UIViewController is a childViewController of UITabbarController or UINavigationController [transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey]And [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey]The obtained UIViewController is actually UITabbarController or UINavigationController. when calling the interface, note that the fromVC and toVC passed in are actually the UITabbarController or UINavigationController of the UIViewController.

If Push/Pop is used, when fromVC is a childViewController of UITabbarController and UITabbarController UITabBar cannot be displayed on toVC, UITabbarController's UITabBar will cause a lot of trouble:

If you set hidesBottomBarWhenPushed to true, the UITabBar animation cannot be customized. It can only be from right to left by default and from left to right by default. If you use custom methods to display and hide UITabBar, there will be more problems in the future due to AutoLayout...

In this case, we recommend that you use the Present method to switch to the new interface. If you have any good solutions, please share them with me. Thank you!

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.