ios_20_ Weibo customizable navigation controller for animated transitions

Source: Internet
Author: User
Tags uikit

Final effect:





AnimatedNavigationController.h

  animatednavigationcontroller.h//  20_ Handsome no Weibo////  Created by Beyond on 14-8-10.//  Copyright (c) 2014 Com.beyond. All rights reserved.//  inherits from the navigation controller, but has a feature that allows you to listen for gestures, animate transitions #import <UIKit/UIKit.h> @interface Animatednavigationcontroller:uinavigationcontroller@end
ANIMATEDNAVIGATIONCONTROLLER.M
animatednavigationcontroller.m//20_ Handsome no Weibo////Created by Beyond on 14-8-10.//Copyright (c) 2014 Com.beyond. All rights reserved.//#import "AnimatedNavigationController.h"//For #import <QuartzCore/QuartzCore.h> #import "    BeyondViewController.h "@interface Animatednavigationcontroller () {//Screen Uiimageview *_screenshotimgview;        Above the black translucent matte UIView *_coverview; Store all Nsmutablearray *_screenshotimgs;}    @end @implementation animatednavigationcontroller-(void) viewdidload{[Super Viewdidload]; 1, create a pan gesture recognizer, and bind the Listener method Uipangesturerecognizer *pangesturerec = [[Uipangesturerecognizer alloc]initwithtarget:self    Action: @selector (Pangesturerec:)];                Add a pan gesture recognizer for the view of the navigation controller [Self.view Addgesturerecognizer:pangesturerec];    2. Created ImageView _screenshotimgview = [[Uiimageview alloc] init];    The app's frame is the frame _screenshotimgview.frame = [UIScreen mainscreen].applicationframe, which removes the height of the status bar;        (0 20; 320 460);  3. Create a black translucent matte above  _coverview = [[UIView alloc] init];    The frame of the mask is the frame _coverview.frame = _screenshotimgview.frame;        Mask is black _coverview.backgroundcolor = [Uicolor blackcolor];            4. Store all array initialization _screenshotimgs = [Nsmutablearray array]; }//rewrite the push method to intercept the picture before push-(void) Pushviewcontroller: (Uiviewcontroller *) Viewcontroller animated: (BOOL) animated{//    The IF (Self.viewControllers.count >= 1) {//Call custom method, using context [self screenshot] is only required if there is a sub-controller inside the navigation controller; }//finished before calling the parent class's Push method [Super Pushviewcontroller:viewcontroller Animated:yes];} Using context and clipping with the specified area, template code-(void) screenshot{//Will be view, that is, the view of the window's root controller (must not contain the status bar, the controller in the default iOS7 contains the status bar) Beyondviewcontrol    Ler *BEYONDVC = (Beyondviewcontroller *) Self.view.window.rootViewController;    The total size of the background picture is cgsize size = beyondVC.view.frame.size;    Open context, after using the parameter, the original (yes 0.0 high quality) uigraphicsbeginimagecontextwithoptions (size, yes, 0.0) is truncated; The rectangular range to crop cgrect rect = CGRectMake (0, -20.8, Size.width,Size.Height + 20); Note: iOS7 after Renderincontext: replaced by drawviewhierarchyinrect:afterscreenupdates: [Beyondvc.view drawviewhierarchyinrect:    Rect Afterscreenupdates:no];    From the context, remove UIImage UIImage *snapshot = Uigraphicsgetimagefromcurrentimagecontext ();        Add the captured image to the image array [_screenshotimgs addobject:snapshot];    Remember, end context (removes the top of the stack based on the current bitmap's graphical context) Uigraphicsendimagecontext (); }//the method of monitoring gestures, as long as there is a gesture will be executed-(void) Pangesturerec: (Uipangesturerecognizer *) pangesturerec{//If the currently displayed controller is already a root controller, do not need to do any switching    Animation, return directly if (Self.topviewcontroller = = Self.viewcontrollers[0]) return;            Determine the stages of the pan gesture Switch (pangesturerec.state) {case Uigesturerecognizerstatebegan://Start drag phase            [Self dragbegin];                    Break            Case uigesturerecognizerstateended://End drag stage [self dragend];                    Break            Default://Is dragging stage [self dragging:pangesturerec];    Break }} #pragma mark Start dragging, add pictures and masks-(void) dragbegin{//focus, add imageview and cloak cover to window each time you start a pan gesture [Self.view.window insertsubview:_sc    Reenshotimgview atindex:0];        [Self.view.window Insertsubview:_coverview Abovesubview:_screenshotimgview]; Also, let Imgview display the last (newest) one in the array _screenshotimgview.image = [_screenshotimgs lastobject];} The default initial scale of the Kdefaultscale 0.6//that will be scaled is the initial transparency (All Black) of the default opacity mask #define Kdefaultalpha 1.0//when the distance is dragged, which takes up 3/4 of the total width of the screen. Let ImageView completely show that the mask completely disappears # define Ktargettranslatescale 0.75#pragma mark is dragging, the essence of the animation effect, zooming and transparency changes-(void) dragging: (    Uipangesturerecognizer *) pan{//Get finger drag displacement cgfloat OffsetX = [Pan translationinview:self.view].x;    Only allow to the right to drag, prohibit left-drag if (OffsetX < 0) OffsetX = 0;        Let the entire view pan//nudge the entire navigation view Self.view.transform = cgaffinetransformmaketranslation (OffsetX, 0); Calculate the current finger drag displacement accounted for the total screen width of the ratio, when this ratio reached 3/4, let ImageView completely display, cover completely disappear double Currenttranslatescalex = offsetx/        Self.view.frame.size.width; Let ImageView zoom, default scale + (current balance ratio/target balance ratio) * (1-default ratio) Double scale = Kdefaultscale + (currenttranslatescalex/ktargettranslatescale) * (1-kdefaultscale);    Have reached the original size, you can, do not have to put a bigger if (Scale > 1) scale = 1;        _screenshotimgview.transform = Cgaffinetransformmakescale (scale, scale); Let the opacity change until it is reduced to 0, so that the mask is completely transparent, the default scale-(current balance ratio/target balance ratio) * Default ratio double alpha = kdefaultalpha-(currenttranslatescalex/    Ktargettranslatescale) * KDEFAULTALPHA; _coverview.alpha = Alpha;} #pragma mark ends the drag, determines the distance at the end of the drag to be processed accordingly, and removes the picture and mask from the parent control-(void) dragend{//Remove the distance cgfloat TranslateX = self.view.transfor    M.TX;        CGFloat width = self.view.frame.size.width; if (TranslateX <= Width * 0.5) {//If the finger moves less than half the screen, move to the left (bounce back) [UIView animatewithduration:0.3 Animation            s:^{//Important ~ ~ Let the right shift of the view bounce back bit, as long as the empty transform can do self.view.transform = cgaffinetransformidentity; Let ImageView size restore the default scale _screenshotimgview.transform = Cgaffinetransformmakescale (Kdefaultscale, Kdefaul Tscale);        Let the opacity of the mask restore the default Alpha 1.0 _coverview.alpha = Kdefaultalpha; } completion:^ (BOOL finished) {//important, after the animation is complete, remember to remove two view each time, and then add it again the next time you start dragging [_screenshotimgview Remov            Efromsuperview];        [_coverview Removefromsuperview];    }]; } else {//If the finger moves more than half the screen, move to the right [UIView animatewithduration:0.3 animations:^{//Let the view move right            To the far right of the screen, after the end, remember to empty the view transform Self.view.transform = cgaffinetransformmaketranslation (width, 0);            Let ImageView indent to 1 _screenshotimgview.transform = Cgaffinetransformmakescale (1, 1);        Make cover Alpha 0, become completely transparent _coverview.alpha = 0; } completion:^ (BOOL finished) {//Important ~ ~ Let the right-shifted view move completely to the far right of the screen, and after the end, remember to clear the transform of the view, or the next time you start drag again, there will be a problem, because view's t            Ransform No zeroing self.view.transform = cgaffinetransformidentity;           Remove two view, next time you start dragging, add back [_screenshotimgview Removefromsuperview]; [_coverview Removefromsuperview];                        Perform a normal pop operation: Remove the stack top controller so that the real previous controller becomes the top controller of the navigation controller [self popviewcontrolleranimated:no];        Important ~ Remember this time, you can remove the last one in the divisor group is useless [_screenshotimgs removelastobject];    }]; }} @end





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.