在專案中經常會用到這樣的需求,我們一般都是自己去定製(如下圖)
下面講講具體的封裝,有需要的朋友可以直接拿走。
首先我們需要建立一個類 繼承自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];
複製程式碼