iOS开发——抽屉效果实现
来源:互联网 发布:mac口红正红色色号 编辑:程序博客网 时间:2024/06/05 10:06
抽屉效果的原理:其实就是把两个子控制器添加到一个RootViewController中,将子控制器的view添加到RootViewController的view上,然后改变子控制器view的frame实现抽屉的效果。
下面直接看看我自己写的一个小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]; //设置一个按钮点击实现抽屉效果 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{ //判断抽屉是否是展开状态 if (self.midView.view.frame.origin.x == 0) { //通过动画实现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;}
运行代码,效果图如下:
1 0
- iOS开发——抽屉效果实现
- iOS开发之抽屉效果实现
- ios 抽屉效果实现
- iOS实现抽屉效果
- iOS 抽屉效果实现
- iOS 开发抽屉效果 MMDrawerController
- IOS开发中的抽屉效果
- iOS-简单抽屉效果实现
- iOS 简单实现抽屉效果
- IOS 实现简单抽屉效果
- IOS 开发使用UITableView实现抽屉打开关闭效果
- iOS开发-18.两种方法实现抽屉效果
- 实现IOS版的抽屉效果
- 【代码笔记】iOS-抽屉效果的实现
- 安卓开发SlidingDrawer实现抽屉效果
- 安卓开发SlidingDrawer实现抽屉效果
- Android开发之DrawerLayout实现抽屉效果
- 玩转iOS开发 - 简易的实现2种抽屉效果
- mysql主主关系配置
- CTS DownloadManagerTest testMinimumDownload failed
- java-----String的intern关键字
- Java异常处理(1)
- Web前端面试题目汇总
- iOS开发——抽屉效果实现
- java的设计模式 建造者模式
- [CortexM3--CC26XX]Watchdog Timer(exp)
- 欢迎使用CSDN-markdown编辑器
- LoadRunner如何测试mysql语句
- Eclipse 中关联JDK源代码
- oracle 查询当前占用cpu及时间最长的sql语句
- Theme.AppCompat.Light无法找到问题
- 【mahapps.metro】WPF“关于”窗体