IOS easy steps to implement custom transitions animation _ios

Source: Internet
Author: User

One, the system provides the animation of the transitions

At present, the system provides us push/pops with present/dismiss two kinds of controllers between the jump side. Of course, by setting UIModalTransitionStyle properties, you can achieve the following 4 kinds of modal effect, I believe we are more familiar with, here will no longer show the effect of the map.

 Uimodaltransitionstylecoververtical//   from bottom up,
 uimodaltransitionstylefliphorizontal  //Horizontal Flip
 Uimodaltransitionstylecrossdissolve   //Fade in
 uimodaltransitionstylepartialcurl   //Roll Corner page

Custom Transitions Animation usage scenes

The system gives us less animations for transitions, and sometimes you want to make some changes to the effect of the system. For example: We use the fading modal effect, the system's animation time we are not satisfied with the hope that the animation faster, or slow. The system does not provide me with directly modified properties or methods, then, At this point we have to consider using custom transition animations to achieve what we want.

Third, the implementation of custom transitions animation steps

1th Step: Set up the need for a pop-up controller modalPresentationStyle forUIModalPresentationCustom

 Detailvc.modalpresentationstyle = Uimodalpresentationcustom;

2nd step: Set up the transition agent

Detailvc.transitioningdelegate = Self

Can draw a tool class, specifically responsible for transition animation

3rd Step: Comply with UIViewControllerTransitioningDelegate agent implementation of two methods

Determine who is responsible for ejecting animations

-(id<uiviewcontrolleranimatedtransitioning>) Animationcontrollerforpresentedcontroller: (UIViewController * ) presented Presentingcontroller: (Uiviewcontroller *) presenting Sourcecontroller: (Uiviewcontroller *) source {
 self.ispresnted = YES;
 return self;
}

Determine who is responsible for the animation disappearing

-(id<uiviewcontrolleranimatedtransitioning>) Animationcontrollerfordismissedcontroller: (UIViewController * ) dismissed {
 self.ispresnted = NO;
 return self;
}

4th step: Realize animation of transitions

The above all returns self, which shows that the current controller is responsible for ejecting the animation and is responsible for the animation disappear, so the specific transition animation in the current controller.

Determine Transitions animation time interval

-(Nstimeinterval) Transitionduration: (id<uiviewcontrollercontexttransitioning>) TransitionContext {
 return 1.0;
}

Then implement the specific animation (because the current controller is responsible for pop-up animation, but also responsible for vanishing animation, so define a Bool type attribute to isPresnted differentiate)

-(void) Animatetransition: (id<uiviewcontrollercontexttransitioning>) Transitioncontext {

 if ( self.ispresnted = = YES) {
  //here do pop-up animation

 } else {

 //here do vanishing animation}
}

Summary:

You can also have different controllers responsible for pop-up and vanishing animations, this makes it unnecessary to define a property that is specifically used to differentiate. Personal advice it is best to extract a tool class that specializes in transition animations, so that the tool class is responsible for all transitions animations, which is better for both business logic and code reusability. Of course, One drawback of this is that some of the attributes that need to be used in a transition animation must be passed in by defining variables, proxies, and blocks, while using different controllers to manage pop-up and vanishing animations is more convenient for getting properties. What to do, but also to the beholder.

The following to achieve a specific transition animation effect bar

Because the core code to implement animation in the fourth step, here is only to see animateTransition: this method!

The modal effect from the top down

-(void) Animatetransition: (id<uiviewcontrollercontexttransitioning>) Transitioncontext {if (self.isPresnted =
  = YES) {//1. Remove view UIView *presentedview = [Transitioncontext Viewforkey:uitransitioncontexttoviewkey];
  2. Put into Containerview [[Transitioncontext Containerview]addsubview:presentedview];
  3. Set basic Properties Presentedview.frame = CGRectMake (0,-667, 375, 667); 4. Animation [UIView animatewithduration:[self transitionduration:transitioncontext] animations:^{presentedView.frame = CG
  Rectmake (0, 0, 375, 667);
  }completion:^ (BOOL finished) {[Transitioncontext completetransition:yes];

 }];
  else {//1. Remove view UIView *dismissedview = [Transitioncontext Viewforkey:uitransitioncontextfromviewkey];

  2. Put into Containerview [[Transitioncontext Containerview]addsubview:dismissedview]; 3. Animation [UIView animatewithduration:[self Transitionduration:transitioncontext] animations:^{dismissedView.frame =CG
  Rectmake (0,-667, 375, 667);
}completion:^ (BOOL finished) {
   [Transitioncontext Completetransition:yes];

 }]; }
}


Simulate system fade effect

-(void) Animatetransition: (id<uiviewcontrollercontexttransitioning>) Transitioncontext {if (self.isPresnted =
  = YES) {//1. Remove view UIView *presentedview = [Transitioncontext Viewforkey:uitransitioncontexttoviewkey];
  2. Put into Containerview [[Transitioncontext Containerview]addsubview:presentedview];
  3. Set the Basic property Presentedview.alpha = 0; 4. Animation [UIView animatewithduration:[self transitionduration:transitioncontext] animations:^{presentedView.alpha = 1.
  0;

  }completion:^ (BOOL finished) {[Transitioncontext completetransition:yes];

 }];
  else {//1. Remove view UIView *dismissedview = [Transitioncontext Viewforkey:uitransitioncontextfromviewkey];
  2. Put into Containerview [[Transitioncontext Containerview]addsubview:dismissedview];
  3. Set the Basic property Dismissedview.alpha = 1; 
  4. Animation [UIView animatewithduration:[self transitionduration:transitioncontext] animations:^{dismissedView.alpha = 0; }completion:^ (BOOL finished) {[Transitioncontext completetransitIon:yes];
 }]; }
}


The time of the animation can be obtained from the ransitionDuration: method

UITransitionContextToViewKey UITransitionContextFromViewKey Key get pop-up and vanishing animations by and two individually View (note don't confuse two Key )

To add an animation View to a specific animation containerView

Summarize

This is the whole story of this article, here only describes how to achieve the transition animation, as for other fun NB effect, we can try to achieve, small series will also share other better results, I hope this article for everyone can have some help, if there is doubt you can message exchange.

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.