抽屉效果思路:
三个View叠加,一个作为左View,一个作为右View,一个主View,在主View上添加拖动手势,修改主View的frame以显示左View和右View,设置分界值和左/右边界值;根据响应者链,将主View回收的tap手势,添加到self.view 上最合适
//
// 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