码迷,mamicode.com
首页 > 移动开发 > 详细

IOS 自定义 滑动返回 手势

时间:2014-06-11 08:28:52      阅读:331      评论:0      收藏:0      [点我收藏+]

标签:class   tar   ext   color   com   get   

/**

  只需要在你自定义的导航控制器中,改成如下代码即可,自定义手势返回

*/

#define KEY_WINDOW [[UIApplication sharedApplication] keyWindow]

#define kScreenW KEY_WINDOW.bounds.size.width

#define kAnimateDuration 0.3

 

#define kDefaultAlapa 0.5

#define kDefaultScale 0.95

 

#define iOS7 ([UIDevice currentDevice].systemVersion.floatValue >= 7.0)

 

#import "CXNavigationController.h"

#import <QuartzCore/QuartzCore.h>

 

@interfaceCXNavigationController ()

{

    NSMutableArray *_images; //图片数组

    UIView *_bgView; //背景图

    UIView *_blankMask; //面罩

    UIImageView *_imageView; //图像

    CGPoint _startPoint; //开始接触点

}

@end

 

@implementation CXNavigationController

 

- (void)viewDidLoad{

    [superviewDidLoad];

    _images = [NSMutableArrayarray];

    if (!iOS7) {

        UIPanGestureRecognizer *pan = [[UIPanGestureRecognizeralloc] initWithTarget:selfaction:@selector(panMove:)];

        [self.viewaddGestureRecognizer:pan];

    }

}

 

- (void)panMove:(UIPanGestureRecognizer *)pan{

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

        return;

    }

    CGRect selfF = self.view.frame;

    CGPoint location = [pan locationInView:KEY_WINDOW];

    if (pan.state == UIGestureRecognizerStateBegan) { //开始拖拽

        _startPoint = location;

        _imageView.image = [_imageslastObject];

        [KEY_WINDOWinsertSubview:_bgViewbelowSubview:self.tabBarController.view];

        _imageView.transform = CGAffineTransformMakeScale(kDefaultScale, kDefaultScale);

    }elseif(pan.state == UIGestureRecognizerStateChanged){ //拖拽中

        

        CGFloat marginX = location.x - _startPoint.x;

        marginX = marginX > kScreenW ? kScreenW : marginX;

        marginX = marginX < 0 ? 0 : marginX;

        selfF.origin.x = marginX;

        self.view.frame = selfF;

        

        CGFloat scale = kDefaultScale + marginX * 1.0 / 6400;

        scale = marginX == kScreenW ? 0.5 : scale;

        scale = marginX == 0 ? 0 : scale;

        _imageView.transform = CGAffineTransformMakeScale(scale, scale);

        

        CGFloat alpha = kDefaultAlapa - (1 - kDefaultAlapa) * marginX * 1.0 / kScreenW;

        alpha = marginX == kScreenW ? kDefaultAlapa : alpha;

        alpha = marginX == 0 ? 0 : alpha;

        _blankMask.alpha = alpha;

        

    }elseif(pan.state == UIGestureRecognizerStateEnded){ //结束

        CGFloat marginX = location.x - _startPoint.x;

        if (marginX > 50) {

            marginX = marginX > kScreenW ? kScreenW : marginX;

            CGFloat dur = kAnimateDuration - marginX * 1.0 / 2000;

            [UIViewanimateWithDuration:dur animations:^{

                CGRect selfF = self.view.frame;

                selfF.origin.x = KEY_WINDOW.bounds.size.width;

                self.view.frame = selfF;

                _blankMask.alpha = 0;

                _imageView.transform = CGAffineTransformMakeScale(1, 1);

            } completion:^(BOOL finished) {

                [_images removeLastObject];

                [_bgView removeFromSuperview];

                CGRect rect = self.view.frame;

                rect.origin.x = 0;

                self.view.frame = rect;

                [superpopViewControllerAnimated:NO];

            }];

        }else{

            CGFloat marginX = location.x - _startPoint.x;

            marginX = marginX > 50 ? 50 : marginX;

            marginX = marginX < 0 ? 0 : marginX;

            CGFloat dur = marginX * 1.0 / 500;

            [UIViewanimateWithDuration:0.1 + dur  animations:^{

                CGRect selfF = self.view.frame;

                selfF.origin.x = 0;

                self.view.frame = selfF;

            }];

        }

    }

}

 

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

    if (self.viewControllers.count > 0) {

        if (!iOS7) {

            [self createBg];

        }

    }

    [super pushViewController:viewController animated:animated];

}

 

#pragma mark 创建背景

- (void)createBg{

    [_imagesaddObject:[selfcurrentScreenImage]];

    if (!_bgView) {

        _bgView = [[UIView alloc] initWithFrame:KEY_WINDOW.bounds];

        _bgView.backgroundColor = [UIColorblackColor];

        

        _imageView = [[UIImageView alloc] initWithFrame:KEY_WINDOW.bounds];

        _imageView.backgroundColor = [UIColorclearColor];

        [_bgViewaddSubview:_imageView];

        

        _blankMask = [[UIView alloc] initWithFrame:KEY_WINDOW.bounds];

        _blankMask.backgroundColor = [UIColorblackColor];

        _blankMask.alpha = kDefaultAlapa;

        [_bgViewaddSubview:_blankMask];

    }

}

 

#pragma mark 获取当期屏幕

- (UIImage *)currentScreenImage{

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

    if (self.topViewController.tabBarController) {

        [self.topViewController.tabBarController.view.layerrenderInContext:UIGraphicsGetCurrentContext()];

    }else{

        [self.view.layerrenderInContext:UIGraphicsGetCurrentContext()];

    }

    UIImage * img = UIGraphicsGetImageFromCurrentImageContext();

    UIGraphicsEndImageContext();

    return img;

}

IOS 自定义 滑动返回 手势,布布扣,bubuko.com

IOS 自定义 滑动返回 手势

标签:class   tar   ext   color   com   get   

原文地址:http://www.cnblogs.com/d2688/p/3773145.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!