在项目中经常会用到这样的需求,我们一般都是自己去定制(如下图)
下面讲讲具体的封装,有需要的朋友可以直接拿走。
首先我们需要创建一个类 继承自UIView
在GiveUpPayView.h中:
#import <UIKit/UIKit.h>
@interface GiveUpPayView : UIView
//这个block是用来执行确定按钮点击以后 传执行的内容
@property (nonatomic, copy)void(^block)();
@end
在GiveUpPayView.m中:
#import "GiveUpPayView.h"
@interface GiveUpPayView ()
@property (weak, nonatomic) IBOutlet UIView *backGroundView;
@end
@implementation GiveUpPayView
- (instancetype)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
self = [[[NSBundle mainBundle] loadNibNamed:@"GiveUpPayView" owner:self options:nil] firstObject];
self.frame = CGRectMake(0, 0, frame.size.width, frame.size.height);
[self setView];
}
return self;
}
#pragma mark - 给view添加点击手势
- (void)setView{
UITapGestureRecognizer *backGroundViewClicked = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(backGroundViewClicked)];
backGroundViewClicked.numberOfTapsRequired = 1;
[self.backGroundView addGestureRecognizer:backGroundViewClicked];
}
- (void)backGroundViewClicked{
[self removeFromSuperview];
}
- (IBAction)yesButtonClicked:(id)sender {
self.block();
}
- (IBAction)cancalButtonClicked:(id)sender {
[self removeFromSuperview];
}
@end
新建一个XIB :
其中的灰色背景就是backView 将整个xib的class设置成刚才建立的GiveUpPayView 这样就完成了关联。
封装视图的使用:
GiveUpPayView *view = [[GiveUpPayView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT)];
view.block = ^(){
[self giveUpPay];
};
[self.view addSubview:view];