#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;
+ (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