Ios-like Sina Weibo UINavigationController displays the View of the previous controller when sliding to the left.

Source: Internet
Author: User

Ios-like Sina Weibo UINavigationController displays the View of the previous controller when sliding to the left.

The View of the previous controller is displayed when the Sina Weibo UINavigationController slides left.


Implementation principle: when the self. view of UINavigationController is displayed, the current view is saved to an array. When a view is pushed, the previous view is placed behind self. view. When self. view is dragged to the right, the previous view is displayed.

NavigationController. m

# Import "NavigationController. h "@ interface NavigationController ()/** store the full screen of each controller */@ property (nonatomic, strong) 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 (dragging :)]; [self. view addGestureRecognizer: recognizer];}-(void) dragging :( UIPanGestureRecognizer *) recognizer {// if there is only one sub-controller, stop dragging if (self. viewControllers. count <= 1) return; // The distance from CGFloat tx = [recognizer translationInView: self. view]. x; if (tx <0) return; if (recognizer. state = UIGestureRecognizerStateEnded | recognizer. state = UIGestureRecognizerStateCancelled) {// determine whether to restore the pop or CGFloat x = self. view. frame. origin. x; if (x> = self. view. frame. size. width * 0.5) {[UIView animateWithDuration: 0.25 animations: ^ {self. 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 = CGAffineTransformIdentity;}] ;}} else {// move view self. view. transform = CGAffineTransformMakeTranslation (tx, 0); UIWindow * window = [UIApplication sharedApplication]. keyWindow; // Add it to the end of self. lastVcView. image = self. images [self. images. count-2]; [window insertSubview: self. lastVcView atIndex: 0]; [window insertSubview: self. cover aboveSubview: self. lastVcView] ;}}/*** generate */-(void) createScreenShot {uigraphicsbeginimagecontextwitexceptions (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


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.