iOS 自定义弹框

一个可以自定义弹出视图内容,弹出视图所在位置的小轮子。

#import "CommanBottomPopView.h"

#define LC_ACTION_SHEET_COLOR(r, g, b)      [UIColor colorWithRed:(r)/255.0f green:(g)/255.0f blue:(b)/255.0f alpha:1.0f]

@interface CommanBottomPopView ()

@property(nonatomic, weak) UIView *darkView;
@property(nonatomic, weak) UIView *customView;

@end

@implementation CommanBottomPopView

+ (instancetype)bottomPopWithCustomView:(UIView *)customView positionState:(PositionState)state{
    return [[self alloc] initWithCustomView:customView positionState:state];
}

- (instancetype)initWithCustomView:(UIView *)customView positionState:(PositionState)state{
    if (self = [super init]) {
        self.customView = customView;
        [self setupViewWithPosition:state];
    }
    return self;
}

- (void)setupViewWithPosition:(PositionState)state {
    UIWindow *keyWindow = [UIApplication sharedApplication].keyWindow;
    [keyWindow addSubview:self];
    [self makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(keyWindow);
    }];
    if (self.customView) {
        UIView *darkView = [[UIView alloc] init];
        darkView.alpha = 0;
        darkView.userInteractionEnabled = NO;
        darkView.backgroundColor = LC_ACTION_SHEET_COLOR(46, 49, 50);
        [self addSubview:darkView];
        [darkView makeConstraints:^(MASConstraintMaker *make) {
            make.edges.equalTo(@(0));
        }];
        self.darkView = darkView;
        
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(darkViewClicked)];
        [darkView addGestureRecognizer:tap];
        [self addSubview:self.customView];
        
        CGFloat height = CGRectGetHeight(self.customView.frame);
        CGFloat width = CGRectGetWidth(self.customView.frame);
        CGFloat top = [UIScreen mainScreen].bounds.size.height - height;
        
        
        if (state == PositionStateCenter) {
            [self.customView mas_updateConstraints:^(MASConstraintMaker *make) {
                make.centerX.mas_equalTo(self.mas_centerX);
                make.centerY.mas_equalTo(self.mas_centerY);
                make.height.mas_equalTo(@(height));
                make.width.mas_equalTo(@(width));
            }];
        } else if (state == PositionStateBottom) {
            [self.customView mas_updateConstraints:^(MASConstraintMaker *make) {
                make.top.mas_equalTo(@(top));
                make.centerX.mas_equalTo(self.mas_centerX);
                make.height.mas_equalTo(@(height));
                make.width.mas_equalTo(@(width));
            }];
        } else {
            
        }
    
        self.customView.hidden = YES;
        return;
    }
}

- (void)darkViewClicked {
    [self hideSelf];
}

- (void)show {
    [self layoutIfNeeded];
    __weak typeof (self) weakSelf = self;
    [UIView animateWithDuration:0.3f delay:0 options:UIViewAnimationOptionCurveEaseOut animations:^{
        weakSelf.darkView.alpha = .4;
        weakSelf.darkView.userInteractionEnabled = YES;
        if (weakSelf.customView) {
            weakSelf.customView.hidden = NO;
        }
        [weakSelf layoutIfNeeded];
    } completion:^(BOOL finished) {
        
    }];
}

- (void)hideSelf {
    __weak typeof (self) weakSelf = self;
    [UIView animateWithDuration:0.3f delay:0 options:UIViewAnimationOptionCurveEaseOut animations:^{
        weakSelf.darkView.alpha = 0;
        weakSelf.darkView.userInteractionEnabled = NO;
        if (weakSelf.customView) {
            weakSelf.customView.hidden = YES;
        }
        [weakSelf layoutIfNeeded];
    } completion:^(BOOL finished) {
        [weakSelf removeFromSuperview];
    }];
}

- (void)dealloc {
    NSLog(@"dealloc -- %@", [self class]);
}

使用:

- (void)popView {
    self.customView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 200)];
    self.customView.backgroundColor = [UIColor redColor];
    self.popBgView = [CommanBottomPopView bottomPopWithCustomView:self.customView positionState:PositionStateCenter];
    [self.popBgView show];
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 根据需求文档,要实现一个弹框选择器,本来想在网上找一个人家写好的拿来用用的,但是最近收到多方打击,决定动手写一个。...
    泥_叔阅读 2,965评论 0 8
  • 前言: 在开发会遇到众多自定义弹框的需求,如果是用到的情况非常多的话,就有有必要创建一个跟控制器,再根控制器中实现...
    Mr_Bob_阅读 3,373评论 1 8
  • 项目中需要自定义弹框就撸一个弹框, 做个记录。 xib 自定义一个View,添加在keyWindow上即可,blo...
    雪_晟阅读 1,486评论 0 7
  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 177,624评论 25 709
  • 课前 第一次上课前,老大在群里发了一段“洗脑”文字,甚合我意。自己最近一直有类似的想法,只是没有成型,所以看到这段...
    Mavis_Hou阅读 528评论 0 1

友情链接更多精彩内容