iOS 抽屉效果

抽屉效果思路:

三个View叠加,一个作为左View,一个作为右View,一个主View,在主View上添加拖动手势,修改主View的frame以显示左View和右View,设置分界值和左/右边界值;根据响应者链,将主View回收的tap手势,添加到self.view 上最合适

渐变色动画.gif
//
//  ViewController.m
//  抽屉效果
//
//  Created by Captain on 2017/7/29.
//  Copyright © 2017年 CaptainSir. All rights reserved.
//

#import "ViewController.h"

#define screenW  [UIScreen mainScreen].bounds.size.width
#define screenH  [UIScreen mainScreen].bounds.size.height

@interface ViewController ()

@property (nonatomic, weak) UIView * leftView;
@property (nonatomic, weak) UIView * rightView;
@property (nonatomic, weak) UIView * mainView;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    [self addViews];
    [self setupViews];
}

- (void)addViews{
    // 左侧View
    UIView * leftV = [[UIView alloc]init];
    leftV.backgroundColor = [UIColor greenColor];
    [self.view addSubview:leftV];
    
    // 右侧view
    UIView * rightV = [[UIView alloc]init];
    rightV.backgroundColor = [UIColor yellowColor];
    [self.view addSubview:rightV];
    
    // 主View
    UIView * mainV = [[UIView alloc]init];
    mainV.backgroundColor = [UIColor redColor];
    [self.view addSubview:mainV];
    
    leftV.frame = rightV.frame = mainV.frame = self.view.frame;
    self.leftView = leftV;
    self.rightView  = rightV;
    self.mainView = mainV;
}

- (void)setupViews{
    // 主View拖动手势
    UIPanGestureRecognizer * panGes = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(mainViewPanAction:)];
    [self.mainView addGestureRecognizer:panGes];
    
    // 点击self.view tap手势回收mainView
    UITapGestureRecognizer * tapGes = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(mainViewRecoveryTapAction)];
    [self.view addGestureRecognizer:tapGes];
}

#define BoundaryValue      [UIScreen mainScreen].bounds.size.width * 0.5        // 位置分界值
#define TargetRight        [UIScreen mainScreen].bounds.size.width - 50        // 右侧最远距离
#define TargetLeft         50                                                   // 左侧最远距离

- (void)mainViewPanAction:(UIPanGestureRecognizer *) panGes{
    // 获取手势在self.mainView上的偏移量
    CGPoint point = [panGes translationInView:self.mainView];
    // 修改self.mainView的frame
    [self changeMainViewFrameWithOffsetX:point.x];
    // 复原手势在self.mainView上的偏移量,防止叠加偏移
    [panGes setTranslation:CGPointZero inView:self.mainView];
    
    if (panGes.state == UIGestureRecognizerStateEnded) {
        // 设置边界距离
        CGFloat margin = 0;
        if (self.mainView.frame.origin.x > BoundaryValue) {
            margin = TargetRight - self.mainView.frame.origin.x;
            
        }else if (CGRectGetMaxX(self.mainView.frame) < BoundaryValue) {
            margin = TargetLeft - CGRectGetMaxX(self.mainView.frame);
        }else {
            margin = -self.mainView.frame.origin.x;
        }
         [self changeMainViewFrameWithOffsetX: margin];
    }
}

#define MarginY     80
// 根据偏移量计算self.mainView的frame
- (void)changeMainViewFrameWithOffsetX:(CGFloat)offsetX{
    
    CGRect frame = self.mainView.frame;
    // X 值
    frame.origin = CGPointMake(frame.origin.x + offsetX, 0);
    
    // Y 值
    frame.origin.y = fabs((frame.origin.x / screenW) * MarginY);
    
    // H 值
    frame.size.height = screenH - (2 * frame.origin.y);
    

    self.mainView.frame = frame;
    
    if (self.mainView.frame.origin.x > 0) {
        self.rightView.hidden = YES;
    }else {
        self.rightView.hidden = NO;
    }
}

// 回收mainView的frame
- (void)mainViewRecoveryTapAction{
    self.mainView.frame = self.view.bounds;
}

@end
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 1.添加需要实现抽屉效果的三个视图,这里需要注意主视图需要放在最后添加 2.实现左滑显示左边视图,右滑出现右边视图...
    azhang_coder阅读 1,088评论 0 3
  • 抽屉效果 MMDrawerController 使用说明 这是一个抽屉导航控制器, 越来越多的应用开始使用抽屉效果...
    iOS_愛OS阅读 2,185评论 8 13
  • 效果图 平时开发中经常会用到抽屉效果,关于抽屉的实现有许多三方库,读者可以根据需要选用,本节内容主要简单的实现一个...
    Hardy_Hu阅读 5,060评论 0 5
  • 我们在用QQ时都会发现,消息列表向左滑动时,左侧的功能界面被显示出来,消息列表会拉到最右侧, 就像一个抽屉拉出来一...
    f94bd4cac294阅读 2,515评论 1 20
  • 一个简单的抽屉效果,简单实用!整体思路:主要是在控制器的View上添加了两个View,一个左侧leftView和一...
    大冰哒哒呤阅读 824评论 0 1