標籤:ios開發 右滑手勢出棧 自訂pop出棧 任意位置右滑出棧 uinavagationcontroll
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
IOS開發—IOS自訂任意位置右滑POP視圖控制器