ios开发-新浪微博10-(下拉菜单的二次封装 完整版)

88 篇文章 0 订阅

为了供大家的参考学习特意将每个知识点 分开列出

注意这里的封装

我们对下拉菜单的 大小 内容大小 位置 都进行了改进

可以传入个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



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值