HEDropdownMenu.h 的代码
#import <UIKit/UIKit.h>
@class HEDropdownMenu;
@protocol HEDropdownMenuDelegate <NSObject>
@optional
- (void)dropdownMenuDidDismiss:(HEDropdownMenu *)menu;
- (void)dropdownMenuDidShow:(HEDropdownMenu *)menu;
@end
@interface HEDropdownMenu : UIView
@property (nonatomic, weak) id<HEDropdownMenuDelegate> delegate;
+ (instancetype)menu;
/**
* 显示
*/
- (void)showFrom:(UIView *)from;
/**
* 销毁
*/
- (void)dismiss;
/**
* 内容
*/
@property (nonatomic, strong) UIView *content;
/**
* 内容控制器
*/
@property (nonatomic, strong) UIViewController *contentController;
@end
HEDropdownMenu.m 的代码
#import "HEDropdownMenu.h"
@interface HEDropdownMenu()
/**
* 将来用来显示具体内容的容器
*/
@property (nonatomic, weak) UIImageView *containerView;
@end
@implementation HEDropdownMenu
- (UIImageView *)containerView
{
if (!_containerView) {
// 添加一个灰色图片控件
UIImageView *containerView = [[UIImageView alloc] init];
containerView.image = [UIImage imageNamed:@"popover_background"];
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];
}
return self;
}
+ (instancetype)menu
{
return [[self alloc] init];
}
- (void)setContent:(UIView *)content
{
_content = content;
// 调整内容的位置
content.frame = CGRectMake(10, 15, content.frame.size.width, content.frame.size.height);
// 调整内容的宽度
// content.width = self.containerView.width - 2 * content.x;
self.containerView.frame = CGRectMake(0, 0, CGRectGetMaxX(content.frame) + 10, CGRectGetMaxY(content.frame) + 11);
// 添加内容到灰色图片中
[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.调整灰色图片的位置
// 默认情况下,frame是以父控件左上角为坐标原点
// 转换坐标系
CGRect newFrame = [from convertRect:from.bounds toView:window];
// CGRect newFrame = [from.superview convertRect:from.frame toView:window];
// self.containerView.centerX = CGRectGetMidX(newFrame);
// self.containerView.y = CGRectGetMaxY(newFrame);
self.containerView.center = CGPointMake(CGRectGetMidX(newFrame), self.containerView.center.y);
self.containerView.frame = CGRectMake(self.containerView.frame.origin.x, CGRectGetMaxY(newFrame), self.containerView.frame.size.width, self.containerView.frame.size.height);
// 通知外界,自己显示了
if ([self.delegate respondsToSelector:@selector(dropdownMenuDidShow:)]) {
[self.delegate dropdownMenuDidShow:self];
}
}
/**
* 销毁
*/
- (void)dismiss
{
[self removeFromSuperview];
// 通知外界,自己被销毁了
if ([self.delegate respondsToSelector:@selector(dropdownMenuDidDismiss:)]) {
[self.delegate dropdownMenuDidDismiss:self];
}
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
[self dismiss];
}
@end