自定义弹出视图

2020-04-08  本文已影响0人  这个姑凉儿
示例.png

#import <UIKit/UIKit.h>

NS_ASSUME_NONNULL_BEGIN

@interface FeaturedAreaView : UIView
-(void)showFinishBlock:(void(^)(void))block;

@end

NS_ASSUME_NONNULL_END

#import "FeaturedAreaView.h"


@interface FeaturedAreaView ()
@property(nonatomic,copy)void(^block)(void);
@property(nonatomic,strong)UIView *bgView;
@end

@implementation FeaturedAreaView

-(void)showFinishBlock:(void(^)(void))block{

    FeaturedAreaView *presentV = [[FeaturedAreaView alloc]initWithFrame:CGRectMake(0, NAVIGATION_BAR_HEIGHT, KScreenWidth, KScreenHeight-NAVIGATION_BAR_HEIGHT)];
    presentV.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.3];
    presentV.block = block;
    [[UIApplication sharedApplication].keyWindow addSubview:presentV];
    

}

-(void)cancelBtnAction:(UIButton *)button{

    if (button.tag == 503) {
        [self removeFromSuperview];
    }else{
        [self removeFromSuperview];
        self.block ? self.block() : nil;
    }

}
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    UITouch *touch = [touches anyObject];
    if (touch.view == self) {
        [self removeFromSuperview];
//        [UIView animateWithDuration:0.25 animations:^{
//            self.bgView.transform = CGAffineTransformMakeScale(0.2, 0.2);
//        } completion:^(BOOL finished) {
//            [self removeFromSuperview];
//        }];
    }
}


- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        UIView *bgView = [[UIView alloc]init];
        bgView.backgroundColor = KWhiteColor;
        [self addSubview:bgView];
        self.bgView = bgView;
        ViewRadius(bgView, 10*SCALE);
        [bgView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.height.offset(162*SCALE);
            make.width.offset(292*SCALE);
            make.centerX.equalTo(self.mas_centerX);
            make.centerY.equalTo(self.mas_centerY).offset(-10*SCALE);
        }];
        
        UILabel *titleLab = [[UILabel alloc]init];
        titleLab.text = @"开通会员";
        titleLab.textColor = KThemColor_333333;
        titleLab.font = FONT(@"PingFangSC-Medium", 17);
        [bgView addSubview:titleLab];
        [titleLab mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.offset(31*SCALE);
            make.height.offset(16*SCALE);
            make.centerX.equalTo(bgView.mas_centerX);
        }];
        
        UILabel *titleLab1 = [[UILabel alloc]init];
        titleLab1.text = @"开通会员即可进入专区";
        titleLab1.textColor = KThemColor_666666;
        titleLab1.font = FONT(@"PingFangSC-Medium", 14);
        [bgView addSubview:titleLab1];
        [titleLab1 mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(titleLab.mas_bottom).offset(28*SCALE);
            make.centerX.equalTo(bgView.mas_centerX);
            make.height.offset(13*SCALE);
        }];
       
        UIView *lineV = [[UIView alloc]init];
        lineV.backgroundColor = KThemColor_F7F7FA;
        [bgView addSubview:lineV];
        [lineV mas_makeConstraints:^(MASConstraintMaker *make) {
            make.height.offset(1);
            make.left.right.offset(0);
            make.top.equalTo(titleLab1.mas_bottom).offset(28*SCALE);
        }];
        
        UIView *lineV1 = [[UIView alloc]init];
        lineV1.backgroundColor = KThemColor_F7F7FA;
        [bgView addSubview:lineV1];
        [lineV1 mas_makeConstraints:^(MASConstraintMaker *make) {
            make.width.offset(1);
            make.bottom.offset(0);
            make.centerX.equalTo(bgView.mas_centerX);
            make.top.equalTo(lineV.mas_bottom);
        }];
        
        UIButton *cancelBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [cancelBtn setTitle:@"放弃" forState:UIControlStateNormal];
        cancelBtn.tag = 503;
        cancelBtn.titleLabel.font = kfont16;
        [cancelBtn setTitleColor:KThemColor_333333 forState:UIControlStateNormal];
        [cancelBtn addTarget:self action:@selector(cancelBtnAction:) forControlEvents:UIControlEventTouchUpInside];
        [bgView addSubview:cancelBtn];
        [cancelBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(lineV1.mas_left);
            make.top.equalTo(lineV.mas_bottom);
            make.bottom.left.offset(0);

        }];
        
        
        UIButton *sureBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [sureBtn setTitle:@"立即开通" forState:UIControlStateNormal];
        sureBtn.tag = 504;
        sureBtn.titleLabel.font = kfont16;
        [sureBtn setTitleColor:KThemColor_F71A1F forState:UIControlStateNormal];
        [sureBtn addTarget:self action:@selector(cancelBtnAction:) forControlEvents:UIControlEventTouchUpInside];
        [bgView addSubview:sureBtn];
        [sureBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(lineV1.mas_right);
            make.top.equalTo(lineV.mas_bottom);
            make.bottom.right.offset(0);

        }];
    }
    return self;
}


@end

上一篇下一篇

猜你喜欢

热点阅读