iOS如何用100行代碼實(shí)現(xiàn)簡(jiǎn)單的抽屜效果
前言
iOS中抽屜效果的簡(jiǎn)單實(shí)現(xiàn)現(xiàn)在很多應(yīng)用中都使用到了,網(wǎng)上也有很多了例子,本文主要是通過(guò)簡(jiǎn)單的一些代碼來(lái)實(shí)現(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];
// 給左視圖和主視圖添加手勢(shì)
[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;
}
}
// 給左視圖和主視圖添加手勢(shì)
- (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];
}
// 平移和縮放一個(gè)視圖
- (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é)
以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作能有所幫助,如果有疑問(wèn)大家可以留言交流。
- iOS實(shí)現(xiàn)頂部標(biāo)簽式導(dǎo)航欄及下拉分類菜單
- iOS從App跳轉(zhuǎn)至系統(tǒng)設(shè)置菜單各功能項(xiàng)的編寫方法講解
- iOS中長(zhǎng)按調(diào)出菜單組件UIMenuController的使用實(shí)例
- 如何使用jQuery技術(shù)開(kāi)發(fā)ios風(fēng)格的頁(yè)面導(dǎo)航菜單
- IOS代碼筆記之下拉菜單效果
- IOS中safari下的select下拉菜單文字過(guò)長(zhǎng)不換行的解決方法
- iOS開(kāi)發(fā)中WebView的基本使用方法簡(jiǎn)介
- IOS開(kāi)發(fā)代碼分享之設(shè)置UISearchBar的背景顏色
- iOS毛玻璃效果的實(shí)現(xiàn)及圖片模糊效果的三種方法
- iOS實(shí)現(xiàn)簡(jiǎn)單的二級(jí)菜單效果
相關(guān)文章
ios通過(guò)SDWebImage實(shí)現(xiàn)圖片加載時(shí)的漸變效果
本篇文章主要介紹了ios通過(guò)SDWebImage實(shí)現(xiàn)圖片加載時(shí)的漸變效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-04-04
IOS 開(kāi)發(fā)自定義條形ProgressView的實(shí)例
這篇文章主要介紹了IOS 開(kāi)發(fā)自定義條形ProgressView的實(shí)例的相關(guān)資料,希望開(kāi)發(fā)自己的條形進(jìn)度條的朋友可以參考下2016-10-10
iOS中的表單按鈕選項(xiàng)UIActionSheet常用方法整理
UIActionSheet經(jīng)常被用來(lái)制作各種彈出的選項(xiàng),這里我們就來(lái)看一下iOS中的表單按鈕選項(xiàng)UIActionSheet常用方法整理,需要的朋友可以參考下2016-06-06
iOS touch事件區(qū)分單擊雙擊響應(yīng)的方法
如果您的 iPhone 應(yīng)用里有個(gè) view,既有單擊操作又有雙擊操作。用戶雙擊 view 時(shí),總是先執(zhí)行一遍單擊的操作再執(zhí)行雙擊的操作。所以直接判斷時(shí)就會(huì)發(fā)現(xiàn)不能直接進(jìn)入雙擊操作。下面是區(qū)分 touch 事件是單擊還是雙擊的方法,需要的朋友可以參考下2016-10-10
IOS設(shè)備上給body綁定click事件不生效的原因及解決辦法
最近在做移動(dòng)端的項(xiàng)目,在ios上對(duì)body綁定click事件實(shí)現(xiàn)事件代理冒泡至某些元素上不生效,怎么回事,如何解決呢?今天小編給大家?guī)?lái)了IOS設(shè)備上給body綁定click事件不生效的原因及解決辦法,一起看看吧2016-09-09
在IOS系統(tǒng)上滾動(dòng)條滾動(dòng)到指定的位置出現(xiàn)空白頁(yè)面的解決方案
這篇文章主要介紹了 在IOS系統(tǒng)上滾動(dòng)條滾動(dòng)到指定的位置出現(xiàn)空白頁(yè)面的解決方案,需要的朋友可以參考下2017-01-01
詳解iOS App中UIPickerView滾動(dòng)選擇欄的添加方法
UIPickerView組件在應(yīng)用中選擇地區(qū)等方面的運(yùn)用非常常見(jiàn),能夠提供多列的選擇項(xiàng),下買呢我們就來(lái)詳解iOS App中UIPickerView滾動(dòng)選擇欄的添加方法2016-05-05

