iOS開發(fā)實現(xiàn)簡單抽屜效果
本文實例為大家分享了iOS實現(xiàn)簡單抽屜效果的具體代碼,供大家參考,具體內(nèi)容如下
抽屜效果的原理:其實就是把兩個子控制器添加到一個RootViewController中,將子控制器的view添加到RootViewController的view上,然后改變子控制器view的frame實現(xiàn)抽屜的效果。
下面直接看看我自己寫的一個小demo。
RootViewController.h
//兩個子控制器leftView和midView @property(nonatomic,weak)UIViewController *leftView; @property(nonatomic,weak)UIViewController *midView;
RootViewController.m
- (void)viewDidLoad {
? ? [super viewDidLoad];
? ? //將leftView和midView添加到self中作為子控制器。將他們的view添加到self.view中
? ? [self addChildViewController:self.leftView];
? ? [self.view addSubview:self.leftView.view];
? ? [self addChildViewController:self.midView];
? ? [self.view addSubview:self.midView.view];
? ? //設置一個按鈕點擊實現(xiàn)抽屜效果
? ? UIButton *leftButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
? ? leftButton.frame = CGRectMake(0, 50, 150, 150);
? ? [leftButton addTarget:self action:@selector(leftButtonPressed) forControlEvents:UIControlEventTouchUpInside];
? ? [leftButton setTitle:@"left" forState:UIControlStateNormal];
? ? [self.midView.view addSubview:leftButton];
}
-(void)leftButtonPressed
{
? ? //判斷抽屜是否是展開狀態(tài)
? ? if (self.midView.view.frame.origin.x == 0) {
? ? ? ? //通過動畫實現(xiàn)view.fram的改變
? ? ? ? [UIView animateWithDuration:0.3 animations:^{
? ? ? ? ? ? /* ?W ?H ?屏幕實際大小宏
? ? ? ? ? ? ?* #define ScreenWidth [UIScreen mainScreen].bounds.size.width
? ? ? ? ? ? ?* #define ScreenHeight [UIScreen mainScreen].bounds.size.height
? ? ? ? ? ? */
? ? ? ? ? ? self.leftView.view.frame = CGRectMake(0, 0, W, H);
? ? ? ? ? ? self.midView.view.frame = CGRectMake(200, 50, W, H-50*2);
? ? ? ? } completion:^(BOOL finished) {
? ? ? ? }];
? ? }else{
? ? ? ? [UIView animateWithDuration:0.3 animations:^{
? ? ? ? ? ? self.midView.view.frame = CGRectMake(0, 0, W, H);
? ? ? ? } completion:^(BOOL finished) {
? ? ? ? }];
? ? }
}AppDelegate.m
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
? ? self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
? ? LeftViewController *leftView = [[LeftViewController alloc] init];
? ? MidViewController *midView = [[MidViewController alloc]init];
? ? RootViewController *rootView = [[RootViewController alloc]init];
? ? rootView.leftView = leftView;
? ? rootView.midView = midView;
? ? self.window.rootViewController = rootView;
? ? [self.window makeKeyAndVisible];
? ? return YES;
}運行代碼,效果圖如下:


以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
IOS UITableView和NavigationBar的常用設置詳解
這篇文章主要介紹了IOS UITableView和NavigationBar的常用設置詳解的相關(guān)資料,需要的朋友可以參考下2017-04-04
iOS11 WKWebView 無法加載內(nèi)容的解決方法
這篇文章主要介紹了iOS11 WKWebView 無法加載內(nèi)容,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-11-11
iOS基礎(chǔ)知識之@property 和 Ivar 的區(qū)別
這篇文章主要介紹了iOS基礎(chǔ)知識之@property 和 Ivar 的區(qū)別介紹,非常不錯,具有參考借鑒價值,需要的朋友可以參考下2016-08-08

