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

前言

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

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

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

官方思路

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

// ios8.0 之后
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"提示" message:@"message" preferredStyle:UIAlertControllerStyleAlert];

UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
NSLog(@"确定");
}];

[alertController addAction:cancelAction];
[alertController addAction:okAction];
[self presentViewController:alertController animated:YES completion:nil];
// ios8.0 之前
UIAlertView * alertView = [[UIAlertView alloc] initWithTitle:@"Tittle" message:@"This is message" delegate:self
cancelButtonTitle:@"cancel" otherButtonTitles:nil, nil];
[alertView show];

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

delegate

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

1、首先申明协议

#pragma mark - 协议
@class HLAlertView;
@protocol HLAlertViewDelegate<NSObject>
- (void)alertViewDidClickButtonWithIndex:(NSInteger)index;
@end

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

<HLAlertViewDelegate>
self.delegate = self;

#pragma mark --- HLAlertViewDelegate
-(void)alertViewDidClickButtonWithIndex:(NSInteger)index{
if (index == AlertSureButtonClick) {
[self alertSureButtonClick];
}else{
[self alertCauseButtonClick];
}
}

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

//.h 文件
#import <UIKit/UIKit.h>

typedef enum : NSUInteger {
AlertCauseButtonClick = 0,
AlertSureButtonClick
} AlertButtonClickIndex;

#pragma mark - 协议
@class HLAlertView;
@protocol HLAlertViewDelegate<NSObject>
- (void)alertViewDidClickButtonWithIndex:(NSInteger)index;
@end
@interface HLAlertView : UIView

@property(nonatomic, weak) id <HLAlertViewDelegate> delegate;

- (instancetype)initWithTittle:(NSString *)tittle message:(NSString *)message sureButton:(NSString *)sureBtn;
- (void)show;

@end
@interface HLAlertView()

/** 弹窗主内容view */
@property (nonatomic,strong) UIView *contentView;

/** 弹窗标题 */
@property (nonatomic,copy) NSString *title;

/** message */
@property (nonatomic,copy) NSString *message;

/** 确认按钮 */
@property (nonatomic,copy) UIButton *sureButton;

@end

@implementation HLAlertView

- (instancetype)initWithTittle:(NSString *)tittle message:(NSString *)message sureButton:(NSString *)sureBtn{

if (self = [super init]) {
self.title = tittle;
self.message = message;

[self sutUpView];
}
return self;
}

- (void)sutUpView{
self.frame = [UIScreen mainScreen].bounds;
self.backgroundColor = [UIColor colorWithWhite:0.5 alpha:0.85];
[UIView animateWithDuration:0.5 animations:^{
self.alpha = 1;
}];

//------- 弹窗主内容 -------//
self.contentView = [[UIView alloc]init];
self.contentView.frame = CGRectMake(0, 0, SCREEN_WIDTH - 80, 150);
self.contentView.center = self.center;
self.contentView.backgroundColor = [UIColor whiteColor];
self.contentView.layer.cornerRadius = 6;
[self addSubview:self.contentView];

// 标题
UILabel *titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 10, self.contentView.width, 22)];
titleLabel.font = [UIFont boldSystemFontOfSize:20];
titleLabel.textAlignment = NSTextAlignmentCenter;
titleLabel.text = self.title;
[self.contentView addSubview:titleLabel];

// message
UILabel *messageLable = [[UILabel alloc]initWithFrame:CGRectMake(0, 50, self.contentView.width, 22)];
messageLable.font = [UIFont boldSystemFontOfSize:17];
messageLable.textAlignment = NSTextAlignmentCenter;
messageLable.text = self.message;
[self.contentView addSubview:messageLable];

// 取消按钮
UIButton * causeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
causeBtn.frame = CGRectMake(0, self.contentView.height - 40, self.contentView.width/2, 40);
causeBtn.backgroundColor = [UIColor grayColor];
[causeBtn setTitle:@"取消" forState:UIControlStateNormal];
[causeBtn addTarget:self action:@selector(causeBtn:) forControlEvents:UIControlEventTouchUpInside];
[self.contentView addSubview:causeBtn];

// 确认按钮
UIButton * sureButton = [UIButton buttonWithType:UIButtonTypeCustom];
sureButton.frame = CGRectMake(causeBtn.width, causeBtn.y, causeBtn.width, 40);
sureButton.backgroundColor = [UIColor redColor];
[sureButton setTitle:@"确定" forState:UIControlStateNormal];
[sureButton addTarget:self action:@selector(processSure:) forControlEvents:UIControlEventTouchUpInside];

[self.contentView addSubview:sureButton];

}

- (void)show{
UIWindow *keyWindow = [UIApplication sharedApplication].keyWindow;
[keyWindow addSubview:self];
}

- (void)processSure:(UIButton *)sender{
if ([self.delegate respondsToSelector:@selector(alertViewDidClickButtonWithIndex:)]) {
[self.delegate alertViewDidClickButtonWithIndex:AlertSureButtonClick];
}
[self dismiss];
}

- (void)causeBtn:(UIButton *)sender{

if ([self.delegate respondsToSelector:@selector(alertViewDidClickButtonWithIndex:)]) {
[self.delegate alertViewDidClickButtonWithIndex:AlertCauseButtonClick];
}
[self dismiss];
}

#pragma mark - 移除此弹窗
/** 移除此弹窗 */
- (void)dismiss{
[self removeFromSuperview];
}

通过代理的方式我们就完成了我们自己页面的封装了。

block弹窗

先看一下封装之后我们的调用方式吧:

HLAlertViewBlock * alertView = [[HLAlertViewBlock alloc] initWithTittle:@"提示" message:@"通过Block弹窗回调的弹窗" block:^(NSInteger index) {
if (index == AlertSureButtonClick) {
[self alertSureButtonClick];
}else{
[self alertCauseButtonClick];
}
}];
[alertView show];

相比代理的方式的话,我们还行喜欢这种block回调的,简大气接地气啊。当然在我们需要处理逻辑多的时候,还是代理会比较好一点,具体环境下具体使用。

封装成block的好处就是在我们构造方法的时候就可以实现我们将来的点击方法,所以在自定义弹窗类的.h文件中,我们要申明block属性。代码

//.h
@interface HLAlertViewBlock : UIView

@property(nonatomic, copy) void (^buttonBlock) (NSInteger index);

- (instancetype)initWithTittle:(NSString *)tittle message:(NSString *)message block:(void (^) (NSInteger index))block;

- (void)show;

@end
//.m
@interface HLAlertViewBlock()

/** 弹窗主内容view */
@property (nonatomic,strong) UIView *contentView;

/** 弹窗标题 */
@property (nonatomic,copy) NSString *title;

/** message */
@property (nonatomic,copy) NSString *message;

/** 确认按钮 */
@property (nonatomic,copy) UIButton *sureButton;

@end

@implementation HLAlertViewBlock

- (instancetype)initWithTittle:(NSString *)tittle message:(NSString *)message block:(void (^)(NSInteger))block{
if (self = [super init]) {
self.title = tittle;
self.message = message;
self.buttonBlock = block;
[self sutUpView];
}
return self;
}

到此为止,我们的block弹窗申明方法也搞定了。

xib的封装弹窗

好处就是不用写界面代码了。

殊途同归

还有一种实现弹窗效果的方法,不通过新建view而是Controller来实现的,就是新建一个透明的控制器。代码如下

PopViewController * popVC = [[PopViewController alloc] init];
UIColor * color = [UIColor blackColor];
popVC.view.backgroundColor = [color colorWithAlphaComponent:0.85];
popVC.modalPresentationStyle = UIModalPresentationOverCurrentContext;
[self presentViewController:popVC animated:NO completion:nil];

更加简单,逻辑也更加好处理一些。

最后附上demo地址:gibHub地址:https://github.com/MrBMask

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,如果有疑问大家可以留言交流,谢谢大家对我们的支持。

(0)

相关推荐

  • IOS 中弹框的实现方法整理

    IOS 中弹框的实现方法整理 #define iOS8Later ([UIDevice currentDevice].systemVersion.doubleValue >= 8.0) ios 8以前的弹框 @interface RootViewController ()<UIAlertViewDelegate> @end UIAlertView * alert = [[UIAlertView alloc] initWithTitle:@"登陆失败" message:

  • IOS中Swift仿QQ最新版抽屉侧滑和弹框视图

    导读 简单用Swift写了一个抽屉效果,可以直接使用并且简单; 很多软件都运了抽屉效果,比如qq的左抽屉,英雄联盟,滴滴打车,和uber等等都运用了抽屉; 效果 iOS抽屉式结构实现分析 主要是在控制器的View上添加了两个View,一个左侧leftView和一个mainView.这里我们自定义一个DrawerViewController,init(mainVC: UIViewController, leftMenuVC: UIViewController, leftWidth: CGFloat

  • iOS实现多个弹框按顺序依次弹出效果

    有时候会有这样的需求:App 运行完,加载 RootVC ,此时需要做一些操作,比如检查更新,之类的.此时可能会需要有2个甚至多个弹框依次弹出. 本篇将以系统的 UIAlertController 作为示例,当然,如果是自定义的,也要看一下这篇文章,如何来处理多个弹窗. 首先,如果就按照如下的默认写法: - (void)viewDidAppear:(BOOL)animated { [super viewDidAppear:animated]; UIAlertController *alert =

  • ionic在开发ios系统微信时键盘挡住输入框的解决方法(键盘弹出问题)

    在使用ionic开发IOS系统微信的时候会有一个苦恼的问题,填写表单的时候键盘会挡住输入框,其实并不算什么大问题,只要用户输入一个字就可以立刻看见输入框了. 可惜的是,有些客户是不讲理的,他才不管这个问题,反正就是不行,所以在一天睡觉的时候突然惊醒,想出来这个方案. 我就不仔细讲代码了,直接上图 angular.module('MyApp') .directive('focusInput', ['$ionicScrollDelegate', '$window', '$timeout', '$io

  • iOS项目开发键盘弹出遮挡输入框问题解决方案

    在iOS或Android等移动端开发过程中,经常遇到很多需要我们输入信息的情况,例如登录时要输入账号密码.查询时要输入查询信息.注册或申请时需要填写一些信息等都是通过我们键盘来进行输入的,在iOS开发过程中,一般用于进行输入信息的有两类:UITextField和UITextView,前者是单行输入文本框,后者是可滑动的多行输入文本框,在这整个开发过程中,我们需要控制键盘的弹出和收起.在输入结束的时候获取输入的信息,此外,我们还需要保证在键盘弹起的时候不遮挡我们输入的文本框.今天,我们就主要来说一

  • iOS自定义提示弹出框实现类似UIAlertView的效果

    首先来看看实现的效果图 下面话不多说,以下是实现的示例代码 #import <UIKit/UIKit.h> typedef void(^AlertResult)(NSInteger index); @interface XLAlertView : UIView @property (nonatomic,copy) AlertResult resultIndex; - (instancetype)initWithTitle:(NSString *)title message:(NSString

  • 详谈iOS 位置权限弹出框闪现的问题

    当编码如下的时候,进入页面的时候可以看到UIAlertView弹出框出现一下,刚想点击的时候,他不见了,这个郁闷 CLLocationManager* _locationManager = [[CLLocationManager alloc] init]; _locationManager.desiredAccuracy = kCLLocationAccuracyHundredMeters; if ([[UIDevice currentDevice].systemVersion floatVal

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

    前言 弹框是人机交互中常见的方式,常常出现于询问.警示以及完成某个插入任务,常见于网页端及移动端.弹框能使用户有效聚焦于当前最紧急的信息,也可以在不用离开当前页面的前提下,完成一些轻量的任务. 在我们的实际开发项目中,弹窗是必不可少的,很多时候我们用的是系统的AlertViewController,但是实际情况中,并不能满足我们的开发需求,这个时候我们需要的就是自定义自己的弹窗效果.接下来我会写一些自己的所封装的弹窗效果.包括代理delegate回调,block 回调,xib新建view来创建我

  • iOS中自定义弹出pickerView效果(DEMO)

    UIPickerView平常用的地方好像也不是很多,顶多就是一些需要选择的地方,这次项目需要这一个功能,我就单独写了一个简单的demo,效果图如下: 新增主页面弹出view,在主页面添加的代码 有个小问题就是第四个直接添加在主页弹出来的view好像被导航栏给覆盖了,我还没去研究,就着急的先吧功能写了.大家谅解下 最初版本 话说我终于弄了gif了,再也不要去截图每张图都发一遍了!! 这个demo呢,等于是可以拿来直接用的第三方了吧,只需要传数据就可以了,弹出的三个框显示的数据也不一样,我的封装能力

  • iOS中实现图片自适应拉伸效果的方法

    前言 在Android中实现图片的拉伸特别特别简单,甚至不用写一行代码,直接使用.9图片进行划线即可.但是iOS就没这么简单了,比如对于下面的一张图片(原始尺寸:200*103): 我们不做任何处理,直接将它用作按钮的背景图片: // // ViewController.m // ChatBgTest // // Created by 李峰峰 on 2017/1/23. // Copyright © 2017年 李峰峰. All rights reserved. // #import "View

  • 详解IOS中如何实现瀑布流效果

    首先是效果演示 特点:可以自由设置瀑布流的总列数(效果演示为2列) 虽然iphone手机的系统相册没有使用这种布局效果,瀑布流依然是一种很常见的布局方式!!!下面来详细介绍如何实现这种布局. 首先使用的类是UICollectionView 我们要做的是自定义UICollectionViewCell和UICollectionViewLayout 1.自定义UICollectionViewCell类,只需要一个UIImageView即可,frame占满整个cell. 2.重点是自定义UICollec

  • iOS中Navbar设置渐变色效果的方法示例

    本文主要给大家介绍了关于iOS中Navbar设置渐变色效果的相关内容,分享出来供大家参考学习,下面来看看详细的介绍吧. 设置渐变色 #import "NavigationViewController.h" #define LBColor(r, g, b) [UIColor colorWithRed:(r)/255.0 green:(g)/255.0 blue:(b)/255.0 alpha:1.0] @interface NavigationViewController () @end

  • iOS中利用CoreAnimation实现一个时间的进度条效果

    在iOS中实现进度条通常都是通过不停的设置progress来完成的,这样的进度条适用于网络加载(上传下载文件.图片等).但是对于录制视频这样的需求的话,如果是按照每秒来设置进度的话,显得有点麻烦,于是我就想直接用CoreAnimation来按时间做动画,只要设置最大时间,其他的就不用管了,然后在视频暂停与继续录制时,对动画进行暂停和恢复即可.录制视频的效果如下: 你可以在这里下载demo 那么接下来就是如何用CoreAnimation实现一个进度条控件了. 首先呢,让我们创建一个继承自CASha

  • iOS中给自定义tabBar的按钮添加点击放大缩小的动画效果

    之前想过一些通过第三方的方式实现动画,感觉有点麻烦,就自己写了一个 不足之处还望大家多多指出 // 一句话,写在UITabBarController.m脚本中,tabBar是自动执行的方法 // 点击tabbarItem自动调用 -(void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item { NSInteger index = [self.tabBar.items indexOfObject:item]; [self a

  • jQuery 中msgTips 顶部弹窗效果实现代码

    最近发现好多网站都采用顶部弹窗,并且不用用户手动去点击确定.感觉这样很方便用户,所以也找了好多大神的代码,整理一下方便以后查找 前端: @{ Layout = null; } <!DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns=&quo

  • Android UI设计系列之自定义SwitchButton开关实现类似IOS中UISwitch的动画效果(2)

    做IOS开发的都知道,IOS提供了一个具有动态开关效果的UISwitch组件,这个组件很好用效果相对来说也很绚丽,当我们去点击开关的时候有动画效果,但遗憾的是Android上并没有给我们提供类似的组件(听说在Android4.0的版本上提供了具有动态效果的开关组件,不过我还没有去看文档),如果我们想实现类似的效果那该怎么办了呢?看来又得去自定义了. 公司的产品最近一直在做升级,主要做的就是把界面做的更绚丽更美观给用户更好的体验(唉,顾客是上帝......),其中的设置功能中就有开关按钮,原来的开

  • Android中实现iOS中的毛玻璃效果

    为了实现毛玻璃效果,我们需要一组compute kernels(.rs文件中编写),及一组用于控制renderScript相关的Javaapi(.rs文件自动生成为Java类). 由于compute kernels的编写需要一定的学习成本,从JELLY_BEAN_MR1开始,Androied内置了一些compute kernels用于常用的操作,其中就包括了Gaussian blur. 下面,通过实操来讲解一下RenderScript来实现高斯模糊,最终实现效果(讲文字背景进行模糊处理): 实现

随机推荐