iOS中你需要的弹窗效果总结大全

  

  

弹框是人机交互中常见的方式,常常出现于询问,警示以及完成某个插入任务,常见于网页端及移动端。弹框能使用户有效聚焦于当前最紧急的信息,也可以在不用离开当前页面的前提下,完成一些轻量的任务。

  

在我们的实际开发项目中,弹窗是必不可少的,很多时候我们用的是系统的AlertViewController,但是实际情况中,并不能满足我们的开发需求,这个时候我们需要的就是自定义自己的弹窗效果。接下来我会写一些自己的所封装的弹窗效果。包括代理委托回调,块回调,xib新建视图来创建我们需要的弹窗效果。

  

下面话不多说了,来一起看看详细的介绍吧

  


  

  

1。在我们自己动手之前一定要先看看官方是怎么封装的,这样我们写出来的代码才接近苹果语言,看起来高大上。好的代码一定是见名知意的,别人一看这个方法就知道大概我们通过这个方法可以得到什么样的效果。

     //ios8.0之后   UIAlertController * alertController=[UIAlertController alertControllerWithTitle: @”提示”信息:@“消息”preferredStyle: UIAlertControllerStyleAlert);      UIAlertAction * cancelAction=[UIAlertAction actionWithTitle: @”取消”风格:UIAlertActionStyleCancel处理程序:nil);   UIAlertAction * okAction=[UIAlertAction actionWithTitle: @”确定”风格:UIAlertActionStyleDefault处理程序:^ (UIAlertAction * _Nonnull行动){   NSLog(@”确定”);   });      [alertController addAction cancelAction):;   [alertController addAction okAction):;   【自我presentViewController: alertController动画:是的完成:nil);//ios8.0之前   UIAlertView * alertView=[[UIAlertView alloc] initWithTitle: @“微量”消息:@“这是消息”代表:自我   cancelButtonTitle: @“取消”otherButtonTitles: nil, nil);   (alertView显示),之前      

因为在代码量风格上,我还是比较喜欢老版本的弹窗,毕竟代码上啊,一句话调用美滋滋,所以接下来我们封装也是模仿官方开始.....

  


  

  

我们可以看到在苹果官方中,我们需要通过识别用户点击某个按钮来确定需要进一步的操作事件,这个时候是通过代理来实现的。代理的话,我们在熟悉不过了。

  

1,首先申明协议
  

        # pragma马克-协议   @class HLAlertView;   @protocol HLAlertViewDelegate   - (void) alertViewDidClickButtonWithIndex: (NSInteger)指数;   @end      

2,在viewController中遵循代理,设置代理,实现方法即可
  

        & lt; HLAlertViewDelegate>   self.delegate=自我;      # pragma马克- - - HLAlertViewDelegate   - (void) alertViewDidClickButtonWithIndex: (NSInteger)指数{   如果(指数==AlertSureButtonClick) {   (自我alertSureButtonClick);   其他}{   (自我alertCauseButtonClick);   }   }      

3,接下来就是实现我们封装类的. h文件方法申明,以及。米的实现方法
  

     //文件   # import & lt; UIKit/UIKit.h>      typedef枚举:了NSUInteger {   AlertCauseButtonClick=0,   AlertSureButtonClick   }AlertButtonClickIndex;      # pragma马克-协议   @class HLAlertView;   @protocol HLAlertViewDelegate   - (void) alertViewDidClickButtonWithIndex: (NSInteger)指数;   @end   @ interface HLAlertView: UIView      @ property(原子、弱)id & lt; HLAlertViewDelegate>委托;      - (instancetype) initWithTittle:(NSString *)一点信息:(NSString *)消息sureButton: sureBtn (NSString *);   - (void)显示;      @end            @ interface HLAlertView ()/* *弹窗主内容视图*/@ property(原子、强)UIView * contentView;/* *弹窗标题*/@ property(原子、复制)NSString *标题;/* *信息*/@ property(原子、复制)NSString *消息;/* *确认按钮*/@ property(原子、复制)UIButton * sureButton;      @end         @ implementation HLAlertView      - (instancetype) initWithTittle:(NSString *)一点信息:(NSString *)消息sureButton: (NSString *) sureBtn {      如果(自我=(超级init)) {   自我。标题=一点;   自我。消息=消息;      (自我sutUpView);   }   回归自我;   }      - (void) sutUpView {   self.frame=[UIScreen mainScreen] .bounds;   自我。α写成backgroundColor=[用户界面颜色colorWithWhite: 0.5: 0.85);   [UIView animateWithDuration: 0.5动画:^ {   自我。α=1;   });//- - - - - - -弹窗主内容- - - - - - -//自我。contentView=[[UIView alloc] init);   self.contentView.frame=CGRectMake (0, 0, SCREEN_WIDTH - 80, 150);   self.contentView。中心=self.center;   self.contentView。写成backgroundColor=[用户界面颜色whiteColor];   self.contentView.layer。cornerRadius=6;   (自我addSubview: self.contentView);//标题   UILabel * titleLabel=[[UILabel alloc] initWithFrame: CGRectMake (self.contentView 0, 10日。宽度、22)];   titleLabel。字体=[UIFont boldSystemFontOfSize: 20);   titleLabel。textAlignment=NSTextAlignmentCenter;   titleLabel。文本=self.title;   (自我。contentView addSubview titleLabel):;//消息   UILabel * messageLable=[[UILabel alloc] initWithFrame: CGRectMake (0 50 self.contentView。宽度、22)];   messageLable。字体=[UIFont boldSystemFontOfSize: 17);   messageLable。textAlignment=NSTextAlignmentCenter;   messageLable。文本=self.message;   (自我。contentView addSubview messageLable):;//取消按钮   UIButton * causeBtn=[UIButton buttonWithType UIButtonTypeCustom):;   causeBtn.frame=CGRectMake (0, self.contentView。身高- 40,self.contentView。宽/2、40);   causeBtn。写成backgroundColor=[用户界面颜色grayColor];   [causeBtn setTitle: @”取消”forState: UIControlStateNormal);   [causeBtn addTarget:自我行动:@ selector (causeBtn:) forControlEvents: UIControlEventTouchUpInside);   (自我。contentView addSubview causeBtn):;//确认按钮   UIButton * sureButton=[UIButton buttonWithType UIButtonTypeCustom):;   sureButton.frame=CGRectMake (causeBtn。宽度,causeBtn。y, causeBtn。宽度,40);   sureButton。写成backgroundColor=[用户界面颜色redColor];   [sureButton setTitle: @”确定“forState: UIControlStateNormal);   [sureButton addTarget:自我行动:@ selector (processSure:) forControlEvents: UIControlEventTouchUpInside);      (自我。contentView addSubview sureButton):;      }      - (void) {   ui窗口* keyWindow=[UIApplication sharedApplication] .keyWindow;   (keyWindow addSubview:自我);   }         null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null

iOS中你需要的弹窗效果总结大全