iOS Kingdom — 滑動選單
左滑選單是我們在開發 App 應用時常用的一種展示選單方式,下面我們來看下我們這次要實現的目標。。。
.實現原理
先在你的檢視的左邊加上一個 subView ,然後再在你的檢視上加上左滑手勢,當你的選單向左移動時,你檢視上的所有子 view 都跟著動起來。
.實現準備
1、先建立一個 UIView 的分類 UIView+Menu.h
2、實現一個為 View 新增選單的方法
- (void)addMenu:(UIView *)view;
3、建立一些例項變數來儲存我們須要的值
UIView *menu; // 選單檢視
BOOL isShow; // 選單是否已出現
BOOL isAnimation; // 動畫是否完成
CGPoint beginPoint; // 記錄第一個觸屏點
CGPoint changePoint; // 記錄滑動到的點
CGRect initMenuRect; // 選單檢視的初始值
SwipeLocation swipeLocation;//滑動方向
4、滑動方向
typedef enum {
SwipeInit,
SwipeLeft,
SwipeRight,
SwipeUp,
SwipeDown
}SwipeLocation;
.實現
- (void)addMenu:(UIView *)view{
menu = view;
// 將選單放在 view 的左邊
menu.x = -menu.width;
self.layer.masksToBounds = YES;
[self addSubview:menu];
initMenuRect = menu.frame;
UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(pan:)];
[self addGestureRecognizer:pan];
}
新增 pan: 方法來實現滑動
- (void)pan:(UIPanGestureRecognizer *)gesture{
CGPoint point = [gesture translationInView:self];
// 滑動百分比
CGFloat precent = point.x/self.width;
switch (gesture.state) {
case UIGestureRecognizerStateBegan: {
swipeLocation = SwipeInit;
beginPoint = point;
break;
}
case UIGestureRecognizerStateChanged: {
// 記錄滑動點
changePoint = point;
// 判斷滑動方向
[self judgeDirection];
if (menu.x < - menu.width || menu.x > 0) {
// 選單已收起不能再左滑,已出現不能再右滑
return;
}
for (UIView *view in [self subviews]) {
if (isShow) {
// hide
if (precent > 0) {
return;
}
if (menu.x > -menu.width) {
view.x += menu.width*precent - menu.x;
}
}else {
// show
if (precent < 0){
return;
}
if (menu.x < 0) {
view.x += (menu.width*precent - (menu.x - initMenuRect.origin.x));
}
}
}
break;
}
default:{
// 滑動的百分比大於 0.3 時自動收起或顯示選單
if (fabs(precent) > 0.3) {
if (isShow) {
if (swipeLocation == SwipeLeft) {
isShow = YES;
[self hideMenuDuration:0.2];
}else {
[self leftAnimationDuration:0.2];
}
}else {
if (swipeLocation == SwipeRight) {
[self leftAnimationDuration:0.2];
}else {
isShow = YES;
[self hideMenuDuration:0.2];
}
}else {
if (isShow) {
[self leftAnimationDuration:0.2];
}else {
isShow = YES;
[self hideMenuDuration:0.2];
}
}
break;
}
}
判斷滑動方向
/**
* 判斷滑動手勢
*/
- (void)judgeDirection{
if (showLocation == Left || showLocation == Right) {
if (changePoint.x - beginPoint.x > 0) {
swipeLocation = SwipeRight;
}else {
swipeLocation = SwipeLeft;
}
}else {
if (changePoint.y - beginPoint.y > 0) {
swipeLocation = SwipeDown;
}else {
swipeLocation = SwipeUp;
}
}
}
選單從左邊出現動畫
- (void)leftAnimationDuration:(NSTimeInterval)duration{
isAnimation = YES;
[UIView animateWithDuration:duration
delay:0
options:UIViewAnimationOptionCurveEaseIn
animations:^{
for (UIView *subView in [self subviews]) {
subView.x += (0 - menu.x);
}
} completion:^(BOOL finished) {
isShow = finished;
isAnimation = !finished;
}];
}
隱藏動畫
- (void)hideMenuDuration:(NSTimeInterval)duration{
if (!isShow || isAnimation) {
return;
}
isAnimation = YES;
for (UIView *view in [self subviews]) {
[UIView animateWithDuration:duration
delay:0
options:UIViewAnimationOptionCurveLinear
animations:^{
view.x -= (menu.x - initMenuRect.origin.x);
} completion:^(BOOL finished) {
isShow = !finished;
isAnimation = !finished;
}];
}
}
相關文章
- (iOS)可以這樣來玩玩tableViewCell的滑動選單iOSView
- 微信小程式:選單滑動微信小程式
- jQuery滑動導航選單jQuery
- (有圖)仿QQ側滑選單:RecyclerView側滑選單,長按拖拽,滑動刪除View
- 圓形可滑動選單(可以動態新增選單項)
- iOS 一個滑動選擇控制元件iOS控制元件
- CollectionView 單個選項卡的滑動View
- (十)如果實現滑動展示選單效果
- 透明層滑動跟隨導航選單
- 函式節流實現滑動下拉選單函式
- css3實現側邊滑動選單CSSS3
- jQuery背景滑動跟隨的導航選單jQuery
- iOS開發UI篇--一個側滑選單SlidingMenuiOSUI
- iOS7滑動返回iOS
- 滑鼠滑過,展示下拉選單
- 史上最簡單,一步整合側滑(刪除)選單,高仿QQ、IOS。iOS
- css3滑鼠懸浮背景滑動導航選單CSSS3
- 帶有視覺滾動差的選單側滑欄視覺
- iOS11 UITableViewCell滑動事件改動iOSUIView事件
- Android側滑選單DrawerLayout使用Android
- Swift - 仿寫QQ側滑選單Swift
- 用Vue擼一個『A-Z字母滑動檢索選單』Vue
- 使用 jQuery 和 CSS3 製作滑動導航選單jQueryCSSS3
- 自定義View:側滑選單實現View
- js橫向滑動摺疊導航選單程式碼例項JS
- 滑動選單欄(一)開源專案SlidingMenu的使用
- MySQL 滑動訂單問題MySql
- Flutter | 超簡單仿微信QQ側滑選單元件Flutter元件
- iOS UITableViewCell允許滑動的處理iOSUIView
- iOS6下實現滑動返回iOS
- 原生Android 側滑選單實踐(部分)Android
- 自定義View:側滑選單動畫實現View動畫
- android的左右側滑選單實現Android
- 聊天平臺原始碼,簡單使用 禁止滑動和設定滑動方向原始碼
- iOS UISlider數值與滑塊聯動iOSUIIDE
- 自定義view——仿酷狗的側滑選單View
- 【只發精品】匠心打造Vue側滑選單元件Vue元件
- iOS 如何絲滑的側滑返回iOS