IOS开发—IOS自定义任意位置右滑POP视图控制器

IOS自定义任意位置右滑POP视图控制器

IOS7.0之后系统提供了原生的从左边缘滑动pop出栈的方法,也可以自定义左边缘pop出栈,将在下一篇介绍,本篇介绍通过添加手势的方法实现IOS当前屏幕任意位置(非指定左边缘)右滑pop视图控制器出栈。代码如下:

//
// LXXPopViewController.m
//  任意点右滑Pop
//
//  Created by Lotheve on 15/6/12.
//  Copyright (c) 2015年Lotheve. All rights reserved.
//

#import "LXXPopViewController.h"
#define KEYWINDOW[UIApplication sharedApplication].keyWindow
 
@interface LXXPopViewController ()
@property (nonatomic,strong) NSMutableArray *snapshotArray;
@property (nonatomic, strong) UIView *backView;
@property (nonatomic, strong) UIImageView *imageView;
@property (nonatomic, strong) UIPanGestureRecognizer *pan;
@property (nonatomic, assign) CGPoint startPoint;
@property (nonatomic, assign) CGPoint endPoint;
@end
 
@implementation LXXPopViewController
 
- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{
    if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
        //数据初始化
        _snapshotArray = [NSMutableArray array];
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
     //添加pop手势
    _pan = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(panAction:)];
    //???: 什么用
/**这句话的作用是:在手势没有失败之前,不接受其他的touch事件**/    [self.view addGestureRecognizer:_pan];
}<span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);"> </span>

- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated{
    //截屏并保存截图
UIGraphicsBeginImageContextWithOptions(CGSizeMake(self.view.frame.size.width, self.view.frame.size.height), NO, 1);
    [self.view drawViewHierarchyInRect:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) afterScreenUpdates:NO];
    UIImage *snapshot = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    [_snapshotArray addObject:snapshot];
    [super pushViewController:viewController animated:animated];
}

- (UIViewController*)popViewControllerAnimated:(BOOL)animated{
    [_snapshotArray removeLastObject];
    return [super popViewControllerAnimated:animated];
}

#pragma mark - privatemethods
- (void)panAction:(UIPanGestureRecognizer *)panGestureRecognizer{
    if (self.viewControllers.count == 1) {
        return;
    }
    if (panGestureRecognizer.state == UIGestureRecognizerStateBegan) {
        NSLog(@"开始滑动");
        self.startPoint = [panGestureRecognizer locationInView:KEYWINDOW];
        if (!_backView) {
            _backView = [[UIView alloc]initWithFrame:[UIScreen mainScreen].bounds];
            _backView.backgroundColor = [UIColor blackColor];
        }
        if (!_imageView) {
            _imageView = [[UIImageView alloc]initWithFrame:[UIScreen mainScreen].bounds];
            _imageView.backgroundColor = [UIColor clearColor];
            _imageView.image = [_snapshotArray lastObject];
        }
        [_backView addSubview:_imageView];
        [self.view.superview insertSubview:_backView belowSubview:self.view];
       
    }else if (panGestureRecognizer.state == UIGestureRecognizerStateEnded){
        NSLog(@"结束滑动");
        self.endPoint = [panGestureRecognizer locationInView:KEYWINDOW];
        [self judgeWhetherToPop];
    }else{
        CGPoint currentPoint =[panGestureRecognizer locationInView:KEYWINDOW];
        CGFloat moveX = currentPoint.x - self.startPoint.x;
        [self moveViewMaskWithX:moveX];
    }
}

//移动视图
- (void)moveViewMaskWithX:(CGFloat)moveX{
    if (moveX >= 0 && moveX <= [UIScreen mainScreen].bounds.size.width) {
        CGRect frame = self.view.frame;
        frame.origin.x = moveX;
        self.view.frame = frame;
        //透明度渐变
        float alpha = (moveX/[UIScreen mainScreen].bounds.size.width)*2.0/3+1.0/3;
        _imageView.alpha = alpha;
        //缩放
        float scale = (moveX/3200)+0.9;
        _imageView.transform = CGAffineTransformMakeScale(scale, scale);
    }
}
//判断并执行是否pop
- (void)judgeWhetherToPop{
    if (self.endPoint.x - self.startPoint.x > 50) {
        [UIView animateWithDuration:0.3 animations:^{
            [self moveViewMaskWithX:[UIScreen mainScreen].bounds.size.width];
        } completion:^(BOOL finished) {
            [self popViewControllerAnimated:NO];
            [_backView removeFromSuperview];
            _backView = nil;
            _imageView = nil;
            CGRect frame = self.view.frame;
            frame.origin.x = 0;
            self.view.frame = frame;
        }];
    }else{
        [UIView animateWithDuration:0.3 animations:^{
            [self moveViewMaskWithX:0];
        } completion:^(BOOL finished) {
            [_backView removeFromSuperview];
            _backView = nil;
            _imageView = nil;
        }];
    }
}
@end

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值