关于ios自定义弹框

来源:互联网 发布:java语言和c语言 编辑:程序博客网 时间:2024/06/16 19:35

有时开发项目过程中系统弹框不是想要的效果,那么久需要自定义弹框。首先弹框继承与UIView。

如下代码:

#import <UIKit/UIKit.h>


typedefvoid (^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"


@interfacePhoneView ()


@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=[superinit]) {


        float totalW=SCREEN_W-2*DIS;

        

        float buttonW=(totalW-3*DIS)/2;

        

        

        

         self.frame = [UIScreenmainScreen].bounds;

        

        self.backgroundColor=[UIColorlightGrayColor];

        self.alpha=0.6;

    

        _alterV=[[UIViewalloc]initWithFrame:CGRectMake(0,0, totalW, 200)];

        _alterV.backgroundColor=[UIColorwhiteColor];

        

        _alterV.layer.cornerRadius=5;

        

        [selfaddSubview:_alterV];

        

        

        _phoneTitle=[[UILabelalloc]initWithFrame:CGRectMake(TAP,TAP, 80, 40)];

        _phoneTitle.textColor=[UIColorlightGrayColor];

        //[self.contentView addSubview:_title];

        _phoneTitle.font=[UIFontsystemFontOfSize:18];

        _phoneTitle.textAlignment=NSTextAlignmentLeft;

        _phoneTitle.text=@"联系电话";

        [_alterVaddSubview:_phoneTitle];

        

        _phone=[[UILabelalloc]initWithFrame:CGRectMake(CGRectGetMaxX(_phoneTitle.frame)+TAP,TAP, totalW-3*TAP-CGRectGetWidth(_phoneTitle.frame),40)];

        _phone.textColor=[UIColorblackColor];

        //[self.contentView addSubview:_title];

        _phone.font=[UIFontsystemFontOfSize:18];

        _phone.textAlignment=NSTextAlignmentLeft;

        [_alterVaddSubview:_phone];

        

        _timeTitle=[[UILabelalloc]initWithFrame:CGRectMake(CGRectGetMinX(_phoneTitle.frame),CGRectGetMaxY(_phoneTitle.frame)+TAP,CGRectGetWidth(_phoneTitle.frame),40)];

        _timeTitle.textColor=[UIColorlightGrayColor];

        //[self.contentView addSubview:_title];

        _timeTitle.font=[UIFontsystemFontOfSize:18];

        _timeTitle.textAlignment=NSTextAlignmentLeft;

        _timeTitle.text=@"工作时间";

        [_alterVaddSubview:_timeTitle];

        

        _workTime=[[UILabelalloc]initWithFrame:CGRectMake(CGRectGetMaxX(_timeTitle.frame)+TAP,CGRectGetMaxY(_phoneTitle.frame)+TAP,CGRectGetWidth(_phone.frame),40)];

        _workTime.textColor=[UIColorblackColor];

        //[self.contentView addSubview:_title];

        _workTime.font=[UIFontsystemFontOfSize:18];

        _workTime.textAlignment=NSTextAlignmentLeft;

        [_alterVaddSubview:_workTime];

 

        UILabel*line=[[UILabelalloc]initWithFrame:CGRectMake(TAP,CGRectGetMaxY(_workTime.frame)+TAP, totalW, 10)];

        line.text=@"--------------------------------";

        line.textColor=[UIColorlightGrayColor];

        [_alterVaddSubview:line];


        _call=[UIButtonbuttonWithType:UIButtonTypeCustom];

        _call.frame=CGRectMake(DIS, CGRectGetMaxY(line.frame)+TAP, buttonW,40);

         _call.titleLabel.font=[UIFontsystemFontOfSize:18];

        [_callsetTitleColor:[UIColorwhiteColor] forState:UIControlStateNormal];

        [_callsetTitle:@"立即拨打"forState:UIControlStateNormal];

        [_callsetBackgroundImage:[UIImageimageNamed:@"等级"]forState:UIControlStateNormal];

        [_alterVaddSubview:_call];

        [_calladdTarget:selfaction:@selector(callPhone:)forControlEvents:UIControlEventTouchUpInside];

        _call.tag=99;


        

        

       

        _cancel=[UIButtonbuttonWithType:UIButtonTypeCustom];

        _cancel.frame=CGRectMake(CGRectGetMaxX(_call.frame)+DIS, CGRectGetMaxY(line.frame)+TAP, buttonW,40);

        _cancel.titleLabel.font=[UIFontsystemFontOfSize:18];

        [_cancelsetTitleColor:RGBA(209,167, 76, 1)forState:UIControlStateNormal];

        [_cancelsetTitle:@"取消"forState:UIControlStateNormal];

        

        _cancel.layer.borderWidth=2;

        CGColorSpaceRef colorSpaceRef =CGColorSpaceCreateDeviceRGB();

        

        CGColorRef color =CGColorCreate(colorSpaceRef, (CGFloat[]){0.8,0.7,0.3,1});

        [_cancel.layersetBorderColor:color];

        [_alterVaddSubview:_cancel];

        

      [_canceladdTarget:selfaction:@selector(callPhone:)forControlEvents:UIControlEventTouchUpInside];

        _cancel.tag=88;

        _cancel.layer.cornerRadius=20;

        

        

        

        

    }

    

    returnself;

    

}


- (void)show{

    

    UIWindow*root=[UIApplicationsharedApplication].keyWindow;

    

    [root addSubview:self];

    

    self.alterV.layer.position = self.center;

    self.alterV.transform = CGAffineTransformMakeScale(0.90,0.90);

    [UIViewanimateWithDuration:0.25delay:0usingSpringWithDamping:0.8initialSpringVelocity:1options:UIViewAnimationOptionCurveLinearanimations:^{

        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);

        }

        

        

    }

    

    [selfremoveFromSuperview];

    

}


最终运行效果如附图


原创粉丝点击