PopView

作者: 阿龍飛 | 来源:发表于2020-08-11 16:06 被阅读0次

    底部弹窗

    //
    //  MyPopView.h
    //  text
    //
    //  Created by 123 on 2020/8/11.
    //  Copyright © 2020 . All rights reserved.
    //
    
    #import <UIKit/UIKit.h>
    
    NS_ASSUME_NONNULL_BEGIN
    
    @interface MyPopView : UIView
    ///点背景关闭弹窗要加,否则可以去掉backgroundView,加是为了防止事件穿透,点击contentView不会关闭弹窗
    @property(nonatomic,strong)UIView *backgroundView;
    
    @property(nonatomic,strong)UIView *contentView;
    
    - (void)show;
    
    - (void)dismiss;
    
    @property (copy, nonatomic) void (^touchSureBtnClickBlock)(void);
    
    @property(nonatomic,strong)UIButton *sureBtn;
    
    @end
    
    NS_ASSUME_NONNULL_END
    
    
    //
    //  MyPopView.m
    //  text
    //
    //  Created by 123 on 2020/8/11.
    //  Copyright © 2020 . All rights reserved.
    //
    
    #import "MyPopView.h"
    #import "Masonry.h"
    
    @implementation MyPopView
    
    - (instancetype)initWithFrame:(CGRect)frame{
        self = [super initWithFrame:frame];
        if (self) {
            self.backgroundColor = [UIColor clearColor];
            self.tag = 12000;
            UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(dismiss)];
            [self.backgroundView addGestureRecognizer:tap];
            [self setUI];
            [self layout];
        }
        return self;
    }
    
    #pragma mark - 视图
    
    - (void)setUI{
        [self addSubview:self.backgroundView];
        [self addSubview:self.contentView];
        [self.contentView addSubview:self.sureBtn];
    }
    
    - (void)layout{
        
        [self.backgroundView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.bottom.top.equalTo(@0);
        }];
        
        [self.contentView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.bottom.equalTo(@0);
            make.height.equalTo(@400);
        }];
        
        [self.sureBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(@-20);
            make.width.equalTo(@60);
            make.height.equalTo(@30);
            make.top.equalTo(@20);
        }];
        
    }
    
    #pragma mark - LazyLoad
    
    - (UIView *)backgroundView{
        if (!_backgroundView) {
            _backgroundView = [[UIView alloc] init];
            _backgroundView.backgroundColor =[UIColor colorWithWhite:0 alpha:0.5];
        }
        return _backgroundView;
    }
    
    - (UIView *)contentView{
        if (!_contentView) {
            _contentView = [[UIView alloc] init];
            _contentView.backgroundColor = [UIColor whiteColor];
        }
        return _contentView;
    }
    
    - (UIButton *)sureBtn{
        if (!_sureBtn) {
            _sureBtn = [[UIButton alloc] init];
            [_sureBtn setTitle:@"确定" forState:(UIControlStateNormal)];
            [_sureBtn setTitleColor:UIColor.blueColor forState:(UIControlStateNormal)];
            [_sureBtn addTarget:self action:@selector(touchSureBtn) forControlEvents:(UIControlEventTouchUpInside)];
        }
        return _sureBtn;
    }
    
    #pragma mark - 交互
    
    -(void)show
    {
        UIView *view = [[UIApplication sharedApplication].keyWindow viewWithTag:12000];
        if (view) {
            [view removeFromSuperview];
        }
        [[UIApplication sharedApplication].keyWindow addSubview:self];
        
        [self mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.top.bottom.right.equalTo(@0);
        }];
        
        self.contenView.top = kScreenHeight;
        [UIView animateWithDuration:0.25 animations:^{
            self.contenView.top = kScreenHeight - 400;
            self.backgroundView.backgroundColor = [UIColor colorWithWhite:0 alpha:.5];
        } completion:^(BOOL finished) {
        }];
        
    }
    
    -(void)dismiss
    {
        [UIView animateWithDuration:0.25 animations:^{
            self.contenView.top = kScreenHeight;
            self.backgroundView.backgroundColor = [UIColor colorWithWhite:1 alpha:0];
        } completion:^(BOOL finished) {
            [self removeFromSuperview];
        }];
    }
    
    
    -(void)touchSureBtn{
        [self dismiss];
        !_touchSureBtnClickBlock ? : _touchSureBtnClickBlock();
    }
    
    @end
    
    
    

    中间弹窗

    -(void)show
    {
        UIView *view = [[UIApplication sharedApplication].keyWindow viewWithTag:22000];
        if (view) {
            [view removeFromSuperview];
        }
        [[UIApplication sharedApplication].keyWindow addSubview:self];
        
        self.subView.transform = CGAffineTransformMakeScale(1.20f, 1.20f);
        
        [UIView animateWithDuration:.25 animations:^{
            self.subView.transform = CGAffineTransformMakeScale(1.0f, 1.0f);
            self.backgroundView.backgroundColor = [UIColor colorWithWhite:0 alpha:.5];
        } completion:^(BOOL finished) {
        }];
        
    }
    
    -(void)dismiss
    {
        self.backgroundView.backgroundColor = [UIColor colorWithWhite:1 alpha:0];
        
        [UIView animateWithDuration:.25 animations:^{
        } completion:^(BOOL finished) {
            [self removeFromSuperview];
        }];
        
    }
    
    
    
    

    相关文章

      网友评论

          本文标题:PopView

          本文链接:https://www.haomeiwen.com/subject/klrydktx.html