iOS开发 - 手势移除控制器

手势移除控制器

类似于IT之家、QQ、QQ音乐移除控制器

#import "NavigationController.h"

@interface NavigationController ()
/** 存放每一个控制器的全屏截图 */
@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];

    // 拖拽手势
    UIPanGestureRecognizer *recognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(dragging:)];
    [self.view addGestureRecognizer:recognizer];
}

- (void)dragging:(UIPanGestureRecognizer *)recognizer
{
    // 如果只有1个子控制器,停止拖拽
    if (self.viewControllers.count <= 1) return;

    // 在x方向上移动的距离
    CGFloat tx = [recognizer translationInView:self.view].x;
    if (tx < 0) return;

    if (recognizer.state == UIGestureRecognizerStateEnded || recognizer.state == UIGestureRecognizerStateCancelled) {
        // 决定pop还是还原
        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 {
        // 移动view
        self.view.transform = CGAffineTransformMakeTranslation(tx, 0);

        UIWindow *window = [UIApplication sharedApplication].keyWindow;
        // 添加截图到最后面
        self.lastVcView.image = self.images[self.images.count - 2];
        [window insertSubview:self.lastVcView atIndex:0];
        [window insertSubview:self.cover aboveSubview:self.lastVcView];
    }
}

/**
 *  产生截图
 */
- (void)createScreenShot
{
    UIGraphicsBeginImageContextWithOptions(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

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-07-29 20:52:56

iOS开发 - 手势移除控制器的相关文章

iOS开发UI篇—导航控制器属性和基本使用

IOS开发UI篇—导航控制器属性和基本使用 一.导航控制器的一些属性和基本使用 1.把子控制器添加到导航控制器中的四种方法 (1) 1.创建一个导航控制器 UINavigationController *nav=[[UINavigationControlleralloc]init]; 2.设置导航控制器为window的根视图 self.window.rootViewController=nav; 3.添加 YYOneViewController  *one = [[YYOneViewContro

IOS开发UI篇—导航控制器属性和基本使用 - 文顶顶

原文  http://www.cnblogs.com/wendingding/p/3768622.html IOS开发UI篇—导航控制器属性和基本使用 一.导航控制器的一些属性和基本使用 1.把子控制器添加到导航控制器中的四种方法 (1) UINavigationController *nav=[[ UINavigationController alloc ] init ]; 2.设置导航控制器为window的根视图 self . window . rootViewController =nav

iOS开发UI篇—多控制器和导航控制器简单介绍

iOS开发UI篇—多控制器和导航控制器简单介绍 一.多控制器 一个iOS的app很少只由一个控制器组成,除非这个app极其简单.当app中有多个控制器的时候,我们就需要对这些控制器进行管理 有多个view时,可以用一个大的view去管理1个或者多个小view,控制器也是如此,用1个控制器去管理其他多个控制器 比如,用一个控制器A去管理3个控制器B.C.D.控制器A被称为控制器B.C.D的“父控制器”:控制器B.C.D的被称为控制器A的“子控制器” 为了便于管理控制器,iOS提供了2个比较特殊的控

IOS开发——手势 &amp; 传感器 &amp; 物理引擎

这次思维导图比较杂,demo已经全部上传到github上,小编的github地址是:狂戳 先看下效果图: 手势画板: 物理引擎: 传感器: IOS开发--手势 & 传感器 & 物理引擎

【iOS开发-手势】iOS中各种手势总结

iOS中支持的手势事件 UITapGestureRecognizer(敲击) UIPinchGestureRecognizer(捏合,用于缩放) UIPanGestureRecognizer(拖拽) UISwipeGestureRecognizer(轻扫) UIRotationGestureRecognizer(旋转) UILongPressGestureRecognizer(长按) UITapGestureRecognizer(敲击) //创建UITapGestureRecognizer对象,

ios开发手势处理之手势识别二

#import "ViewController.h" @interface ViewController ()<UIGestureRecognizerDelegate> @property (weak, nonatomic) IBOutlet UIImageView *imageV; @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; // Do any additio

IOS开发-手势简单使用及手势不响应处理办法

1.点击 2.长按 3.拖拽 4.轻扫.捏合.旋转 5.使用手势需要注意的地方 1.注意处理轻扫和拖拽的冲突 //那个时间短的话 就让那个先执行 //处理 拖拽和轻扫 两个手势的冲突 //需要轻扫手势执行完毕后 再去执行拖拽 先执行轻扫后 再执行拖动 处理方法: [panges(拖拽对象) requireGestureRecognizerToFail:swiges(轻扫对象)]; 2.程序出现手势不响应处理方法 //    手势如果不响应 //    1.看是否添加到了imageView上 如果

iOS开发之--移除重复的视图

在开发过程中,可能会遇到这种情况,比如我们需要不断调整一个地方的控件,这个时候,会出现重叠的情况,也可能是我们写的方法,但是不管怎样,问题还是要修复的,这里提供一个解决方法,希望能帮到大家! 代码如下: 在合适的地方,添加上: for (UIView *view in (重复视图).subviews) { [view removeFromSuperview]; } 这样的话,每次加载的时候,把原来添加的视图移除掉,重新添加下就可以了!

IOS开发——手势 &amp;amp; 传感器 &amp;amp; 物理引擎

这次思维导图比較杂,demo已经所有上传到github上,小编的github地址是:狂戳 先看下效果图: 手势画板: 物理引擎: 传感器: