前言:我們開發(fā)的時候經(jīng)常會自定義一些view,以備后用,今天從簡書上看到一篇用view封裝彈框的文章梯澜,就拿來學(xué)習(xí)學(xué)習(xí)姻锁。封裝一下枕赵,以備學(xué)習(xí),感謝那位作者位隶。
效果圖:
目錄:
1.簡單的思路
2.代碼示例
3.使用方法
4.學(xué)習(xí)要點
5.思路總結(jié)
6.學(xué)習(xí)體會
7.Demo地址以及原作者地址
-
簡單的思路:
1.創(chuàng)建一個繼承于UIView的類
2.設(shè)置類所需的子控件
3.在創(chuàng)建一個類方法拷窜,供于外部調(diào)去,屬性傳值涧黄,以及block回調(diào)篮昧。
利用block將點擊事件傳遞到外部類中。 - 代碼示例:
ZYAlterView.h
#import <UIKit/UIKit.h>
/**取消按鈕點擊事件*/
typedef void (^cancelBlock)();
/**確定按鈕點擊事件*/
typedef void (^sureBlock)();
@interface ZYAlterView : UIView
/**失敗回調(diào)*/
@property(copy,nonatomic) cancelBlock cancel_block;
/**成功回調(diào)*/
@property(copy,nonatomic) sureBlock sure_block;
/**
*
* @param title 標(biāo)題
* @param content 內(nèi)容
* @param cancel 取消按鈕內(nèi)容
* @param sure 確定按鈕內(nèi)容
* @param cancelBlock 取消按鈕點擊事件
* @param sureBlock 確定按鈕點擊事件
*
* @return ZYAlterView
*/
+(instancetype)alterViewWithTitle:(NSString *)title
content:(NSString *)content
cancel:(NSString *)cancel
sure:(NSString *)sure
cancel_Block_Clcik:(cancelBlock)cancelBlock
sure_Block_Click:(sureBlock)sureBlock;
@end
解析:
- 首先笋妥,在.h文件懊昨,創(chuàng)建兩個block的別名(成功和失敗的回調(diào)),用于把按鈕的點擊事件傳到外界春宣,有良好的溝通酵颁。
- 其次,創(chuàng)建一個類方法-要把你封裝的AlterView,所需的屬性月帝,從外界傳進(jìn)來躏惋,在通過block進(jìn)行回調(diào)。也就是嚷辅,從外界傳進(jìn)來的屬性簿姨,需要我們從外界調(diào)用此方法,并設(shè)置AlterView的屬性值簸搞。
ZYAlterView.m
#import "ZYAlterView.h"
#define KSCREEN_W [UIScreen mainScreen].bounds.size.width
#define KSCREEN_H [UIScreen mainScreen].bounds.size.height
@interface ZYAlterView ()
/**標(biāo)題lable*/
@property(strong,nonatomic)UILabel *titleLab;
/**內(nèi)容lable*/
@property(strong,nonatomic)UILabel *contentLab;
/**取消Btn*/
@property(strong,nonatomic)UIButton *cancelBtn;
/**確定Btn*/
@property(strong,nonatomic)UIButton *sureBtn;
/**標(biāo)題*/
@property(copy,nonatomic)NSString *title;
/**內(nèi)容*/
@property(copy,nonatomic)NSString *content;
/**取消*/
@property(copy,nonatomic)NSString *cancelTitle;
/**確定*/
@property(copy,nonatomic)NSString *sureTitle;
@end
@implementation ZYAlterView
#pragma mark - 初始化(添加控件)
- (instancetype)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
>
// 標(biāo)題
_titleLab = [[UILabel alloc]initWithFrame:(CGRect){0,0,self.bounds.size.width,50}];
_titleLab.textAlignment = NSTextAlignmentCenter;
_titleLab.textColor = [UIColor blackColor];
[self addSubview:_titleLab];
>
// 內(nèi)容
_contentLab = [[UILabel alloc]initWithFrame:(CGRect){0,CGRectGetMaxY(_titleLab.frame),self.bounds.size.width,50}];
_contentLab.textAlignment = NSTextAlignmentCenter;
_contentLab.textColor = [UIColor redColor];
[self addSubview:_contentLab];
>
// 取消按鈕
_cancelBtn = [[UIButton alloc]initWithFrame:(CGRect){0,CGRectGetMaxY(_contentLab.frame),self.bounds.size.width/2,50}];
_cancelBtn.layer.borderColor = [UIColor grayColor].CGColor;
_cancelBtn.layer.borderWidth = 0.5;
[_cancelBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[_cancelBtn addTarget:self action:@selector(cancelBtClick) forControlEvents:UIControlEventTouchUpInside];
[self addSubview:_cancelBtn];
>
// 確定按鈕
_sureBtn = [[UIButton alloc]initWithFrame:(CGRect){self.bounds.size.width/2,CGRectGetMaxY(_contentLab.frame),self.bounds.size.width/2,50}];
_sureBtn.layer.borderColor = [UIColor grayColor].CGColor;
_sureBtn.layer.borderWidth = 0.5;
[_sureBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[_sureBtn addTarget:self action:@selector(sureBtClick) forControlEvents:UIControlEventTouchUpInside];
[self addSubview:_sureBtn];
}
return self;
}
#pragma mark----實現(xiàn)類方法
+(instancetype)alterViewWithTitle:(NSString *)title
content:(NSString *)content
cancel:(NSString *)cancel
sure:(NSString *)sure
cancel_Block_Clcik:(cancelBlock)cancelBlock
sure_Block_Click:(sureBlock)sureBlock
{
ZYAlterView *alterView = [[ZYAlterView alloc]initWithFrame:(CGRect){0,0,250,150}];
alterView.backgroundColor = [UIColor whiteColor];
alterView.center = (CGPoint){KSCREEN_W/2,KSCREEN_H/2};
alterView.layer.cornerRadius = 5;
alterView.layer.masksToBounds = YES;
alterView.title=title;
alterView.content=content;
alterView.cancelTitle=cancel;
alterView.sureTitle=sure;
alterView.cancel_block=cancelBlock;
alterView.sure_block=sureBlock;
return alterView;
}
#pragma mark--給屬性重新賦值
-(void)setTitle:(NSString *)title
{
_titleLab.text = title;
}
-(void)setContent:(NSString *)content
{
_contentLab.text = content;
}
-(void)setSureTitle:(NSString *)sureTitle
{
[_sureBtn setTitle:sureTitle forState:UIControlStateNormal];
}
- (void)setCancelTitle:(NSString *)cancelTitle
{
[_cancelBtn setTitle:cancelTitle forState:UIControlStateNormal];
}
#pragma mark----取消按鈕點擊事件
-(void)cancelBtClick
{
[self removeFromSuperview];
self.cancel_block();
}
#pragma mark----確定按鈕點擊事件
-(void)sureBtClick
{
[self removeFromSuperview];
self.sure_block();
}
@end
解析:
通過init初始化控件扁位,在通過自定義的類方法,創(chuàng)建自定義的類攘乒,對其屬性進(jìn)行賦值贤牛,但是,此時则酝,控件沒有值。其實闰集,為其屬性賦值沽讹,就是寫其屬性的set方法,通過set方法武鲁,給本類上的控件賦值爽雄。如上代碼。
- 使用方法:
在ViewController.m中 導(dǎo)入"#import "ZYAlterView.h""頭文件調(diào)用代碼:
ZYAlterView *alterView = [ZYAlterView alterViewWithTitle:@"自定義彈框?qū)W習(xí)" content:@"虛心學(xué)習(xí)沐鼠,真愛生命挚瘟!" cancel:@"取消" sure:@"確定" cancel_Block_Clcik:^{
//取消按鈕點擊事件
NSLog(@"取消");
} sure_Block_Click:^{
//確定按鈕點擊事件
NSLog(@"確定");
}];
ZYAlterView直接封裝成了類方法叹谁,并且可以手動填寫標(biāo)題,內(nèi)容乘盖,取消按鈕的內(nèi)容焰檩,確定按鈕的內(nèi)容,還有一個需要注意的就是筆者將取消按鈕和確定按鈕的點擊事件利用block傳遞出來了,這點值得我們學(xué)習(xí)订框。
-
學(xué)習(xí)要點:
1.取消按鈕和確定按鈕的點擊事件利用block傳遞出來
2.set方法傳值
3.自定義UIView思想的學(xué)習(xí) - 思路總結(jié):
因為筆者將該view封裝成了類方法析苫,所以會調(diào)用這個類時會先執(zhí)行
+(instancetype)alterViewWithTitle:(NSString *)title...
方法,緊接著執(zhí)行
-(instancetype)initWithFrame:(CGRect)frame
方法穿扳,然后會繼續(xù)執(zhí)行alterView.title=title;屬性賦值的方法衩侥,但是這時界面展示不出來內(nèi)容的,需要在set方法中重新給相關(guān)內(nèi)容賦值才會展示出來矛物,最后倆個函數(shù)就是利用block將點擊事件傳遞到外部類中茫死。
-
學(xué)習(xí)體會:在這里,學(xué)習(xí)的并不是作者怎么用UIView封裝AlterView,主要是履羞,學(xué)習(xí)用UIView學(xué)習(xí)封裝的一種思想璧榄。培養(yǎng)自己的思維能力。
Demo地址以及原作者地址: - gitthb地址:
https://github.com/RenZhengYang/ZYCustomAlterView/tree/master - 原作者地址:
http://www.reibang.com/p/de2ecfd770c2
基本上與作者匹配的大概吧雹,稍微改了一下骨杂,感謝原作者提供文章學(xué)習(xí)