有时开发项目过程中系统弹框不是想要的效果,那么久需要自定义弹框。首先弹框继承与UIView。
如下代码:
#import <UIKit/UIKit.h>
typedef void (^AlertResult)(NSInteger index);
@interface PhoneView : UIView
@property(copy,nonatomic)AlertResult resultIndex;
@property(strong,nonatomic)UILabel*phone;
@property(strong,nonatomic)UILabel*workTime;
- (void)show;
#import "PhoneView.h"
#import "Header.h"
@interface PhoneView ()
@property(strong,nonatomic)UIView*alterV;
@property(strong,nonatomic)UILabel*phoneTitle;
@property(strong,nonatomic)UILabel*timeTitle;
@property(strong,nonatomic)UIButton*call;
@property(strong,nonatomic)UIButton*cancel;
@end
@implementation PhoneView
-(instancetype)init{
if (self=[super init]) {
float totalW=SCREEN_W-2*DIS;
float buttonW=(totalW-3*DIS)/2;
self.frame = [UIScreen mainScreen].bounds;
self.backgroundColor=[UIColor lightGrayColor];
self.alpha=0.6;
_alterV=[[UIView alloc]initWithFrame:CGRectMake(0, 0, totalW, 200)];
_alterV.backgroundColor=[UIColor whiteColor];
_alterV.layer.cornerRadius=5;
[self addSubview:_alterV];
_phoneTitle=[[UILabel alloc]initWithFrame:CGRectMake(TAP, TAP, 80, 40)];
_phoneTitle.textColor=[UIColor lightGrayColor];
//[self.contentView addSubview:_title];
_phoneTitle.font=[UIFont systemFontOfSize:18];
_phoneTitle.textAlignment=NSTextAlignmentLeft;
_phoneTitle.text=@"联系电话";
[_alterV addSubview:_phoneTitle];
_phone=[[UILabel alloc]initWithFrame:CGRectMake(CGRectGetMaxX(_phoneTitle.frame)+TAP, TAP, totalW-3*TAP-CGRectGetWidth(_phoneTitle.frame), 40)];
_phone.textColor=[UIColor blackColor];
//[self.contentView addSubview:_title];
_phone.font=[UIFont systemFontOfSize:18];
_phone.textAlignment=NSTextAlignmentLeft;
[_alterV addSubview:_phone];
_timeTitle=[[UILabel alloc]initWithFrame:CGRectMake(CGRectGetMinX(_phoneTitle.frame), CGRectGetMaxY(_phoneTitle.frame)+TAP, CGRectGetWidth(_phoneTitle.frame), 40)];
_timeTitle.textColor=[UIColor lightGrayColor];
//[self.contentView addSubview:_title];
_timeTitle.font=[UIFont systemFontOfSize:18];
_timeTitle.textAlignment=NSTextAlignmentLeft;
_timeTitle.text=@"工作时间";
[_alterV addSubview:_timeTitle];
_workTime=[[UILabel alloc]initWithFrame:CGRectMake(CGRectGetMaxX(_timeTitle.frame)+TAP, CGRectGetMaxY(_phoneTitle.frame)+TAP, CGRectGetWidth(_phone.frame), 40)];
_workTime.textColor=[UIColor blackColor];
//[self.contentView addSubview:_title];
_workTime.font=[UIFont systemFontOfSize:18];
_workTime.textAlignment=NSTextAlignmentLeft;
[_alterV addSubview:_workTime];
UILabel*line=[[UILabel alloc]initWithFrame:CGRectMake(TAP, CGRectGetMaxY(_workTime.frame)+TAP, totalW, 10)];
line.text=@"--------------------------------";
line.textColor=[UIColor lightGrayColor];
[_alterV addSubview:line];
_call=[UIButton buttonWithType:UIButtonTypeCustom];
_call.frame=CGRectMake(DIS, CGRectGetMaxY(line.frame)+TAP, buttonW, 40);
_call.titleLabel.font=[UIFont systemFontOfSize:18];
[_call setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[_call setTitle:@"立即拨打" forState:UIControlStateNormal];
[_call setBackgroundImage:[UIImage imageNamed:@"等级"] forState:UIControlStateNormal];
[_alterV addSubview:_call];
[_call addTarget:self action:@selector(callPhone:) forControlEvents:UIControlEventTouchUpInside];
_call.tag=99;
_cancel=[UIButton buttonWithType:UIButtonTypeCustom];
_cancel.frame=CGRectMake(CGRectGetMaxX(_call.frame)+DIS, CGRectGetMaxY(line.frame)+TAP, buttonW, 40);
_cancel.titleLabel.font=[UIFont systemFontOfSize:18];
[_cancel setTitleColor:RGBA(209, 167, 76, 1) forState:UIControlStateNormal];
[_cancel setTitle:@"取消" forState:UIControlStateNormal];
_cancel.layer.borderWidth=2;
CGColorSpaceRef colorSpaceRef = CGColorSpaceCreateDeviceRGB();
CGColorRef color = CGColorCreate(colorSpaceRef, (CGFloat[]){0.8,0.7,0.3,1});
[_cancel.layer setBorderColor:color];
[_alterV addSubview:_cancel];
[_cancel addTarget:self action:@selector(callPhone:) forControlEvents:UIControlEventTouchUpInside];
_cancel.tag=88;
_cancel.layer.cornerRadius=20;
}
return self;
}
- (void)show{
UIWindow*root=[UIApplication sharedApplication].keyWindow;
[root addSubview:self];
self.alterV.layer.position = self.center;
self.alterV.transform = CGAffineTransformMakeScale(0.90, 0.90);
[UIView animateWithDuration:0.25 delay:0 usingSpringWithDamping:0.8 initialSpringVelocity:1 options:UIViewAnimationOptionCurveLinear animations:^{
self.alterV.transform = CGAffineTransformMakeScale(1.0, 1.0);
} completion:^(BOOL finished) {
}];
}
-(void)callPhone:(UIButton*)button{
if (button.tag==99) {
if (self.resultIndex) {
self.resultIndex(button.tag);
}
}
[self removeFromSuperview];
}