全屏滑动返回

原创 2015年11月19日 11:07:18

自定义导航

#import <UIKit/UIKit.h>


@interface CJNavigationController : UINavigationController

//是否可以滑动返回

@property (nonatomic,assign) BOOL isCanDragBack;


@end


#import "CJNavigationController.h"



#define CJScreenHeight [UIScreen mainScreen].bounds.size.height

#define CJScreenWidth [UIScreen mainScreen].bounds.size.width


#define CJOffsetFloat  0.65// 拉伸参数

#define CJOffetDistance 100// 最小回弹距离


@interface CJNavigationController ()


@property(nonatomic,assign) CGPoint mStartPoint;


@property(nonatomic,strong) UIImageView *mLastScreenShotView;


@property (nonatomic, strong) UIView *mBgView;


@property (nonatomic, strong) NSMutableArray *mScreenShots;


@property (nonatomic, assign) BOOL mIsMoving;



@end


@implementation CJNavigationController


- (instancetype)init

{

    self = [super init];

    if (self) {

        self.isCanDragBack = YES;

    }

    return self;

}


-(void)viewDidLoad{

    [super viewDidLoad];

    self.view.layer.shadowOffset = CGSizeMake(0, 10);

    self.view.layer.shadowOpacity = 0.8;

    self.view.layer.shadowRadius = 10;

    self.view.layer.shadowColor = [UIColor blackColor].CGColor;

    UIPanGestureRecognizer *recognizer = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(didHandlePanGesture:)];

    [recognizer delaysTouchesBegan];

    [self.view addGestureRecognizer:recognizer];

}



-(NSMutableArray *)mScreenShots{

    if (!_mScreenShots) {

        _mScreenShots = [NSMutableArray new];

    }

    return _mScreenShots;

}

//初始化截屏的view

-(void)initViews{

    if (!self.mBgView) {

        self.mBgView = [[UIView alloc]initWithFrame:self.view.bounds];

        self.mBgView.backgroundColor = [UIColor blackColor];

        [self.view.superview insertSubview:self.mBgView belowSubview:self.view];

    }

    self.mBgView.hidden = NO;

    if (self.mLastScreenShotView) [self.mLastScreenShotView removeFromSuperview];

    UIImage *lastScreenShot = [self.mScreenShots lastObject];

    self.mLastScreenShotView = [[UIImageView alloc] initWithImage:lastScreenShot];

    self.mLastScreenShotView.frame = (CGRect){-(CJScreenWidth*CJOffsetFloat),0,CJScreenWidth,CJScreenHeight};

    [self.mBgView addSubview:self.mLastScreenShotView];

}

//改变状态栏颜色

-(UIStatusBarStyle)preferredStatusBarStyle{

    UIViewController* topVC = self.topViewController;

    return [topVC preferredStatusBarStyle];

}


- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated

{

    if (self.viewControllers.count > 0) {

        [self.mScreenShots addObject:[self capture]];

        NSLog(@"%@",self.mScreenShots);

        //        [self pushAnimation:viewController];

        //        return;

    }

    [super pushViewController:viewController animated:animated];

    

}

-(void)pushAnimation:(UIViewController *)viewController{

    CATransition *animation = [CATransition animation];

    [animation setDuration:0.2];

    [animation setType: kCATransitionMoveIn];

    [animation setSubtype: kCATransitionFromRight];

    [animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionDefault]];

    [super pushViewController:viewController animated:NO];

    [self.view.layer addAnimation:animation forKey:nil];

}


- (UIViewController *)popViewControllerAnimated:(BOOL)animated {

    if (animated) {

        [self popAnimation];

        return nil;

    } else {

        return [super popViewControllerAnimated:animated];

    }

}

- (void) popAnimation {

    if (self.viewControllers.count == 1) {

        return;

    }

    [self initViews];

    [UIView animateWithDuration:0.4 animations:^{

        [self doMoveViewWithX:CJScreenWidth];

    } completion:^(BOOL finished) {

        [self completionPanBackAnimation];

    }];

}



- (UIImage *)capture

{

    UIGraphicsBeginImageContextWithOptions(self.view.bounds.size, self.view.opaque, 0.0);

    [self.view.layer renderInContext:UIGraphicsGetCurrentContext()];

    UIImage * img = UIGraphicsGetImageFromCurrentImageContext();

    UIGraphicsEndImageContext();

    return img;

}

#pragma mark ------------  UIPanGestureRecognizer -------


-(void)didHandlePanGesture:(UIPanGestureRecognizer *)recoginzer{

    if (self.viewControllers.count <= 1 && !self.isCanDragBack) return;

    CGPoint touchPoint = [recoginzer locationInView:[[UIApplication sharedApplication]keyWindow]];

    

    CGFloat offsetX = touchPoint.x - self.mStartPoint.x;

    if(recoginzer.state == UIGestureRecognizerStateBegan)

    {

        [self initViews];

        _mIsMoving = YES;

        _mStartPoint = touchPoint;

        offsetX = 0;

    }

    

    if(recoginzer.state == UIGestureRecognizerStateEnded)

    {

        if (offsetX > CJOffetDistance)

        {

            [UIView animateWithDuration:0.3 animations:^{

                [self doMoveViewWithX:CJScreenWidth];

            } completion:^(BOOL finished) {

                [self completionPanBackAnimation];

                

                self.mIsMoving = NO;

            }];

        }else{

            [UIView animateWithDuration:0.3 animations:^{

                [self doMoveViewWithX:0];

            } completion:^(BOOL finished) {

                self.mIsMoving = NO;

                self.mBgView.hidden = YES;

            }];

        }

        self.mIsMoving = NO;

    }

    if(recoginzer.state == UIGestureRecognizerStateCancelled)

    {

        [UIView animateWithDuration:0.3 animations:^{

            [self doMoveViewWithX:0];

        } completion:^(BOOL finished) {

            self.mIsMoving = NO;

            self.mBgView.hidden = YES;

        }];

        self.mIsMoving = NO;

    }

    if (self.mIsMoving) {

        [self doMoveViewWithX:offsetX];

    }

}

-(void)doMoveViewWithX:(CGFloat)x{

    x = x>CJScreenWidth?CJScreenWidth:x;

    x = x<0?0:x;

    CGRect frame = self.view.frame;

    frame.origin.x = x;

    self.view.frame = frame;

    self.mLastScreenShotView.frame = (CGRect){-(CJScreenWidth*CJOffsetFloat)+x*CJOffsetFloat,0,CJScreenWidth,CJScreenHeight};

}

-(void)completionPanBackAnimation{

    [self.mScreenShots removeLastObject];

    [super popViewControllerAnimated:NO];

    CGRect frame = self.view.frame;

    frame.origin.x = 0;

    self.view.frame = frame;

    self.mBgView.hidden = YES;

}



@end



导航控制器全屏滑动返回

  • 2016年04月18日 10:46
  • 35KB
  • 下载

iOS 3行代码搞定导航控制器全屏滑动返回效果

本文方法是从大神的方法中获得的启示。 在导航控制器里的最后一个视图控制器的viewDidLoad:中添加如下代码: id target = self.navigationController.in...

iOS一个丝滑的全屏滑动返回手势

原文  http://blog.sunnyxx.com/2015/06/07/fullscreen-pop-gesture/ 全屏返回手势  自 iOS7 之后,Apple 增...

IOS开发 导航控制器全屏滑动返回效果

原文地址:http://www.cocoachina.com/ios/20150811/12897.html 分析: 1.导航控制器的view自带了滑动手势,只不过手势的触发范围只能在...

一个丝滑的全屏滑动返回手势

demo 地址:https://github.com/forkingdog/FDFullscreenPopGesture 全屏返回手势 自 iOS7 之后,Apple 增加了屏幕边缘右划返...

一个丝滑的全屏滑动返回手势

全屏返回手势自 iOS7 之后,Apple 增加了屏幕边缘右划返回交互的支持,再配合上 UINavigationController 的交互式动画,pop 到上一级页面的操作变的非常顺畅和丝滑,从此,...

ios 8行代码教你搞定导航控制器全屏滑动返回效果

一、自定义导航控制器 目的:以后需要使用全屏滑动返回功能,就使用自己定义的导航控制器。 二、分析导航控制器侧滑功能 效果:导航控制器默认自带了侧滑功能,当用户在界面的左边滑动的时候,就会有侧滑功...

一个丝滑的全屏滑动返回手势

全屏返回手势自 iOS7 之后,Apple 增加了屏幕边缘右划返回交互的支持,再配合上 UINavigationController 的交互式动画,pop 到上一级页面的操作变的非常顺畅和丝滑,从此,...

8行代码教你搞定导航控制器全屏滑动返回效果

此次文章,讲述的是导航控制器全屏滑动返回效果,而且代码量非常少,10行内搞定。 效果如图: 如果喜欢我的文章,可以关注我,也可以来小码哥,了解下我们的iOS培训课程。陆续还会有更新ing......

全屏滑动返回手势

查看原文 全屏返回手势 自 iOS7 之后,Apple 增加了屏幕边缘右划返回交互的支持,再配合上 UINavigationController 的交互式动画,pop 到上一级...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:全屏滑动返回
举报原因:
原因补充:

(最多只允许输入30个字)