废话不多说,直接看代码
.h文件
@interface YAlertView : UIView
- (instancetype)initWithTitleArray:(NSArray *)titleArray finish:(void (^)(NSInteger index))finish;
// 显示
- (void)show;
@end
.m文件
@interface YAlertView ()
@property (nonatomic,strong) UIView *mainView;// 主界面
@property (nonatomic,copy) NSArray *titleArray;
@property (nonatomic,copy) void(^finish)(NSInteger index);
@end
static CGFloat const bottomH = 5;
static CGFloat const gap = 1;
static CGFloat const blockH = 50;
@implementation YAlertView
- (instancetype)initWithTitleArray:(NSArray *)titleArray finish:(void (^)(NSInteger index))finish
{
self = [super init];
if (self) {
self.titleArray = titleArray;
self.finish = finish;
[self initViews];
}
return self;
}
- (void)initViews{
[[UIApplication sharedApplication].keyWindow addSubview:self];
[self mas_makeConstraints:^(MASConstraintMaker *make) {
make.edges.equalTo([UIApplication sharedApplication].keyWindow);
}];
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapClick:)];
[self addGestureRecognizer:tap];
self.mainView = [[UIView alloc] init];
self.mainView.backgroundColor = kHexColor(0xE8E8EA);
NSInteger count = self.titleArray.count;
CGFloat tempHeight = blockH + bottomH +count*(blockH + gap) - gap;
[self addSubview:self.mainView];
[self.mainView mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.right.equalTo(self);
make.bottom.equalTo(self.mas_bottom).offset(tempHeight);
make.height.mas_equalTo(tempHeight);
}];
// 取消按钮
UIButton *cancelButton = [self createButtonWithTitle:@"取消"];
[self.mainView addSubview:cancelButton];
[cancelButton mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.right.bottom.equalTo(self.mainView);
make.height.mas_equalTo(blockH);
}];
[self.titleArray enumerateObjectsUsingBlock:^(NSString * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
UIButton *tempButton = [self createButtonWithTitle:obj];
tempButton.tag = 100 + idx;
[self.mainView addSubview:tempButton];
[tempButton mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.right.equalTo(self.mainView);
make.height.mas_equalTo(blockH);
make.bottom.equalTo(cancelButton.mas_top).offset(-(bottomH + (gap + blockH)*idx));
}];
}];
[self layoutIfNeeded];
}
// 按钮生成器
- (UIButton *)createButtonWithTitle:(NSString *)title {
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.backgroundColor = [UIColor whiteColor];
[button setTitle:title forState:UIControlStateNormal];
[button addTarget:self action:@selector(buttonClick:) forControlEvents:(UIControlEventTouchUpInside)];
[button setTitleColor:[UIColor blackColor] forState:(UIControlStateNormal)];
button.titleLabel.font = [UIFont systemFontOfSize:18];
return button;
}
// 显示
- (void)show{
// 动画 背景色、main浮现
[UIView animateWithDuration:0.25 delay:0 options:UIViewAnimationOptionCurveLinear animations:^{
self.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.3];
[self.mainView mas_updateConstraints:^(MASConstraintMaker *make) {
make.bottom.equalTo(self.mas_bottom);
}];
[self layoutIfNeeded];
} completion:^(BOOL finished) {
}];
}
#pragma mark -- action
- (void)buttonClick:(UIButton *)sender{
// 动画
[UIView animateWithDuration:0.25 delay:0 options:UIViewAnimationOptionCurveLinear animations:^{
self.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0];
[self.mainView mas_updateConstraints:^(MASConstraintMaker *make) {
make.bottom.equalTo(self.mas_bottom).offset(blockH + bottomH +self.titleArray.count*(blockH + gap) - gap);
}];
[self layoutIfNeeded];
} completion:^(BOOL finished) {
[self removeFromSuperview];
}];
// block回调
if (self.finish) {
self.finish(sender.tag - 100);
}
}
- (void)tapClick:(UITapGestureRecognizer *)tap{
CGPoint point = [tap locationInView:tap.view];
if (!CGRectContainsPoint(self.mainView.frame, point)) {
// 动画
[UIView animateWithDuration:0.25 delay:0 options:UIViewAnimationOptionCurveLinear animations:^{
self.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0];
[self.mainView mas_updateConstraints:^(MASConstraintMaker *make) {
make.bottom.equalTo(self.mas_bottom).offset(blockH + bottomH +self.titleArray.count*(blockH + gap) - gap);
}];
[self layoutIfNeeded];
} completion:^(BOOL finished) {
[self removeFromSuperview];
}];
}
}
注:
点击事件通过block回调处理