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

88 篇文章 0 订阅

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

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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值