前言
遍歷構(gòu)造器又稱工廠方法,可以把一個或多個控件封裝到一個類中,每次創(chuàng)建控件只需要調(diào)用方法就可以了
本次我所說的就是封裝一個根據(jù)所輸入的數(shù)組進(jìn)行自動創(chuàng)建提示框的類
效果圖:
上代碼
首先創(chuàng)建一個CustomAlertView的類,該類繼承自NSobject
然后在CustomAlertView.h中寫上方法聲明,因為是繼承自NSobject所以要手動導(dǎo)入UIKit框架
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
// 代理方法
@protocol CustomAlertViewDelegate <NSObject>
// 可選執(zhí)行方法
@optional
// 點擊按鈕下標(biāo)時傳遞參數(shù)
- (void)didSelectAlertButton:(NSString *)title;
@end
@interface CustomAlertView : NSObject
/** 單例 */
+ (CustomAlertView *)singleClass;
/** 快速創(chuàng)建提示框*/
- (UIView *)quickAlertViewWithArray:(NSArray *)array;
// 代理屬性
@property (assign, nonatomic)id<CustomAlertViewDelegate>delegate;
@end
然后在CustomAlertView.h.m中開始實現(xiàn)方法
#import "CustomAlertView.h"
/** 二進(jìn)制碼轉(zhuǎn)RGB */
#define UIColorFromRGBValue(rgbValue) [UIColor colorWithRed:((float)((rgbValue & 0xFF0000) >> 16))/255.0 green:((float)((rgbValue & 0xFF00) >> 8))/255.0 blue:((float)(rgbValue & 0xFF))/255.0 alpha:1.0]
@implementation CustomAlertView
/** 單例 */
+ (CustomAlertView *)singleClass{
static CustomAlertView *manager = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
manager = [[CustomAlertView alloc] init];
});
return manager;
}
/** 提示view */
- (UIView *)quickAlertViewWithArray:(NSArray *)array{
CGFloat buttonH = 61;
CGFloat buttonW = 250;
// 通過數(shù)組長度創(chuàng)建view的高
UIView *alert = [[UIView alloc] initWithFrame:CGRectMake(0, 0,buttonW, array.count * buttonH)];
for (int i = 0; i < array.count;i++) {
// 因為有一條分割線 所以最下面是一層view
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, i*buttonH, buttonW, buttonH)];
view.backgroundColor = [UIColor whiteColor];
// 創(chuàng)建button
UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
button.frame = CGRectMake(0, 0, buttonW, buttonH);
[button setTitle:array[i] forState:(UIControlStateNormal)];
// 所有button都關(guān)聯(lián)一個點擊方法,通過按鈕上的title做區(qū)分
[button addTarget:self action:@selector(alertAction:) forControlEvents:(UIControlEventTouchUpInside)];
[view addSubview:button];
// 這里可以根據(jù)傳值改變狀態(tài)
if ([array[i] isEqualToString:@"取消"]) {
button.tintColor = [UIColor whiteColor];
// 綠色背景
view.backgroundColor = UIColorFromRGBValue(0x82DFB0);
}else{
button.tintColor = UIColorFromRGBValue(0x333333);
// 分割線
// 如果不是最后一行
UIView *lineView = [[UIView alloc] initWithFrame:CGRectMake(0, 60, buttonW, 1)];
lineView.backgroundColor = UIColorFromRGBValue(0xefefef);
[view addSubview:lineView];
}
[alert addSubview:view];
}
return alert;
}
/** button點擊事件,通知代理執(zhí)行代理方法 */
- (void)alertAction:(UIButton *)button{
[_delegate didSelectAlertButton:button.titleLabel.text];
}
@end
ViewController中調(diào)用
#import "ViewController.h"
#import "CustomAlertView.h"
@interface ViewController ()<CustomAlertViewDelegate>
/** 提示框 */
@property (strong, nonatomic) UIView *alertView;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// self.view.backgroundColor = [UIColor lightGrayColor];
// 將提示頁面加入到view上
[self.view addSubview:self.alertView];
}
// 這個是stroyBoard里創(chuàng)建的button
- (IBAction)alertAction:(UIButton *)sender {
// UIView動畫
[UIView animateWithDuration:0.1 animations:^{
self.alertView.alpha = 1;
self.alertView.hidden = NO;
}];
}
/** 提示框懶加載 */
- (UIView *)alertView{
if (!_alertView) {
// 這里還可以把alerView創(chuàng)建到一個蒙版上,直接進(jìn)行操作蒙版的透明度隱藏來展示動畫,也可以避免點擊框外的其他控件,就不在這里細(xì)寫了
// 賦值
_alertView = [[CustomAlertView singleClass]
// 傳入數(shù)組
quickAlertViewWithArray:@[@"確定",@"測試A",@"測試B",@"取消"]
];
// 設(shè)定中心,如果需要適配請layoutIfNeed
_alertView.center = self.view.center;
// 切圓角
_alertView.layer.masksToBounds = YES;
_alertView.layer.cornerRadius = 10;
// 初始狀態(tài)為隱藏,透明度為0
_alertView.hidden = YES;
_alertView.alpha = 0.0;
// 設(shè)置代理
[CustomAlertView singleClass].delegate = self;
}
return _alertView;
}
// 代理方法傳值
- (void)didSelectAlertButton:(NSString *)title{
[UIView animateWithDuration:0.1 animations:^{
self.alertView.alpha = 0;
} completion:^(BOOL finished) {
// 如果直接在動畫里隱藏不會出現(xiàn)動畫效果,所以要在動畫結(jié)束之后進(jìn)行隱藏
self.alertView.hidden = YES;
}];
NSLog(@"%@",title);
}
@end