我們在做開發(fā)時經(jīng)常會自定義一些view,并且如果如果該view比較復(fù)雜或者經(jīng)常被調(diào)用到,我們就可以將該view單獨(dú)放到一個類中,方便調(diào)用或者修改。筆者前段時間封裝了一個自定義的彈出框,功能簡單,借此跟大家交流一下封裝自定義的視圖view的方法與心得,僅僅是為了分享學(xué)習(xí),并不建議直接在項(xiàng)目中使用,如果有錯誤或者不足之處,歡迎大家前來批評指正。
先上效果圖:
在ViewController.m中 導(dǎo)入"SZKAlterView.h"頭文件
調(diào)用代碼:
SZKAlterView *lll=[SZKAlterView alterViewWithTitle:@"簡書號:iOS_凱" content:@"感謝各位朋友的關(guān)注與鼓勵" cancel:@"取消" sure:@"確定" cancelBtClcik:^{
//取消按鈕點(diǎn)擊事件
NSLog(@"取消");
} sureBtClcik:^{
//確定按鈕點(diǎn)擊事件
NSLog(@"確定");
}];
[self.view addSubview:lll];
需要注意的是:
SZKAlterView直接封裝成了類方法,并且可以手動填寫標(biāo)題,內(nèi)容,取消按鈕的內(nèi)容,確定按鈕的內(nèi)容,還有一個需要注意的就是筆者將取消按鈕和確定按鈕的點(diǎn)擊事件利用block傳遞出來了,具體實(shí)現(xiàn)方法,下面講到。
實(shí)現(xiàn)方法:
SZKAlterView.h 中
#import <UIKit/UIKit.h>
//取消按鈕點(diǎn)擊事件
typedef void(^cancelBlock)();
//確定按鈕點(diǎn)擊事件
typedef void(^sureBlock)();
@interface SZKAlterView : UIView
@property(nonatomic,copy)cancelBlock cancel_block;
@property(nonatomic,copy)sureBlock sure_block;
/**
* 簡書號:iOS_凱 http://www.lxweimin.com/users/86b0ddc92021/latest_articles
*
* @param title 標(biāo)題
* @param content 內(nèi)容
* @param cancel 取消按鈕內(nèi)容
* @param sure 確定按鈕內(nèi)容
* @param cancelBlock 取消按鈕點(diǎn)擊事件
* @param sureBlock 確定按鈕點(diǎn)擊事件
*
* @return SZKAlterView
*/
+(instancetype)alterViewWithTitle:(NSString *)title
content:(NSString *)content
cancel:(NSString *)cancel
sure:(NSString *)sure
cancelBtClcik:(cancelBlock)cancelBlock
sureBtClcik:(sureBlock)sureBlock;
@end
SZKAlterView.m中
-(instancetype)initWithFrame:(CGRect)frame
{
self=[super initWithFrame:frame];
if (self) {
//標(biāo)題
_titleLb=[[UILabel alloc]initWithFrame:CGRectMake(0, 0, self.bounds.size.width, 50)];
_titleLb.textAlignment=NSTextAlignmentCenter;
_titleLb.textColor=[UIColor blackColor];
[self addSubview:_titleLb];
//內(nèi)容
_contentLb=[[UILabel alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(_titleLb.frame), self.bounds.size.width, 50)];
_contentLb.textAlignment=NSTextAlignmentCenter;
_contentLb.textColor=[UIColor redColor];
[self addSubview:_contentLb];
//取消按鈕
_cancelBt=[[UIButton alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(_contentLb.frame), self.bounds.size.width/2, 50)];
_cancelBt.layer.borderColor=[UIColor grayColor].CGColor;
_cancelBt.layer.borderWidth=0.5;
[_cancelBt setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[_cancelBt addTarget:self action:@selector(cancelBtClick) forControlEvents:UIControlEventTouchUpInside];
[self addSubview:_cancelBt];
//確定按鈕
_sureBt=[[UIButton alloc]initWithFrame:CGRectMake(self.bounds.size.width/2, CGRectGetMaxY(_contentLb.frame), self.bounds.size.width/2, 50)];
_sureBt.layer.borderColor=[UIColor grayColor].CGColor;
_sureBt.layer.borderWidth=0.5;
[_sureBt setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[_sureBt addTarget:self action:@selector(sureBtClick) forControlEvents:UIControlEventTouchUpInside];
[self addSubview:_sureBt];
}
return self;
}
#pragma mark----實(shí)現(xiàn)類方法
+(instancetype)alterViewWithTitle:(NSString *)title
content:(NSString *)content
cancel:(NSString *)cancel
sure:(NSString *)sure
cancelBtClcik:(cancelBlock)cancelBlock
sureBtClcik:(sureBlock)sureBlock;
{
SZKAlterView *alterView=[[SZKAlterView alloc]initWithFrame:CGRectMake(0, 0, 250, 150)];
alterView.backgroundColor=[UIColor whiteColor];
alterView.center=CGPointMake(WIDTH/2, HEIGHT/2);
alterView.layer.cornerRadius=5;
alterView.layer.masksToBounds=YES;
alterView.title=title;
alterView.content=content;
alterView.cancel=cancel;
alterView.sure=sure;
alterView.cancel_block=cancelBlock;
alterView.sure_block=sureBlock;
return alterView;
}
#pragma mark--給屬性重新賦值
-(void)setTitle:(NSString *)title
{
_titleLb.text=title;
}
-(void)setContent:(NSString *)content
{
_contentLb.text=content;
}
-(void)setSure:(NSString *)sure
{
[_sureBt setTitle:sure forState:UIControlStateNormal];
}
-(void)setCancel:(NSString *)cancel
{
[_cancelBt setTitle:cancel forState:UIControlStateNormal];
}
#pragma mark----取消按鈕點(diǎn)擊事件
-(void)cancelBtClick
{
[self removeFromSuperview];
self.cancel_block();
}
#pragma mark----確定按鈕點(diǎn)擊事件
-(void)sureBtClick
{
[self removeFromSuperview];
self.sure_block();
}
需要注意的是:
因?yàn)楣P者將該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將點(diǎn)擊事件傳遞到外部類中。
大致的實(shí)現(xiàn)方法寫到這里,如果有不明白的可以聯(lián)系筆者,筆者有問必答,如果想要下載完整demo請點(diǎn)擊:
SZKAlterView鏈接:https://github.com/18811314750/SZKAlterView
如果覺得筆者寫的不錯,可以star下喲。
如果各位觀眾老爺還沒有看夠,還有一篇關(guān)于無限輪播圖的封裝
iOS開發(fā)-兩句代碼快速實(shí)現(xiàn)無限輪播圖(基于ScrollView封裝)
http://www.lxweimin.com/p/d240bd977689
筆者的其他文章:
iOS在線音樂播放SZKAVPlayer(基于AVPlayer的封裝)
http://www.lxweimin.com/p/4e0ac2898de0
iOS開發(fā)-一句代碼調(diào)用實(shí)現(xiàn)網(wǎng)絡(luò)的監(jiān)測功能(基于AFNetworkReachabilityManager的封裝)
http://www.lxweimin.com/p/b901ad0c1d81
如果有不足或者錯誤的地方還望各位讀者批評指正,可以評論留言,筆者收到后第一時間回復(fù)。
QQ/微信:790057066 。
簡書號:iOS_凱:http://www.lxweimin.com/users/86b0ddc92021/latest_articles
GitHub個人主頁:https://github.com/18811314750
歡迎各位前來查看,star,感謝各位的閱讀。