ios开发-新浪微博10-(下拉菜单的二次封装 完整版)
2015-09-16 21:09
399 查看
为了供大家的参考学习特意将每个知识点 分开列出
注意这里的封装
我们对下拉菜单的 大小 内容大小 位置 都进行了改进
可以传入个UI控件 也可以传入一个控制器
当下拉菜单弹出的时候 要遮盖一层 将菜单意外界面 事件屏蔽
当点击空白处 下拉菜单收回
这里用到了转换坐标系 这个知识点在后面也会讲解到
注意这里的封装
我们对下拉菜单的 大小 内容大小 位置 都进行了改进
可以传入个UI控件 也可以传入一个控制器
当下拉菜单弹出的时候 要遮盖一层 将菜单意外界面 事件屏蔽
当点击空白处 下拉菜单收回
这里用到了转换坐标系 这个知识点在后面也会讲解到
#import <UIKit/UIKit.h> @interface QHDropdownMenu : UIView +(instancetype)menu; @property(nonatomic,strong)UIView *content; @property(nonatomic,strong)UIViewController *contentController; /** * 显示 */ -(void)showFrom:(UIView *)from; /** * 销毁 */ -(void)dismiss; @end
#import "QHDropdownMenu.h" @interface QHDropdownMenu() /** * 将来用来显示具体内容的容器 */ @property(nonatomic,strong)UIImageView *containerView; @end @implementation QHDropdownMenu //懒加载一般用强指针 //除非先创建 -(UIImageView *)containerView { if (!_containerView) { //添加一个灰色图片控件 UIImageView *containerView = [[UIImageView alloc]init]; containerView.image = [UIImage imageNamed:@"popover_background"]; // containerView.width = 217; // containerView.height = 217; containerView.userInteractionEnabled = YES; [self addSubview:containerView]; self.containerView = containerView; } return _containerView; } -(id)initWithFrame:(CGRect)frame { self = [super initWithFrame:frame]; if (self) { //清除颜色 self.backgroundColor = [UIColor clearColor]; // //添加一个灰色图片控件 // UIImageView *containerView = [[UIImageView alloc]init]; // containerView.image = [UIImage imageNamed:@"popover_background"]; // containerView.width = 217; // containerView.height = 217; // containerView.userInteractionEnabled = YES; // [self addSubview:containerView]; // self.containerView = containerView; } return self; } +(instancetype)menu { return [[self alloc]init]; } -(void)setContent:(UIView *)content { _content = content; //调整内部位置 content.x = 10; content.y = 15; //设置内容的宽度 // content.width = self.containerView.width - 2*content.x; //设置灰色的高度 self.containerView.height = CGRectGetMaxY(content.frame)+11; //设置灰色的宽度 self.containerView.width = CGRectGetMaxX(content.frame)+10; //添加内容到灰色图片中 [self.containerView addSubview:content]; } -(void)setContentController:(UIViewController *)contentController { _contentController = contentController; self.content = contentController.view; } /** * 显示 */ -(void)showFrom:(UIView *)from { //1.获得最上面的窗口 UIWindow *window = [[UIApplication sharedApplication].windows lastObject]; //2.添加自己到窗口 [window addSubview:self]; //3.设置尺寸 创建出来都是全屏的 self.frame = window.bounds; //4.调整灰色图片的位置 self.containerView.x = (self.width - self.containerView.width)*0.5; //默认情况下,frame是父控件左上角坐标为原点 //转换坐标新 CGRect newFrame = [from convertRect:from.bounds toView:window]; self.containerView.centerX = CGRectGetMidX(newFrame); self.containerView.y = CGRectGetMaxY(newFrame); } /** * 销毁 */ -(void)dismiss { [self removeFromSuperview]; } /** * 监听整个遮盖的面积 * * @param touches <#touches description#> * @param event <#event description#> */ - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { [self dismiss]; } /* // Only override drawRect: if you perform custom drawing. // An empty implementation adversely affects performance during animation. - (void)drawRect:(CGRect)rect { // Drawing code } */ @end
相关文章推荐
- ios开发-新浪微博-09(下拉菜单封装一)
- wxhl iOS bj 49 张浩 学习心得体会连载 第10章
- ios开发-新浪微博08-下拉菜单的基本实现
- ios-新浪微博开发07-自定义搜索框
- 猫猫学iOS 之微博项目实战(10)微博cell中图片的显示以及各种填充模式简介
- iOS现成的引导页面的实现:纯代码
- ios7.1以后,iphone上隐藏应用图标的方法
- IOS afn三方上传图片
- IOS rumen
- iOS开发 -- WebSocket 通信(一)
- iOS中FMDB简介
- IOS-六种手势的简单使用
- IOS开发学习的思维导图
- iOS6 创建全局的pch文件
- iOS中几种数据永久存储方式
- iOS 9: Getting Started With SFSafariViewController
- iOS 9适配
- iOS 7的手势滑动返回功能
- iOS开发小技巧总汇(不定时增添)
- IOS 控件 TextField设置大全