抽屜式選單是行動應用裡常見的互動模式,本文將透過一個完整範例,帶你一步步實作 iOS 上可左右拖曳的側邊抽屜效果,並附上可直接運行的程式碼。
整個實作可以拆解成三個關鍵環節:首先利用觸控事件 touchesMoved: 來偵測手指的移動,藉此觸發左側與右側視圖的進出;接著在 loadView 方法中提前建立好左、中、右三個 View,並在畫面載入前就設定好各自的 frame;最後透過 KVO 監聽主視圖 frame 的變化,即時切換左右兩側的顯示與隱藏。每個方法各司其職,邏輯清晰。
#import "DarwViewController.h"
@interface DarwViewController ()
@property (nonatomic, weak) UIView *leftView;
@property (nonatomic, weak) UIView *rightView;
@property (nonatomic, weak) UIView *mainView;
/**
* 動畫是否進行
*/
@property (nonatomic ,assign) BOOL animating;
@end
@implementation DarwViewController
- (void)viewDidLoad {
[super viewDidLoad];
}
-(void)loadView {
self.view = [[UIView alloc]initWithFrame:[UIScreen mainScreen].bounds];
//左邊view
UIView *leftView = [[UIView alloc]initWithFrame:self.view.frame];
[self.view addSubview:leftView];
leftView.backgroundColor= [UIColor redColor];
self.leftView = leftView;
//右邊View
UIView *rightView = [[UIView alloc]initWithFrame:self.view.frame];
[self.view addSubview:rightView];
rightView.backgroundColor= [UIColor blueColor];
self.rightView = rightView;
//主頁面
UIView *mainView = [[UIView alloc]initWithFrame:self.view.frame];
[self.view addSubview:mainView];
mainView.backgroundColor= [UIColor yellowColor];
self.mainView = mainView;
//KVO監聽
[self.mainView addObserver:self forKeyPath:@"frame" options:NSKeyValueObservingOptionNew context:nil];
}
/**
* KVO回調方法 當mainView Frame值改變時觸發
*/
-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSString *,id> *)change context:(void *)context {
if (self.animating) return; //如果mainView正在動畫 就不執行
if (self.mainView.frame.origin.x > 0 ) { //X > 0 就隱藏右邊View 顯示左邊View
self.rightView.hidden = YES;
self.leftView.hidden = NO;
} else if (self.mainView.frame.origin.x < 0) { //X < 0 就隱藏左邊View 顯示右邊VIew
self.leftView.hidden = YES;
self.rightView.hidden = NO;
}
}
#pragma mark -- 觸摸事件
-(void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
//獲得觸摸對象
UITouch *touch = [touches anyObject];
//獲得當前觸摸點
CGPoint currentPoint = [touch locationInView:self.view];
//獲得上一個觸摸點
CGPoint previousPoint = [touch previousLocationInView:self.view];
//計算x方向的偏移量
CGFloat offsetX = currentPoint.x - previousPoint.x;
// 根據x的偏移量計算y的偏移量
self.mainView.frame = [self rectWithOffsetX:offsetX];
}
#define screenW [UIScreen mainScreen].bounds.size.width
#define screenH [UIScreen mainScreen].bounds.size.height
/**
* 計算主視圖的frame
*/
- (CGRect ) rectWithOffsetX:(CGFloat )offsetX {
//Y軸的偏移量
CGFloat offsetY = (screenH *1/5) * (offsetX/screenW);
//比例 :(用於寬高的縮放)
CGFloat scale = (screenH - offsetY *2) / screenH;
if (self.mainView.frame.origin.x < 0 ) { //如果x是負數 及左邊View要顯示
//比例就要設為比1小
scale = 2 - scale;
}
//獲取當前mainView的frame
CGRect frame = self.mainView.frame;
//重新設置mainView的frame值
frame.size.width = frame.size.width *scale >screenW ? screenW : frame.size.width *scale;
frame.size.height = frame.size.height *scale >screenH ? screenH :frame.size.height *scale;
frame.origin.x += offsetX;
frame.origin.y =(screenH - frame.size.height)*0.5;
//返回偏移後新的frame
return frame;
}
#define maxRightX (screenW *0.8)
#define maxLeftX (-screenW *0.6)
-(void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
CGFloat targetX = 0;
//如果松手的那一下 當前mainVIew的x大於屏幕的一半
if (self.mainView.frame.origin.x > screenW * 0.5) {
//向右邊定位
targetX = maxRightX;
}
//如果松手的那一下 當前mainVIew的最大X值小於屏幕的一半
else if (CGRectGetMaxX(self.mainView.frame) < screenW *0.5) {
//向左邊定位
targetX = maxLeftX;
}
//計算偏移量
CGFloat offsetX = targetX -self.mainView.frame.origin.x;
self.animating = YES;
[UIView animateWithDuration:0.4 animations:^{
if (targetX == 0) { //如果targetX==0 復位
self.mainView.frame = self.view.frame;
} else { //如果targetX != 0 那就到指定位置
self.mainView.frame = [self rectWithOffsetX:offsetX];
}
} completion:^(BOOL finished) {
self.animating = NO;
}];
}
@end
透過觸控位移即時更新主視圖 frame,並搭配 KVO 自動切換左右背景,就能獲得流暢的抽屜體驗。你可以依需求替換背景色為實際的內容視圖,並微調 maxRightX、maxLeftX 等參數來控制抽屜拉出的幅度。