iOS實(shí)現(xiàn)簡(jiǎn)單抽屜效果
抽屜效果
所謂抽屜效果就是三個(gè)視圖,向右拖拽顯示左邊的視圖,向左拖拽顯示右邊的視圖,當(dāng)拖拽大于屏幕的一半時(shí)最上面的視圖會(huì)自動(dòng)定位到一邊,當(dāng)點(diǎn)擊左邊或右邊視圖時(shí)會(huì)最上面視圖會(huì)自動(dòng)復(fù)位。
效果如圖:三個(gè)視圖(左邊:淺灰色視圖、右邊:品紅視圖、主視圖:黑色視圖)

封裝代碼
DrawerViewController
#import <UIKit/UIKit.h>
@interface DrawerViewController : UIViewController
@property (weak, nonatomic, readonly) UIView *leftView;
@property (weak, nonatomic, readonly) UIView *rightView;
@property (weak, nonatomic, readonly) UIView *mainView;
@end
// -------------------------------------------------------
#import "DrawerViewController.h"
#define ScreenWidth [UIScreen mainScreen].bounds.size.width
#define ScreenHeight [UIScreen mainScreen].bounds.size.height
#define MaxOffsetY 100
#define MaxOffsetX ([UIScreen mainScreen].bounds.size.width - 100)
@implementation DrawerViewController
- (void)viewDidLoad {
[super viewDidLoad];
// 1. 初始化視圖
[self setup];
// 2. 給mainView添加拖動(dòng)手勢(shì)
UIPanGestureRecognizer *panGestureRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panGesture:)];
[self.mainView addGestureRecognizer:panGestureRecognizer];
// 3. 給self.view添加一個(gè)單擊手勢(shì)
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapGesture:)];
[self.view addGestureRecognizer:tap];
}
- (void)tapGesture:(UITapGestureRecognizer *)tap {
// mainView復(fù)位
[UIView animateWithDuration:0.2 animations:^{
self.mainView.frame = self.view.bounds;
}];
}
- (void)panGesture:(UIPanGestureRecognizer *)pan {
CGPoint offsetPoint = [pan translationInView:self.view];
self.mainView.frame = [self frameWithOffset:offsetPoint.x];
if (self.mainView.frame.origin.x > 0) {
// → 右移(顯示leftView)
self.rightView.hidden = YES;
} else if (self.mainView.frame.origin.x < 0) {
// ← 左移(顯示rightView)
self.rightView.hidden = NO;
}
// 如果拖拽結(jié)束,自動(dòng)定位
CGFloat targetOffsetX = 0;
if (pan.state == UIGestureRecognizerStateEnded) {
if (self.mainView.frame.origin.x >= ScreenWidth * 0.5) { // 右側(cè)
targetOffsetX = MaxOffsetX;
} else if (CGRectGetMaxX(self.mainView.frame) < ScreenWidth * 0.5){ // 左側(cè)
targetOffsetX = -MaxOffsetX;
}
// 計(jì)算出當(dāng)前位置距離目標(biāo)位置所需要的偏移距離
CGFloat offsetX = targetOffsetX - self.mainView.frame.origin.x;
// 滑動(dòng)不到屏幕一般時(shí)仍然顯示mainView(self.view.bounds) 否則自動(dòng)定位到左側(cè)或右側(cè)
CGRect mainFrame = targetOffsetX == 0 ? self.view.bounds : [self frameWithOffset:offsetX];
[UIView animateWithDuration:0.2 animations:^{
self.mainView.frame = mainFrame;
}];
}
[pan setTranslation:CGPointZero inView:self.view];
}
- (CGRect)frameWithOffset:(CGFloat)offsetX {
CGRect newFrame = self.mainView.frame;
newFrame.origin.x += offsetX; // x
CGFloat offsetY = self.mainView.frame.origin.x * MaxOffsetY / ScreenWidth;
newFrame.origin.y = fabs(offsetY); // y
CGFloat offsetHeight = ScreenHeight - (newFrame.origin.y * 2);
newFrame.size.height = offsetHeight; // height
return newFrame;
}
- (void)setup {
UIView *leftView = [[UIView alloc] initWithFrame:[UIScreen mainScreen].bounds];
//leftView.backgroundColor = [UIColor lightGrayColor];
_leftView = leftView;
[self.view addSubview:leftView];
UIView *rightView = [[UIView alloc] initWithFrame:[UIScreen mainScreen].bounds];
//rightView.backgroundColor = [UIColor magentaColor];
_rightView = rightView;
[self.view addSubview:rightView];
UIView *mainView = [[UIView alloc] initWithFrame:[UIScreen mainScreen].bounds];
//mainView.backgroundColor = [UIColor blackColor];
_mainView = mainView;
[self.view addSubview:mainView];
}
@end
使用封裝
1.將DrawerViewController類拖入到工程中,并繼承該類
2.分別創(chuàng)建LeftViewController、RightViewController、MainViewController
3.將每個(gè)視圖對(duì)應(yīng)的view添加到對(duì)應(yīng)的視圖上,并成為當(dāng)前控制器的子控制器
第一步:繼承DrawerViewController
#import <UIKit/UIKit.h> #import "DrawerViewController.h" @interface ViewController : DrawerViewController @end
第二步:分別創(chuàng)建LeftViewController、RightViewController、MainViewController
第三步:為leftView、rightView、mainView 添加子視圖,并將每天控制器作為當(dāng)前控制器的子控制器
#import "ViewController.h"
#import "LeftViewController.h"
#import "RightViewController.h"
#import "MainViewController.h"
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Main
MainViewController *mainViewController = [[MainViewController alloc] init];
mainViewController.view.frame = self.view.bounds;
mainViewController.view.backgroundColor = [UIColor brownColor];
[self.mainView addSubview:mainViewController.view];
[self addChildViewController:mainViewController];
// Left
LeftViewController *leftViewController = [[LeftViewController alloc] init];
leftViewController.view.frame = self.view.bounds;
leftViewController.view.backgroundColor = [UIColor purpleColor];
[self.leftView addSubview:leftViewController.view];
[self addChildViewController:leftViewController];
// Right
RightViewController *rightViewController = [[RightViewController alloc] init];
rightViewController.view.frame = self.view.bounds;
rightViewController.view.backgroundColor = [UIColor cyanColor];
[self.rightView addSubview:rightViewController.view];
[self addChildViewController:rightViewController];
}
@end
實(shí)現(xiàn)效果:

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- iOS開發(fā)之路--仿網(wǎng)易抽屜效果
- IOS中MMDrawerController第三方抽屜效果的基本使用示例
- iOS實(shí)現(xiàn)簡(jiǎn)單的抽屜效果
- iOS實(shí)現(xiàn)側(cè)拉欄抽屜效果
- IOS中Swift仿QQ最新版抽屜側(cè)滑和彈框視圖
- iOS實(shí)現(xiàn)左右拖動(dòng)抽屜效果
- IOS實(shí)現(xiàn)點(diǎn)擊滑動(dòng)抽屜效果
- ios仿側(cè)邊抽屜效果實(shí)現(xiàn)代碼
- iOS實(shí)現(xiàn)簡(jiǎn)易抽屜效果、雙邊抽屜效果
- iOS簡(jiǎn)單抽屜效果的實(shí)現(xiàn)方法
相關(guān)文章
iOS實(shí)現(xiàn)秒殺活動(dòng)倒計(jì)時(shí)
這篇文章主要為大家詳細(xì)介紹了iOS實(shí)現(xiàn)秒殺活動(dòng)倒計(jì)時(shí),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-12-12
iOS中幾種定時(shí)器的實(shí)現(xiàn)小結(jié)
這篇文章主要介紹了iOS中幾種定時(shí)器的實(shí)現(xiàn)小結(jié),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-01-01
iOS實(shí)現(xiàn)日歷翻頁(yè)動(dòng)畫
本文的內(nèi)容主要是在IOS中實(shí)現(xiàn)日歷翻頁(yè)的動(dòng)畫,界面簡(jiǎn)單但效果很好,以后可以運(yùn)用到app中,下面一起來看看。2016-08-08
ios實(shí)現(xiàn)底部PopupWindow的示例代碼(底部彈出菜單)
這篇文章主要介紹了ios實(shí)現(xiàn)底部PopupWindow的示例代碼(底部彈出菜單),小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-01-01
分享一個(gè)關(guān)于Storyboard 跳轉(zhuǎn)與傳值
近日不忙,給大家分享一個(gè)關(guān)于storyboard跳轉(zhuǎn)傳值的相關(guān)知識(shí),感興趣的朋友一起看看吧2015-12-12
IOS實(shí)現(xiàn)左右兩個(gè)TableView聯(lián)動(dòng)效果
在我們?nèi)粘i_發(fā)IOS中,經(jīng)常見到兩個(gè)tableview的聯(lián)動(dòng),滑動(dòng)一側(cè)tableview,另一側(cè)tableview跟著滑動(dòng),其實(shí)實(shí)現(xiàn)起來比較簡(jiǎn)單,只是需要搞清楚他們之間的區(qū)別和聯(lián)系,下面一起來看看如何實(shí)現(xiàn)。2016-08-08
iOS App開發(fā)中的UIPageControl分頁(yè)控件使用小結(jié)
UIPageControl分頁(yè)控件的例子簡(jiǎn)單來說即是我們平時(shí)翻動(dòng)多個(gè)桌面頁(yè)時(shí)及底部帶有的圓點(diǎn)頁(yè)碼標(biāo)注,這里我們來看一下iOS App開發(fā)中的UIPageControl分頁(yè)控件使用小結(jié),需要的朋友可以參考下2016-06-06
詳解iOS中Button按鈕的狀態(tài)和點(diǎn)擊事件
這篇文章先是給大家介紹iOS中Button按鈕的狀態(tài),而后又詳細(xì)介紹了iOS中按鈕點(diǎn)擊事件處理方式,本文介紹的很詳細(xì),有需要的朋友們可以參考借鑒,下面來一起看看吧。2016-09-09

