美文网首页
自定义弹出框快捷代码

自定义弹出框快捷代码

作者: 守护地中海的花 | 来源:发表于2019-05-21 11:26 被阅读0次
    • 效果图


      弹出框.png
    • .h中 给定背景视图的父类控制器
    @property(nonatomic,weak)BaseVC *parentVC;
    

    .m

    1.宏

    #define kSelfHeight (407*ADAPTER_WIDTH)
    

    2.分类属性

    @property(nonatomic,strong)UIControl *bgControl;
    @property(nonatomic,strong)UIButton *closeBtton;
    

    3.懒加载

    #pragma mark - lazy懒加载
    - (UIControl *)bgControl
    {
        if (!_bgControl)
        {
            UIControl *bgControl = [[UIControl alloc]initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT)];
            [sharedAppDelegate.window addSubview:bgControl];
            bgControl.backgroundColor = RGB(0, 0, 0, 0.2);
            [bgControl addTarget:self action:@selector(clickBgControl:) forControlEvents:UIControlEventTouchDown];
            _bgControl = bgControl;
        }
        return _bgControl;
    }
    - (UIButton *)closeBtton
    {
        if (!_closeBtton) {
            UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
            [self addSubview:button];
            [button setImage:[UIImage getPNGimageInBundleWithName:@"close_gray"] forState:UIControlStateNormal];;
            [button addTarget:self action:@selector(clickBgControl:) forControlEvents:UIControlEventTouchDown];
            button.frame = CGRectMake(self.width - 45*ADAPTER_WIDTH, 0, 45*ADAPTER_WIDTH, 43*ADAPTER_WIDTH);//10+15+20 18+15+10
            button.imageEdgeInsets = UIEdgeInsetsMake(18*ADAPTER_WIDTH, 10*ADAPTER_WIDTH, 10*ADAPTER_WIDTH, 20*ADAPTER_WIDTH);
            _closeBtton = button;
        }
        return _closeBtton;
    }
    

    5.核心代码

    - (instancetype)initWithFrame:(CGRect)frame
    {
        if (self = [super initWithFrame:frame]) {
            [self createProperty];
            [self createUI];
            [self startAnimation];
        }
        return self;
    }
    #pragma mark - setter方法
    - (void)setParentVC:(BaseVC *)parentVC
    {
        _parentVC = parentVC;
        [self.parentVC.view addSubview:self.bgControl];
    }
    #pragma mark - UI
    - (void)createProperty
    {
        
    }
    - (void)createUI
    {
        [self.bgControl addSubview:self];
        self.backgroundColor = [UIColor whiteColor];
        self.frame = CGRectMake(0, HEIGHT, WIDTH, kSelfHeight);
        
        [self closeBtton];
        
        [self addBeziPath];
    }
    - (void)addBeziPath
    {
        UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:self.bounds byRoundingCorners:UIRectCornerTopLeft | UIRectCornerTopRight cornerRadii:CGSizeMake(10, 0)];
        CAShapeLayer *maskLayer = [[CAShapeLayer alloc]init];
        maskLayer.frame = self.bounds;
        maskLayer.path = maskPath.CGPath;
        self.layer.mask = maskLayer;
    }
    #pragma mark - click点击事件
    - (void)clickBgControl:sender
    {
        [self endAnimation];
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.21 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [self removeAllSubviews];
            [self.bgControl removeAllSubviews];
            [self.bgControl removeFromSuperview];
        });
    }
    #pragma mark - 动画
    - (void)startAnimation
    {
        [UIView animateWithDuration:0.2 animations:^{
            self.frame = CGRectMake(0, HEIGHT-kSelfHeight, WIDTH, kSelfHeight);
        }];
    }
    - (void)endAnimation
    {
        [UIView animateWithDuration:0.2 animations:^{
            self.frame = CGRectMake(0, HEIGHT, WIDTH, kSelfHeight);
        }];
    }
    

    相关文章

      网友评论

          本文标题:自定义弹出框快捷代码

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