65th, iOS7 custom transitions Animation

Source: Internet
Author: User
Tags uikit

Custom transitions animations are only beginning to appear in iOS7 and above, and in some applications we often need to customize the jump animations

1. Compliance with the Agreement:<uiviewcontrolleranimatedtransitioning>

2. The main method of the agreement is two:

1. (
2. (void) Animatetransition: (ID <UIViewControllerContextTransitioning>) transitioncontext;


#import <Foundation/Foundation.h>#import <UIKit/UIKit.h>typedef ns_enum ( Nsuinteger, Hybcontrollertransitiontype) {  11,  12  }; @interface Hybcontrollertransition:nsobject <uiviewcontrolleranimatedtransitioning>+ (instancetype) Transitionwithtype: (Hybcontrollertransitiontype) Transitiontype                          Duration: (nstimeinterval) duration; @end

#import "HYBControllerTransition.h"#import "ViewController.h"#import "DetailController.h"@interfacehybcontrollertransition () @property (nonatomic, assign) Hybcontrollertransitiontype Transitiontype; @property ( Nonatomic, assign) nstimeinterval duration;@end@implementationhybcontrollertransition-(instancetype) init {if(self =[Super Init]) {Self.transitiontype=Kcontrollertransitionpush; }    returnSelf ;}+( instancetype) Transitionwithtype: (Hybcontrollertransitiontype) Transitiontype Duration: (nstim Einterval) Duration {hybcontrollertransition*transition =[[Hybcontrollertransition alloc] init]; Transition.transitiontype=Transitiontype; Transition.duration=duration; returntransition;}#pragmamark-uiviewcontrolleranimatedtransitioning-(void) Animatetransition: (ID<UIViewControllerContextTransitioning>) Transitioncontext {Switch(self.transitiontype) { CaseKcontrollertransitionpush: {[self push:transitioncontext];  Break; }     CaseKcontrollertransitionpop: {[self pop:transitioncontext];  Break; }    default: {       Break; }  }}-(Nstimeinterval) Transitionduration: (ID<UIViewControllerContextTransitioning>) Transitioncontext {returnself.duration;}- (void) animationended: (BOOL) transitioncompleted {NSLog (@"%s", __function__);}#pragmamark-private-(void) Pop: (ID<UIViewControllerContextTransitioning>) Transitioncontext {Detailcontroller*FROMVC =[Transitioncontext Viewcontrollerforkey:uitransitioncontextfromviewcontrollerkey]; Viewcontroller*TOVC =[Transitioncontext Viewcontrollerforkey:uitransitioncontexttoviewcontrollerkey]; UIView*containerview =[Transitioncontext Containerview]; UIView*toimageview = tovc.isimg1?ToVC.img1:toVC.img2; UIView*tempview =ContainerView.subviews.lastObject; //The first view is a Fromvc.view//The second view is the toimageview generated when push comes in.   for(UIView *viewinchcontainerview.subviews) {NSLog (@"%@", view); if(Fromvc.view = =view) {NSLog (@"YES"); }} Toimageview.hidden=YES; Tempview.hidden=NO; //Tovc.view must be placed on top, that is, the first position[Containerview InsertSubview:toVC.view Atindex:0]; [UIView animateWithDuration:self.duration delay:0.0usingspringwithdamping:0.55initialspringvelocity:1/0.55Options:0Animations:^{FromVC.view.alpha=0.0; Tempview.frame=[Toimageview convertRect:toImageView.bounds Toview:containerview]; } Completion:^(BOOL finished) {Tempview.hidden=NO; Toimageview.hidden=NO;        [Tempview Removefromsuperview];        [Transitioncontext Completetransition:yes];  for(UIView *viewinchcontainerview.subviews) {NSLog (@"%@", view); if(Tovc.view = =view) {NSLog (@"YES"); }    }  }];}- (void) Push: (ID<UIViewControllerContextTransitioning>) Transitioncontext {Viewcontroller*FROMVC =[Transitioncontext Viewcontrollerforkey:uitransitioncontextfromviewcontrollerkey]; Detailcontroller*TOVC =[Transitioncontext Viewcontrollerforkey:uitransitioncontexttoviewcontrollerkey]; UIView*containerview =[Transitioncontext Containerview]; UIView*fromimageview = fromvc.isimg1?FromVC.img1:fromVC.img2; UIView*tempview =[Fromimageview Snapshotviewafterscreenupdates:no]; Tempview.frame=[Fromimageview convertRect:fromImageView.bounds Toview:containerview]; UIView*toimageview =Tovc.imgview; Fromimageview.hidden=YES; ToVC.view.alpha=0.0; Toimageview.hidden=YES;  [Containerview AddSubview:toVC.view];    [Containerview Addsubview:tempview]; [UIView animateWithDuration:self.duration delay:0.0usingspringwithdamping:0.55initialspringvelocity:1/0.55Options:0Animations:^{ToVC.view.alpha=1.0; Tempview.frame=[Toimageview convertRect:toImageView.bounds Toview:containerview]; } Completion:^(BOOL finished) {Tempview.hidden=YES; Toimageview.hidden=NO;                   [Transitioncontext Completetransition:yes]; }];}@end

Use the method of complying with the NAV agreement:

#pragmaMark-uinavigationcontrollerdelegate-(ID<UIViewControllerInteractiveTransitioning>) Navigationcontroller: (Uinavigationcontroller *) Navigationcontroller Interactioncontrollerforanimationcontroller: (ID<UIViewControllerAnimatedTransitioning>) Animationcontroller {returnNil;}- (ID<UIViewControllerAnimatedTransitioning>) Navigationcontroller: (Uinavigationcontroller *) Navigationcontroller animationcontrollerforoperation: (uinavigationcontrolleroperation) operation Fromviewcontroller: (Uiviewcontroller *) Fromvc Toviewcontroller: (Uiviewcontroller *) ToVC {if(Operation = =Uinavigationcontrolleroperationpush) {    return[Hybcontrollertransition transitionwithtype:kcontrollertransitionpush Duration:0.75]; } Else {    return[Hybcontrollertransition transitionwithtype:kcontrollertransitionpop Duration:0.75]; }}

65th, iOS7 custom transitions Animation

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.