不依賴第三方框架也能做出抽屜效果,核心就是 UIPanGestureRecognizer 加上 frame 的即時位移。本文提供兩個版本:單側抽屜與雙側抽屜。
@interface ViewController () {
UIView *_leftView;
}
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
_leftView = [[UIView alloc] init];
// 一開始藏在畫面左側之外
_leftView.frame = CGRectMake(-200, 0, 200, self.view.frame.size.height);
_leftView.backgroundColor = [UIColor greenColor];
[self.view addSubview:_leftView];
UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handlePan:)];
[self.view addGestureRecognizer:pan];
}
- (void)handlePan:(UIPanGestureRecognizer *)recognizer {
CGPoint translation = [recognizer translationInView:self.view];
// 加上位移後的 x 座標
CGFloat Xresult = translation.x + _leftView.frame.origin.x;
if (translation.x >= 0) {
// 向右拖:已全部拉出則不再移動
if (_leftView.frame.origin.x >= 0 || Xresult >= 0) {
_leftView.frame = CGRectMake(0, 0, 200, self.view.frame.size.height);
return;
}
} else if (translation.x < 0) {
// 向左拖:已全部收回則不再移動
if (_leftView.frame.origin.x <= -200 || Xresult <= -200) {
_leftView.frame = CGRectMake(-200, 0, 200, self.view.frame.size.height);
return;
}
}
CGRect frame = _leftView.frame;
frame.origin.x += translation.x;
_leftView.frame = frame;
// 關鍵:清空已累積的位移量
[recognizer setTranslation:CGPointZero inView:recognizer.view];
// 放手後以中線為界做吸附
if (recognizer.state == UIGestureRecognizerStateEnded) {
if (_leftView.frame.origin.x > -100) {
[self closeView:NO];
} else {
[self closeView:YES];
}
}
}
- (void)closeView:(BOOL)close {
if (close) {
[self moveView:CGRectMake(-200, 0, 200, self.view.frame.size.height)];
} else {
[self moveView:CGRectMake(0, 0, 200, self.view.frame.size.height)];
}
}
- (void)moveView:(CGRect)frame {
[UIView animateWithDuration:0.3 animations:^{
_leftView.frame = frame;
} completion:^(BOOL finished) {}];
}
改成放在自己的 view 裡實作,讓 view 自己改變自己的 frame,並用兩個屬性分別指定左右抽出的寬度;不需要某一側時把它設為 0 即可。
#import "PathView.h"
#import "UIView+Additions.h"
@implementation PathView
- (instancetype)init {
self = [super init];
if (self) {
[self setupGestureRecognizer];
}
return self;
}
- (void)setupGestureRecognizer {
UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handlePan:)];
[self addGestureRecognizer:pan];
}
- (void)handlePan:(UIPanGestureRecognizer *)recognizer {
CGPoint translation = [recognizer translationInView:self];
CGFloat Xresult = translation.x + self.left;
if (translation.x >= 0) {
// 向右
if (self.left >= _leftWidth || Xresult >= _leftWidth) {
self.frame = CGRectMake(_leftWidth, 0, self.width, self.height);
return;
}
} else if (translation.x < 0) {
// 向左
if (self.left <= -_rightWidth || Xresult <= -_rightWidth) {
self.frame = CGRectMake(-_rightWidth, 0, self.width, self.height);
return;
}
}
self.left += translation.x;
if (recognizer.state == UIGestureRecognizerStateEnded) {
if (self.left > _leftWidth / 2) {
[self openLeft:YES openRight:NO];
} else if (self.left < -(_rightWidth / 2)) {
[self openLeft:NO openRight:YES];
} else {
[self openLeft:NO openRight:NO];
}
}
// 清空位移量
[recognizer setTranslation:CGPointZero inView:recognizer.view];
}
- (void)openLeft:(BOOL)left openRight:(BOOL)right {
if (!left && !right) {
[self moveView:CGRectMake(0, 0, self.width, self.height)];
} else if (!left && right) {
[self moveView:CGRectMake(-_rightWidth, 0, self.width, self.height)];
} else if (left && !right) {
[self moveView:CGRectMake(_leftWidth, 0, self.width, self.height)];
}
}
- (void)moveView:(CGRect)frame {
[UIView animateWithDuration:0.3 animations:^{
self.frame = frame;
} completion:^(BOOL finished) {}];
}