关于ios自定义弹框

有时开发项目过程中系统弹框不是想要的效果,那么久需要自定义弹框。首先弹框继承与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;


.m中实现

#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];

    

}


最终运行效果如附图


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值