本文提供兩個可直接取用的抽屜實作:第一個是單邊抽屜的極簡版本,第二個則把邏輯收進自訂 view 裡,支援左右雙邊且寬度可配置。
@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) {
}];
}
@end
這個版本刻意寫得直白,方便理解流程。三個要點:setTranslation:CGPointZero 必須呼叫,否則位移會重複累加;邊界判斷讓抽屜不會被拖出可視範圍;以 -100(一半寬度)作為吸附門檻,放手後自動靠向最近的狀態。
第二版把整套邏輯搬進自訂的 PathView 裡,讓 view 自己改變自己的 frame,並用兩個屬性分別指定左右抽屜的展開寬度。
#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) {
}];
}
@end
與第一版的差異有兩點:手勢綁在 view 自己身上,對外只要擺上去就能用;另外多了 _leftWidth 與 _rightWidth 兩個屬性,分別控制兩側抽屜的展開寬度,某一邊不需要時設為 0 即可停用。
兩版共同的注意事項:UIVIEWAnimationOptionCurveEaseOut 之類的緩動選項可以讓吸附動畫更有彈性;若抽屜內容需要跟著主視圖一起縮放(做出景深感),就得在動畫 block 裡同步調整 transform,而不是只改 frame。另外記得處理 touchesCancelled 對應的手勢取消狀態,避免抽屜停在半路。