自定义弹框

#import <UIKit/UIKit.h>
//取消按钮点击事件
typedef void(^cancelBlock)();

//确定按钮点击事件
typedef void(^sureBlock)();

@protocol MyAlterViewDelegate <NSObject>

@optional
- (void)clickCancelButton;
- (void)clickContinueButton;
@end

@interface MyAlterView : UIControl
@property (nonatomic ,copy) NSString *title;
@property (nonatomic ,copy) NSString *content;
@property (nonatomic ,copy) NSString *sure;
@property (nonatomic ,copy) NSString *cancel;

@property(nonatomic,assign)cancelBlock cancel_block;
@property(nonatomic,assign)sureBlock sure_block;
@property (nonatomic,weak) id<MyAlterViewDelegate>delegate;
/**
 *  @param title       标题
 *  @param content     内容
 *  @param cancel      取消按钮内容
 *  @param sure        确定按钮内容
 *  @param cancelBlock 取消按钮点击事件
 *  @param sureBlock   确定按钮点击事件
 *
 *  @return AlterView
 */
+ (instancetype)alterViewWithFrame:(CGRect)rect content:(NSString *)content cancel:(NSString *)cancel sure:(NSString *)sure cancelBtClcik:(cancelBlock)cancelBlock sureBtClcik:(sureBlock)sureBlock;
@end
#import "MyAlterView.h"

@interface MyAlterView ()
@property (nonatomic,strong) UILabel *titleLb;
@property (nonatomic,strong) UILabel *contentLb;
@property (nonatomic,strong) UIButton *cancelBt;
@property (nonatomic,strong) UIButton *sureBt;
@end

@implementation MyAlterView
- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        //内容
        self.contentLb = [[UILabel alloc]initWithFrame:CGRectMake(15, 0, self.bounds.size.width - 30, self.height - 81)];
        self.contentLb.textAlignment = NSTextAlignmentLeft;
        self.contentLb.numberOfLines = 0;
        self.contentLb.textColor = [UIColor darkGrayColor];
        [self addSubview:_contentLb];

        //分割线
        UIView *separateLine = [[UIView alloc]initWithFrame:CGRectMake(0, self.height - 80, self.width, 1)];
        separateLine.backgroundColor = ColorWithRGB(222, 222, 222, 1);
        [self addSubview:separateLine];

        //取消按钮
        self.cancelBt = [[UIButton alloc]initWithFrame:CGRectMake(15, self.height - 65, self.bounds.size.width/2 - 30, 50)];
        self.cancelBt.layer.borderColor = [UIColor grayColor].CGColor;
        self.cancelBt.layer.borderWidth = 0.5;
        self.cancelBt.layer.cornerRadius = 8.0f;
        self.cancelBt.layer.masksToBounds = YES;
        [self.cancelBt setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [self.cancelBt setImage:[UIImage imageNamed:@"tooltip_button_no"] forState:UIControlStateNormal];
        [self.cancelBt.imageView setContentMode:UIViewContentModeScaleAspectFill];
        [self.cancelBt addTarget:self action:@selector(cancelBtClick) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:_cancelBt];

        //确定按钮
        CGFloat margeX = self.width * 0.5 - CGRectGetMaxX(_cancelBt.frame);
        self.sureBt = [[UIButton alloc]initWithFrame:CGRectMake(CGRectGetMaxX(_cancelBt.frame) + 2 * margeX, self.height - 65, self.bounds.size.width/2 - 30, 50)];
        self.sureBt.layer.borderColor = [UIColor grayColor].CGColor;
        self.sureBt.layer.borderWidth = 0.5;
        self.sureBt.layer.cornerRadius = 8.0f;
        self.sureBt.layer.masksToBounds = YES;
        [self.sureBt setImage:[UIImage imageNamed:@"tooltip_button_yes_z"] forState:UIControlStateNormal];
        [self.sureBt.imageView setContentMode:UIViewContentModeScaleAspectFill];
        [self.sureBt setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [self.sureBt addTarget:self action:@selector(sureBtClick) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:_sureBt];
    }
    return self;
}

#pragma mark----实现类方法
+ (instancetype)alterViewWithFrame:(CGRect)rect content:(NSString *)content cancel:(NSString *)cancel sure:(NSString *)sure cancelBtClcik:(cancelBlock)cancelBlock sureBtClcik:(sureBlock)sureBlock {
    MyAlterView *alterView = [[MyAlterView alloc]initWithFrame:rect];
    alterView.backgroundColor = [UIColor whiteColor];
    alterView.center = CGPointMake(alterView.width * 0.5, alterView.height * 0.5);
    alterView.layer.cornerRadius = 5;
    alterView.layer.masksToBounds = YES;
    alterView.content = content;
    UIButton *cancelBt = alterView.subviews[2];
    [cancelBt setImage:[UIImage imageNamed:cancel] forState:UIControlStateNormal];
    UIButton *sureBt = alterView.subviews[3];
    [sureBt setImage:[UIImage imageNamed:sure] forState:UIControlStateNormal];

    alterView.cancel_block = cancelBlock;
    alterView.sure_block = sureBlock;
    return alterView;
}

#pragma mark--给属性重新赋值
- (void)setTitle:(NSString *)title {
    self.titleLb.text = title;
}

- (void)setContent:(NSString *)content {
    self.contentLb.text = content;
}

- (void)setSure:(NSString *)sure {
    [self.sureBt setTitle:sure forState:UIControlStateNormal];
}

- (void)setCancel:(NSString *)cancel {
    [self.cancelBt setTitle:cancel forState:UIControlStateNormal];
}

#pragma mark----取消按钮点击事件
- (void)cancelBtClick {
    [self removeFromSuperview];
    if ([self.delegate respondsToSelector:@selector(clickCancelButton)]) {
        [self.delegate clickCancelButton];
    }
}

#pragma mark----确定按钮点击事件
- (void)sureBtClick {
    [self removeFromSuperview];
    if ([self.delegate respondsToSelector:@selector(clickContinueButton)]) {
        [self.delegate clickContinueButton];
    }
}
@end
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值