IOS (View controller transitions)

Source: Internet
Author: User
Tags diff uikit

转场需要提供转场代理,不使用默认的代理则需要自己实现代理方式,有UINavigationController、UITabBarController、UIViewController三种代理,实现以下三种协议
<UINavigationControllerDelegate>     //push和pop切换<UITabBarControllerDelegate>       //tab切换
<UIViewControllerTransitioningDelegate>  //UICollectionViewController 与 UINavigationController 结合的转场方式

转场触发时,需要UIKit 将要求转场代理将提供转场动画的核心构件:动画控制器和交互控制器


动画控制器(Animation Controller):

The most important part is responsible for adding the view and performing the animation, complying with <UIViewControllerAnimatedTransitioning> the agreement;

  

Interactive Controller

Through interactive means, usually gestures to drive animation controller implementation of the animation, so that users can control the entire process, abide by <UIViewControllerInteractiveTransitioning> the protocol, the system has been packaged ready-made classes for us to use

Transition environment (Transition context):

Provide the data needed in the transition, comply with the <UIViewControllerContextTransitioning> protocol, and be used by UIKit to generate and provide the animation controller and interactive controller that we submitted before the transition begins.

  

Transition Coordinator (Transition coordinator):

Other animations can be executed in parallel while the transition animation takes place, and its effect is less coordination than auxiliary, mainly used when Modal transitions and interactive transitions are canceled, rarely used at other times; <UIViewControllerTransitionCoordinator> UIKit generated during transitions, Uiviewcontroller on IOS 7 New method returns an transitionCoordinator() object that adheres to the protocol, and the method returns only one such object when the controller is in transition, and returns nil when the transition is not in progress

动画控制器协议实现:
 

Return animation Time

-(nstimeinterval) transitionduration: (ID<uiviewcontrollercontexttransitioning>) Transitioncontext {

return self . Duration;

}

Performing animations

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

// return to Container view , where transitions occur

// Get a view controller that participates in transitions, with uitransitioncontextfromviewcontrollerkey and Uitransitioncontexttoviewcontrollerkey of two keys.

// through Viewforkey: Gets the view that is Viewcontrollerforkey: the root view of the returned controller, or nil. Viewforkey: method returns nil in only one case : Modal transitions in uimodalpresentationcustom mode , the Presentingview obtained by this method will be niland will be explained in detail in the subsequent Modal transitions.

uiviewcontroller *fromvc = [Transitioncontext viewcontrollerforkey: Uitransitioncontextfromviewcontrollerkey];

uiviewcontroller *TOVC = [Transitioncontext viewcontrollerforkey: Uitransitioncontexttoviewcontrollerkey];

UIView *toview = ToVC. View;

UIView *fromview = Fromvc. View;

[self animatetransition: Transitioncontext fromvc: Fromvc toVC: ToVC Fromview: Fromview toview: Toview];

}

-(void) Animatetransition: (ID<uiviewcontrollercontexttransitioning>) Transitioncontext Fromvc: (uiviewcontroller *) Fromvc ToVC: (uiviewcontroller *) ToVC Fromview: ( UIView *) Fromview Toview: (UIView *) Toview {

//Add the Toview to the container

UIView* Containerview = [Transitioncontext containerview];

[Containerview Addsubview: Toview];

[Containerview sendsubviewtoback: Toview];

cgsize size = Toview. Frame. Size;

nsmutablearray *snapshots = [nsmutablearray new];

cgfloat xfactor = 10.0f;

cgfloat yfactor = xfactor * size. Height /size. width;

//Snapshot The From view, this makes subsequent snaphots more performant

UIView *fromviewsnapshot = [Fromview snapshotviewafterscreenupdates:NO];

//Create a snapshot for each of the exploding pieces

for (cgfloat x=0; x < size. width; x+= size. Width /xfactor) {

for (cgfloat y=0; y < size. Height; y+= size. Height /yfactor) {

cgrect snapshotregion = cgrectmake(x, y, size. Width /xfactor, size. Height /yfactor);

UIView *snapshot = [Fromviewsnapshot resizablesnapshotviewfromrect: snapshotregion Afterscreenupdates:NO withcapinsets:uiedgeinsetszero];

Snapshot. frame = snapshotregion;

[Containerview addsubview: snapshot];

[Snapshots addobject: snapshot];

}

}

[Containerview sendsubviewtoback: Fromview];

//Animate

nstimeinterval Duration = [self transitionduration: transitioncontext];

[UIView animatewithduration:d uration animations: ^{

for (UIView *view in snapshots) {

cgfloat xoffset = [self randomfloatbetween:-100.0 and :100.0];

cgfloat yoffset = [self randomfloatbetween:-100.0 and :100.0 ];

View. frame = cgrectoffset(view. Frame, Xoffset, yoffset);

View. Alpha = 0.0;

View. Transform = Cgaffinetransformscale(cgaffinetransformmakerotation( [ Self Randomfloatbetween:-10.0 and:10.0]), 0.01, 0.01);

}

} Completion: ^ (BOOL finished) {

for (UIView *view in snapshots) {

[View Removefromsuperview];

}

[Transitioncontext completetransition:![ Transitioncontext transitionwascancelled]];

}];

}

-(float)randomfloatbetween: (float)Smallnumber and: (float) bignumber {

float diff = bignumber-smallnumber;

return ((float) (arc4random()% ((unsigned)Rand_max + 1 )/ Rand_max) * diff) + smallnumber;

}

Reference Link: http://blog.devtang.com/2016/03/13/iOS-transition-guide/


  

IOS (View controller transitions)

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.