欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

iOS 自定义底部弹出视图

程序员文章站 2022-03-11 10:17:22
...

因为最近用到  百度下吧  乱糟糟  索性自己写,果然自己写才是王道!!!!!!

.h文件

//
//  XJALertView.h
//
//  Created by 王小胜 on 2018/6/4.
//  Copyright © 2018年 王小胜. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface XJALertView : UIView
- (void)showAlert;
- (void)dismissAlert;
@end

.m文件

//
//  XJALertView.m
//
//  Created by 王小胜 on 2018/6/4.
//  Copyright © 2018年 王小胜. All rights reserved.
//

#import "XJALertView.h"

@interface XJALertView ()
@property (nonatomic, strong) UIView *btnBack;
@end

@implementation XJALertView

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self addElement];
    }
    return self;
}

- (void)addElement {
    self.backgroundColor = [UIColor blackColor];
    self.alpha = 0.2;
    
    _btnBack = [[UIView alloc]init];
    _btnBack.backgroundColor = [UIColor greenColor];
}

- (void)showAlert {
    UIWindow *window = [[UIApplication sharedApplication] keyWindow];
    [window addSubview:self];
    [self addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(dismissAlert)]];
    //遮罩
    [UIView animateWithDuration:0.3 animations:^{
        self.alpha = 0.5;
    }];
    [window addSubview:_btnBack];
    
    [_btnBack mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self);
        make.right.equalTo(self);
        make.bottom.equalTo(self);
        make.height.mas_equalTo(150);
    }];

    self.btnBack.transform = CGAffineTransformMakeTranslation(0.01, Screen_W);
    [UIView animateWithDuration:0.3 animations:^{
        self.btnBack.transform = CGAffineTransformMakeTranslation(0.01, 0.01);
    }];
}

- (void)dismissAlert {
    [UIView animateWithDuration:0.3 animations:^{
        self.btnBack.transform = CGAffineTransformMakeTranslation(0.01, Screen_W);
        self.btnBack.alpha = 0.2;
        self.alpha = 0;
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
        [self.btnBack removeFromSuperview];
    }];
}

@end
iOS 自定义底部弹出视图