欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

iOS自定義提示彈出框?qū)崿F(xiàn)類(lèi)似UIAlertView的效果

 更新時(shí)間:2016年11月16日 11:52:24   作者:鍵盤(pán)上的演繹者  
最近在學(xué)習(xí)iOS開(kāi)發(fā),在牛刀小試的時(shí)候發(fā)現(xiàn)系統(tǒng)的UIAlertView有點(diǎn)不喜歡,然后就自己自定義了一個(gè)UIAlertView,基本上實(shí)現(xiàn)了系統(tǒng)的UIAlertView,可以根據(jù)項(xiàng)目的需求修改UIAlertView的顏色。有需要的朋友們可以參考借鑒,也可以多多指導(dǎo),下面來(lái)一起看看吧。

首先來(lái)看看實(shí)現(xiàn)的效果圖


下面話(huà)不多說(shuō),以下是實(shí)現(xiàn)的示例代碼

#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
///各個(gè)欄目之間的距離
#define XLSpace 10.0

@interface XLAlertView()

//彈窗
@property (nonatomic,retain) UIView *alertView;
//title
@property (nonatomic,retain) UILabel *titleLbl;
//內(nèi)容
@property (nonatomic,retain) UILabel *msgLbl;
//確認(rèn)按鈕
@property (nonatomic,retain) UIButton *sureBtn;
//取消按鈕
@property (nonatomic,retain) UIButton *cancleBtn;
//橫線(xiàn)線(xiàn)
@property (nonatomic,retain) UIView *lineView;
//豎線(xiàn)
@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];

    //兩個(gè)按鈕
    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];

    }

    //計(jì)算高度
    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 - 回調(diào) -設(shè)置只有2 -- > 確定才回調(diào)
- (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

在需要使用的地方直接調(diào)用

XLAlertView *xlAlertView = [[XLAlertView alloc] initWithTitle:@"自定義UIAlertView" message:@"不喜勿噴,大神多多指導(dǎo)。不勝感激" sureBtn:@"確認(rèn)" cancleBtn:@"取消"];
xlAlertView.resultIndex = ^(NSInteger index){
//回調(diào)---處理一系列動(dòng)作
};
[xlAlertView showXLAlertView];

總結(jié)

以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)各位iOS開(kāi)發(fā)們能有所幫助,如果有疑問(wèn)大家可以留言交流,謝謝大家對(duì)腳本之家的支持。

相關(guān)文章

  • Laravel框架中自定義模板指令總結(jié)

    Laravel框架中自定義模板指令總結(jié)

    Blade 允許你自定義命令,你可以使用 directive 方法注冊(cè)命令。下面這篇文章主要給大家總結(jié)介紹了關(guān)于Laravel框架中自定義模板指令的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),需要的朋友可以參考借鑒,下面來(lái)一起看看吧
    2017-12-12
  • PHP引用(&)各種使用方法實(shí)例詳解

    PHP引用(&)各種使用方法實(shí)例詳解

    php的引用(就是在變量或者函數(shù)、對(duì)象等前面加上&符號(hào)),在PHP 中引用的意思是:不同的名字訪(fǎng)問(wèn)同一個(gè)變量?jī)?nèi)容。與C語(yǔ)言中的指針是有差別的.C語(yǔ)言中的指針里面存儲(chǔ)的是變量的內(nèi)容,在內(nèi)存中存放的地址
    2014-03-03
  • phpcms中的評(píng)論樣式修改方法

    phpcms中的評(píng)論樣式修改方法

    phpcms中自帶的評(píng)論插件很好用,但是樣式不太好看,今天小編給大家分享兩種關(guān)于phpcms中的評(píng)論樣式修改方法,一起看看吧
    2016-10-10
  • Symfony2實(shí)現(xiàn)在doctrine中內(nèi)置數(shù)據(jù)的方法

    Symfony2實(shí)現(xiàn)在doctrine中內(nèi)置數(shù)據(jù)的方法

    這篇文章主要介紹了Symfony2實(shí)現(xiàn)在doctrine中內(nèi)置數(shù)據(jù)的方法,結(jié)合實(shí)例形式分析了在doctrine中內(nèi)置數(shù)據(jù)的具體步驟與相關(guān)技巧,需要的朋友可以參考下
    2016-02-02
  • php制作文本式留言板

    php制作文本式留言板

    本文給大家分享的是使用php結(jié)合文本文件制作的留言板的代碼,非常簡(jiǎn)單,實(shí)現(xiàn)了常用的功能,推薦給大家,有需要的小伙伴參考下吧。
    2015-03-03
  • centos7上編譯安裝php7以php-fpm方式連接apache

    centos7上編譯安裝php7以php-fpm方式連接apache

    這篇文章主要介紹了centos7上編譯安裝php7以php-fpm方式連接apache的相關(guān)知識(shí),非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2018-11-11
  • 最新評(píng)論