ios开发-新浪微博-09(下拉菜单封装一)

来源:互联网 发布:奥卡福 知乎 编辑:程序博客网 时间:2024/05/21 14:05

我们要对下来菜单封装
首先我们要考虑三件事情

1.创建下拉菜单

2.设置内容

3.显示

4.销毁

根据以上的需求 我们按照步骤完成即可

这里先对下拉菜单进行了初步的封装

#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)+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;    self.containerView.y = 50;        }/** *  销毁 */-(void)dismiss{    [self removeFromSuperview];}/*// Only override drawRect: if you perform custom drawing.// An empty implementation adversely affects performance during animation.- (void)drawRect:(CGRect)rect {    // Drawing code}*/@end



0 0
原创粉丝点击