iOS imitation Sina Weibo uinavigationcontroller shows the view of the previous controller when it slides to the left.

Source: Internet
Author: User

Imitation Sina Weibo Uinavigationcontroller shows the view of the previous controller when it slides to the left.


The implementation principle, Uinavigationcontroller the Self.view display when the current display of the view down, save to an array. When you push a view, the previous view is placed behind Self.view, and the previous view is displayed when Self.view is dragged to the right.

Navigationcontroller.m

#import "NavigationController.h" @interface Navigationcontroller ()/** Store the full screen/@property (nonatomic, strong) for each controller Nsmutablearray *images; @property (nonatomic, strong) Uiimageview *lastvcview; @property (nonatomic, strong) UIView * Cover, @end @implementation navigationcontroller-(Uiimageview *) lastvcview{if (!_lastvcview) {UIWindow *window        = [UIApplication Sharedapplication].keywindow;        Uiimageview *lastvcview = [[Uiimageview alloc] init];        Lastvcview.frame = Window.bounds;    Self.lastvcview = Lastvcview; } return _lastvcview;}        -(UIView *) cover{if (!_cover) {UIWindow *window = [UIApplication Sharedapplication].keywindow;        UIView *cover = [[UIView alloc] init];        Cover.backgroundcolor = [Uicolor blackcolor];        Cover.frame = Window.bounds;        Cover.alpha = 0.5;    Self.cover = cover; } return _cover;}    -(Nsmutablearray *) images{if (!_images) {self.images = [[Nsmutablearray alloc] init]; } return _iMages;}        -(void) viewdidload {[Super viewdidload]; Drag gesture Uipangesturerecognizer *recognizer = [[Uipangesturerecognizer alloc] initwithtarget:self action: @selector (drag    Ging:)]; [Self.view Addgesturerecognizer:recognizer];} -(void) dragging: (Uipangesturerecognizer *) recognizer{//If only 1 sub-controllers, stop dragging if (self.viewControllers.count <= 1) re        Turn    The distance moved in the x direction cgfloat tx = [recognizer translationinview:self.view].x;        if (TX < 0) return; if (recognizer.state = = Uigesturerecognizerstateended | | recognizer.state = = uigesturerecognizerstatecancelled) {/        /decide whether to pop or restore cgfloat x = self.view.frame.origin.x; if (x >= self.view.frame.size.width * 0.5) {[UIView animatewithduration:0.25 animations:^{S            Elf.view.transform = cgaffinetransformmaketranslation (self.view.frame.size.width, 0);                } completion:^ (BOOL finished) {[Self popviewcontrolleranimated:no]; [Self.lastvcview Removefromsuperview];                [Self.cover Removefromsuperview];                Self.view.transform = cgaffinetransformidentity;            [Self.images Removelastobject];        }]; } else {[UIView animatewithduration:0.25 animations:^{self.view.transform = Cgaffinetransformi            Dentity;        }];                }} else {//mobile View Self.view.transform = cgaffinetransformmaketranslation (TX, 0);        UIWindow *window = [UIApplication Sharedapplication].keywindow;        Add to last face self.lastVcView.image = self.images[self.images.count-2];        [Window InsertSubview:self.lastVcView atindex:0];    [Window InsertSubview:self.cover AboveSubview:self.lastVcView];    }}/** * generates */-(void) createscreenshot{uigraphicsbeginimagecontextwithoptions (self.view.frame.size, YES, 0.0);    [Self.view.layer Renderincontext:uigraphicsgetcurrentcontext ()]; UIImage *image = Uigraphicsgetimagefromcurrentimagecontext ();    [Self.images addobject:image];}        -(void) Viewdidappear: (BOOL) animated{[Super viewdidappear:animated];        if (Self.images.count > 0) return; [Self createscreenshot];} -(void) Pushviewcontroller: (Uiviewcontroller *) Viewcontroller animated: (BOOL) animated{[Super Pushviewcontroller:        Viewcontroller animated:animated]; [Self createscreenshot];} @end


iOS imitation Sina Weibo uinavigationcontroller shows the view of the previous controller when it slides to the left.

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.