抽屜效果思路:
三個(gè)View疊加,一個(gè)作為左View,一個(gè)作為右View,一個(gè)主View,在主View上添加拖動(dòng)手勢(shì),修改主View的frame以顯示左View和右View,設(shè)置分界值和左/右邊界值;根據(jù)響應(yīng)者鏈,將主View回收的tap手勢(shì),添加到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{
// 左側(cè)View
UIView * leftV = [[UIView alloc]init];
leftV.backgroundColor = [UIColor greenColor];
[self.view addSubview:leftV];
// 右側(cè)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拖動(dòng)手勢(shì)
UIPanGestureRecognizer * panGes = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(mainViewPanAction:)];
[self.mainView addGestureRecognizer:panGes];
// 點(diǎn)擊self.view tap手勢(shì)回收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 // 右側(cè)最遠(yuǎn)距離
#define TargetLeft 50 // 左側(cè)最遠(yuǎn)距離
- (void)mainViewPanAction:(UIPanGestureRecognizer *) panGes{
// 獲取手勢(shì)在self.mainView上的偏移量
CGPoint point = [panGes translationInView:self.mainView];
// 修改self.mainView的frame
[self changeMainViewFrameWithOffsetX:point.x];
// 復(fù)原手勢(shì)在self.mainView上的偏移量,防止疊加偏移
[panGes setTranslation:CGPointZero inView:self.mainView];
if (panGes.state == UIGestureRecognizerStateEnded) {
// 設(shè)置邊界距離
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
// 根據(jù)偏移量計(jì)算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