首先来看看实现的效果图
下面话不多说,以下是实现的示例代码
#import <uikit/uikit.h>
typedef void(^alertresult)(nsinteger index);
@interface xlalertview : uiview
@property (nonatomic,copy) alertresult resultindex;
- (instancetype)initwithtitle:(nsstring *)title message:(nsstring *)message surebtn:(nsstring *)suretitle canclebtn:(nsstring *)cancletitle;
- (void)showxlalertview;
@end
#import "xlalertview.h"
///alertview 宽
#define alertw 280
///各个栏目之间的距离
#define xlspace 10.0
@interface xlalertview()
//弹窗
@property (nonatomic,retain) uiview *alertview;
//title
@property (nonatomic,retain) uilabel *titlelbl;
//内容
@property (nonatomic,retain) uilabel *msglbl;
//确认按钮
@property (nonatomic,retain) uibutton *surebtn;
//取消按钮
@property (nonatomic,retain) uibutton *canclebtn;
//横线线
@property (nonatomic,retain) uiview *lineview;
//竖线
@property (nonatomic,retain) uiview *verlineview;
@end
@implementation xlalertview
- (instancetype)initwithtitle:(nsstring *)title message:(nsstring *)message surebtn:(nsstring *)suretitle canclebtn:(nsstring *)cancletitle
{
if (self == [super init]) {
self.frame = [uiscreen mainscreen].bounds;
self.backgroundcolor = [uicolor colorwithwhite:0.8 alpha:0.6];
self.alertview = [[uiview alloc] init];
self.alertview.backgroundcolor = [uicolor whitecolor];
self.alertview.layer.cornerradius = 5.0;
self.alertview.frame = cgrectmake(0, 0, alertw, 100);
self.alertview.layer.position = self.center;
if (title) {
self.titlelbl = [self getadaptivelable:cgrectmake(2*xlspace, 2*xlspace, alertw-4*xlspace, 20) andtext:title andistitle:yes];
self.titlelbl.textalignment = nstextalignmentcenter;
[self.alertview addsubview:self.titlelbl];
cgfloat titlew = self.titlelbl.bounds.size.width;
cgfloat titleh = self.titlelbl.bounds.size.height;
self.titlelbl.frame = cgrectmake((alertw-titlew)/2, 2*xlspace, titlew, titleh);
}
if (message) {
self.msglbl = [self getadaptivelable:cgrectmake(xlspace, cgrectgetmaxy(self.titlelbl.frame)+xlspace, alertw-2*xlspace, 20) andtext:message andistitle:no];
self.msglbl.textalignment = nstextalignmentcenter;
[self.alertview addsubview:self.msglbl];
cgfloat msgw = self.msglbl.bounds.size.width;
cgfloat msgh = self.msglbl.bounds.size.height;
self.msglbl.frame = self.titlelbl?cgrectmake((alertw-msgw)/2, cgrectgetmaxy(self.titlelbl.frame)+xlspace, msgw, msgh):cgrectmake((alertw-msgw)/2, 2*xlspace, msgw, msgh);
}
self.lineview = [[uiview alloc] init];
self.lineview.frame = self.msglbl?cgrectmake(0, cgrectgetmaxy(self.msglbl.frame)+2*xlspace, alertw, 1):cgrectmake(0, cgrectgetmaxy(self.titlelbl.frame)+2*xlspace, alertw, 1);
self.lineview.backgroundcolor = [uicolor colorwithwhite:0.8 alpha:0.6];
[self.alertview addsubview:self.lineview];
//两个按钮
if (cancletitle && suretitle) {
self.canclebtn = [uibutton buttonwithtype:uibuttontypesystem];
self.canclebtn.frame = cgrectmake(0, cgrectgetmaxy(self.lineview.frame), (alertw-1)/2, 40);
[self.canclebtn setbackgroundimage:[self imagewithcolor:[uicolor colorwithred:255/255.0 green:255/255.0 blue:255/255.0 alpha:0.2]] forstate:uicontrolstatenormal];
[self.canclebtn setbackgroundimage:[self imagewithcolor:[uicolor colorwithred:255/255.0 green:255/255.0 blue:255/255.0 alpha:0.2]] forstate:uicontrolstateselected];
[self.canclebtn settitle:cancletitle forstate:uicontrolstatenormal];
//[self.canclebtn settitlecolor:[uicolor graycolor] forstate:uicontrolstatenormal];
self.canclebtn.tag = 1;
[self.canclebtn addtarget:self action:@selector(buttonevent:) forcontrolevents:uicontroleventtouchupinside];
uibezierpath *maskpath = [uibezierpath bezierpathwithroundedrect:self.canclebtn.bounds byroundingcorners:uirectcornerbottomleft cornerradii:cgsizemake(5.0, 5.0)];
cashapelayer *masklayer = [[cashapelayer alloc] init];
masklayer.frame = self.canclebtn.bounds;
masklayer.path = maskpath.cgpath;
self.canclebtn.layer.mask = masklayer;
[self.alertview addsubview:self.canclebtn];
}
if (cancletitle && suretitle) {
self.verlineview = [[uiview alloc] init];
self.verlineview.frame = cgrectmake(cgrectgetmaxx(self.canclebtn.frame), cgrectgetmaxy(self.lineview.frame), 1, 40);
self.verlineview.backgroundcolor = [uicolor colorwithwhite:0.8 alpha:0.6];
[self.alertview addsubview:self.verlineview];
}
if(suretitle && cancletitle){
self.surebtn = [uibutton buttonwithtype:uibuttontypesystem];
self.surebtn.frame = cgrectmake(cgrectgetmaxx(self.verlineview.frame), cgrectgetmaxy(self.lineview.frame), (alertw-1)/2+1, 40);
[self.surebtn setbackgroundimage:[self imagewithcolor:[uicolor colorwithred:255/255.0 green:255/255.0 blue:255/255.0 alpha:0.2]] forstate:uicontrolstatenormal];
[self.surebtn setbackgroundimage:[self imagewithcolor:[uicolor colorwithred:255/255.0 green:255/255.0 blue:255/255.0 alpha:0.2]] forstate:uicontrolstateselected];
[self.surebtn settitle:suretitle forstate:uicontrolstatenormal];
//[self.surebtn settitlecolor:[uicolor whitecolor] forstate:uicontrolstatenormal];
self.surebtn.tag = 2;
[self.surebtn addtarget:self action:@selector(buttonevent:) forcontrolevents:uicontroleventtouchupinside];
uibezierpath *maskpath = [uibezierpath bezierpathwithroundedrect:self.surebtn.bounds byroundingcorners:uirectcornerbottomright cornerradii:cgsizemake(5.0, 5.0)];
cashapelayer *masklayer = [[cashapelayer alloc] init];
masklayer.frame = self.surebtn.bounds;
masklayer.path = maskpath.cgpath;
self.surebtn.layer.mask = masklayer;
[self.alertview addsubview:self.surebtn];
}
//只有取消按钮
if (cancletitle && !suretitle) {
self.canclebtn = [uibutton buttonwithtype:uibuttontypesystem];
self.canclebtn.frame = cgrectmake(0, cgrectgetmaxy(self.lineview.frame), alertw, 40);
[self.canclebtn setbackgroundimage:[self imagewithcolor:[uicolor colorwithred:255/255.0 green:255/255.0 blue:255/255.0 alpha:0.2]] forstate:uicontrolstatenormal];
[self.canclebtn setbackgroundimage:[self imagewithcolor:[uicolor colorwithred:255/255.0 green:255/255.0 blue:255/255.0 alpha:0.2]] forstate:uicontrolstateselected];
[self.canclebtn settitle:cancletitle forstate:uicontrolstatenormal];
//[self.canclebtn settitlecolor:[uicolor graycolor] forstate:uicontrolstatenormal];
self.canclebtn.tag = 1;
[self.canclebtn addtarget:self action:@selector(buttonevent:) forcontrolevents:uicontroleventtouchupinside];
uibezierpath *maskpath = [uibezierpath bezierpathwithroundedrect:self.canclebtn.bounds byroundingcorners:uirectcornerbottomleft | uirectcornerbottomright cornerradii:cgsizemake(5.0, 5.0)];
cashapelayer *masklayer = [[cashapelayer alloc] init];
masklayer.frame = self.canclebtn.bounds;
masklayer.path = maskpath.cgpath;
self.canclebtn.layer.mask = masklayer;
[self.alertview addsubview:self.canclebtn];
}
//只有确定按钮
if(suretitle && !cancletitle){
self.surebtn = [uibutton buttonwithtype:uibuttontypesystem];
self.surebtn.frame = cgrectmake(0, cgrectgetmaxy(self.lineview.frame), alertw, 40);
[self.surebtn setbackgroundimage:[self imagewithcolor:[uicolor colorwithred:255/255.0 green:255/255.0 blue:255/255.0 alpha:0.2]] forstate:uicontrolstatenormal];
[self.surebtn setbackgroundimage:[self imagewithcolor:[uicolor colorwithred:255/255.0 green:255/255.0 blue:255/255.0 alpha:0.2]] forstate:uicontrolstateselected];
[self.surebtn settitle:suretitle forstate:uicontrolstatenormal];
//[self.surebtn settitlecolor:[uicolor graycolor] forstate:uicontrolstatenormal];
self.surebtn.tag = 2;
[self.surebtn addtarget:self action:@selector(buttonevent:) forcontrolevents:uicontroleventtouchupinside];
uibezierpath *maskpath = [uibezierpath bezierpathwithroundedrect:self.surebtn.bounds byroundingcorners:uirectcornerbottomleft | uirectcornerbottomright cornerradii:cgsizemake(5.0, 5.0)];
cashapelayer *masklayer = [[cashapelayer alloc] init];
masklayer.frame = self.surebtn.bounds;
masklayer.path = maskpath.cgpath;
self.surebtn.layer.mask = masklayer;
[self.alertview addsubview:self.surebtn];
}
//计算高度
cgfloat alertheight = cancletitle?cgrectgetmaxy(self.canclebtn.frame):cgrectgetmaxy(self.surebtn.frame);
self.alertview.frame = cgrectmake(0, 0, alertw, alertheight);
self.alertview.layer.position = self.center;
[self addsubview:self.alertview];
}
return self;
}
#pragma mark - 弹出 -
- (void)showxlalertview
{
uiwindow *rootwindow = [uiapplication sharedapplication].keywindow;
[rootwindow addsubview:self];
[self creatshowanimation];
}
- (void)creatshowanimation
{
self.alertview.layer.position = self.center;
self.alertview.transform = cgaffinetransformmakescale(0.90, 0.90);
[uiview animatewithduration:0.25 delay:0 usingspringwithdamping:0.8 initialspringvelocity:1 options:uiviewanimationoptioncurvelinear animations:^{
self.alertview.transform = cgaffinetransformmakescale(1.0, 1.0);
} completion:^(bool finished) {
}];
}
#pragma mark - 回调 -设置只有2 -- > 确定才回调
- (void)buttonevent:(uibutton *)sender
{
if (sender.tag == 2) {
if (self.resultindex) {
self.resultindex(sender.tag);
}
}
[self removefromsuperview];
}
-(uilabel *)getadaptivelable:(cgrect)rect andtext:(nsstring *)contentstr andistitle:(bool)istitle
{
uilabel *contentlbl = [[uilabel alloc] initwithframe:rect];
contentlbl.numberoflines = 0;
contentlbl.text = contentstr;
contentlbl.textalignment = nstextalignmentcenter;
if (istitle) {
contentlbl.font = [uifont boldsystemfontofsize:16.0];
}else{
contentlbl.font = [uifont systemfontofsize:14.0];
}
nsmutableattributedstring *mattrstr = [[nsmutableattributedstring alloc] initwithstring:contentstr];
nsmutableparagraphstyle *mparastyle = [[nsmutableparagraphstyle alloc] init];
mparastyle.linebreakmode = nslinebreakbycharwrapping;
[mparastyle setlinespacing:3.0];
[mattrstr addattribute:nsparagraphstyleattributename value:mparastyle range:nsmakerange(0,[contentstr length])];
[contentlbl setattributedtext:mattrstr];
[contentlbl sizetofit];
return contentlbl;
}
-(uiimage *)imagewithcolor:(uicolor *)color
{
cgrect rect = cgrectmake(0.0f, 0.0f, 1.0f, 1.0f);
uigraphicsbeginimagecontext(rect.size);
cgcontextref context = uigraphicsgetcurrentcontext();
cgcontextsetfillcolorwithcolor(context, [color cgcolor]);
cgcontextfillrect(context, rect);
uiimage *theimage = uigraphicsgetimagefromcurrentimagecontext();
uigraphicsendimagecontext();
return theimage;
}
@end
在需要使用的地方直接调用
xlalertview *xlalertview = [[xlalertview alloc] initwithtitle:@"自定义uialertview" message:@"不喜勿喷,大神多多指导。不胜感激" surebtn:@"确认" canclebtn:@"取消"];
xlalertview.resultindex = ^(nsinteger index){
//回调---处理一系列动作
};
[xlalertview showxlalertview];
总结
以上就是这篇文章的全部内容了,希望本文的内容对各位ios开发们能有所帮助,如果有疑问大家可以留言交流,谢谢大家对硕编程的支持。
【说明】:本文章由站长整理发布,文章内容不代表本站观点,如文中有侵权行为,请与本站客服联系(QQ:254677821)!