iOS如何用100行代碼實現(xiàn)簡單的抽屜效果
前言
iOS中抽屜效果的簡單實現(xiàn)現(xiàn)在很多應(yīng)用中都使用到了,網(wǎng)上也有很多了例子,本文主要是通過簡單的一些代碼來實現(xiàn)的,有需要的可以一起學(xué)習(xí)學(xué)習(xí)。
下面是效果圖
示例代碼如下
#import <UIKit/UIKit.h> @interface MainViewController : UIViewController + (instancetype)mainViewControllerWithLeftViewController:(UIViewController *)leftViewController withMainPageViewController:(UIViewController *)mainPageViewController; @end
#import "MainViewController.h" #define KWidth self.view.frame.size.width #define KHeight self.view.frame.size.height @interface MainViewController () @property (nonatomic,strong)UIViewController *leftVC; @property (nonatomic,strong)UIViewController *centerVC; @property (nonatomic,assign)BOOL isSlider; @property (nonatomic,strong)UIView *corverView; @end @implementation MainViewController + (instancetype)mainViewControllerWithLeftViewController:(UIViewController *)leftViewController withMainPageViewController:(UIViewController *)mainPageViewController{ MainViewController *mainVC = [[MainViewController alloc] init]; mainVC.leftVC = leftViewController; mainVC.centerVC = mainPageViewController; return mainVC; } - (void)viewDidLoad{ [super viewDidLoad]; self.isSlider = NO; self.view.backgroundColor = [UIColor whiteColor]; [self addChildViewController:self.leftVC]; [self.view addSubview:self.leftVC.view]; [self addChildViewController:self.centerVC]; [self.view addSubview:self.centerVC.view]; // 給左視圖和主視圖添加手勢 [self addGestureForView]; } // 給主視圖添加遮蓋 - (void)addCorverView{ if (self.corverView) { [self.corverView removeFromSuperview]; self.corverView = nil; } self.corverView = [[UIView alloc] initWithFrame:self.centerVC.view.bounds]; _corverView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.0]; [self.centerVC.view addSubview:self.corverView]; } // 移除主視圖遮蓋 - (void)removeCoverView{ if (self.corverView) { [self.corverView removeFromSuperview]; self.corverView = nil; } } // 給左視圖和主視圖添加手勢 - (void)addGestureForView{ UISwipeGestureRecognizer *rightGesture = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeRightAction:)]; rightGesture.direction = UISwipeGestureRecognizerDirectionRight; [self.centerVC.view addGestureRecognizer:rightGesture]; UISwipeGestureRecognizer *leftGesture = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeLeftAction:)]; leftGesture.direction = UISwipeGestureRecognizerDirectionLeft; [self.centerVC.view addGestureRecognizer:leftGesture]; UISwipeGestureRecognizer *leftVCLeftSwipeGesture = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(leftVCLeftSwipeAction:)]; leftVCLeftSwipeGesture.direction = UISwipeGestureRecognizerDirectionLeft; [self.leftVC.view addGestureRecognizer:leftVCLeftSwipeGesture]; } - (void)swipeRightAction:(id)sender{ [self moveView:self.centerVC.view scale:0.8 panValue:KWidth]; self.isSlider = YES; [self addCorverView]; } - (void)swipeLeftAction:(id)sender{ [self moveView:self.centerVC.view scale:1 panValue:KWidth / 2]; self.isSlider = NO; [self removeCoverView]; } - (void)leftVCLeftSwipeAction:(id)sender{ [self moveView:self.centerVC.view scale:1 panValue:KWidth / 2]; self.isSlider = NO; [self removeCoverView]; } // 平移和縮放一個視圖 - (void)moveView:(UIView *)view scale:(CGFloat)scale panValue:(CGFloat)value{ [UIView beginAnimations:nil context:nil]; view.transform = CGAffineTransformScale(CGAffineTransformIdentity,scale,scale); view.center = CGPointMake(value, view.center.y); [UIView commitAnimations]; } @end
總結(jié)
以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作能有所幫助,如果有疑問大家可以留言交流。
- iOS實現(xiàn)頂部標簽式導(dǎo)航欄及下拉分類菜單
- iOS從App跳轉(zhuǎn)至系統(tǒng)設(shè)置菜單各功能項的編寫方法講解
- iOS中長按調(diào)出菜單組件UIMenuController的使用實例
- 如何使用jQuery技術(shù)開發(fā)ios風(fēng)格的頁面導(dǎo)航菜單
- IOS代碼筆記之下拉菜單效果
- IOS中safari下的select下拉菜單文字過長不換行的解決方法
- iOS開發(fā)中WebView的基本使用方法簡介
- IOS開發(fā)代碼分享之設(shè)置UISearchBar的背景顏色
- iOS毛玻璃效果的實現(xiàn)及圖片模糊效果的三種方法
- iOS實現(xiàn)簡單的二級菜單效果
相關(guān)文章
IOS百度地圖導(dǎo)航開發(fā)功能實現(xiàn)簡述
百度地圖導(dǎo)航非常實用,那么基于代碼是如何實現(xiàn)的呢,下面通過本文給大家介紹IOS百度地圖導(dǎo)航開發(fā)功能實現(xiàn)簡述,需要的朋友可以參考下本文2016-03-03IOS開發(fā)中使用UIFont設(shè)置字體及批量創(chuàng)建控件
這篇文章主要介紹了IOS開發(fā)中使用UIFont設(shè)置字體及批量創(chuàng)建控件的方法,內(nèi)容很實用,感興趣的小伙伴們可以參考一下2016-03-03iOS DispatchSourceTimer 定時器的具體使用
定時器在很多地方都可以用到,本文主要介紹了iOS DispatchSourceTimer 定時器的具體使用,文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-05-05iOS中的多線程如何按設(shè)定順序去執(zhí)行任務(wù)詳解
多線程相信大家或多或少都有所了解吧,下面這篇文章主要給大家介紹了關(guān)于iOS中多線程如何按設(shè)定順序去執(zhí)行任務(wù)的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對各位iOS開發(fā)者們的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面來一起看看吧。2017-12-12iOS中 UIImage根據(jù)屏寬調(diào)整size的實例代碼
最近做項目遇到這樣一個需求,要求UIImage根據(jù)屏幕寬度按照自己本身比例改變高度,下面通過本文給大家分享iOS UIImage根據(jù)屏寬調(diào)整size的實例代碼,需要的朋友參考下吧2017-01-01