iOS開發,ios開發教程

來源:互聯網
上載者:User

iOS開發,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

著作權聲明:本文為博主原創文章,未經博主允許不得轉載。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.