iOS实现自定义的弹出视图(popView)
前段时间,在项目中有个需求是支付完成后,弹出红包,实现这么一个发红包的功能。做了最后,实现的效果大致如下:

一、使用方法
整个ViewController的代码大致如下
//
// SecondViewController.m
// HWPopTool
//
// Created by HenryCheng on 16/1/11.
// Copyright © 2016年 www.igancao.com. All rights reserved.
//
#import "SecondViewController.h"
#import "HWPopTool.h"
@interface SecondViewController ()
@property (strong, nonatomic) UIView *contentView;
@end
@implementation SecondViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor whiteColor];
_contentView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 200, 300)];
_contentView.backgroundColor = [UIColor clearColor];
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
btn.frame = CGRectMake(100, 200, 100, 50);
btn.backgroundColor = [UIColor greenColor];
[btn addTarget:self action:@selector(popViewShow) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btn];
}
- (void)popViewShow {
UIImageView *imageV = [[UIImageView alloc]initWithFrame:_contentView.bounds];
imageV.image = [UIImage imageNamed:@"jei"];
[_contentView addSubview:imageV];
[HWPopTool sharedInstance].shadeBackgroundType = ShadeBackgroundTypeSolid;
[HWPopTool sharedInstance].closeButtonType = ButtonPositionTypeRight;
[[HWPopTool sharedInstance] showWithPresentView:_contentView animated:YES];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
我们引入了HWPopTool.h,并且创建了一个button,点击button的方法是popViewShow,我们来看一下这里面的代码:
- (void)popViewShow {
UIImageView *imageV = [[UIImageView alloc]initWithFrame:_contentView.bounds];
imageV.image = [UIImage imageNamed:@"jei"];
[_contentView addSubview:imageV];
[HWPopTool sharedInstance].shadeBackgroundType = ShadeBackgroundTypeSolid;
[HWPopTool sharedInstance].closeButtonType = ButtonPositionTypeRight;
[[HWPopTool sharedInstance] showWithPresentView:_contentView animated:YES];
}
这里在_contentView上放了一个imageView,然后我们设置了shadeBackgroundType和closeButtonType以后,下面一句代码就是展示出来popView。
这里主要就是我们弹出一个view,至于这个view多大,上面放什么,都是由你自己决定的。
二、关于HWPopTool里面的一些属性和方法
先来看一下HWPopTool.h
//
// HWPopTool.h
// HWPopTool
//
// Created by HenryCheng on 16/1/11.
// Copyright © 2016年 www.igancao.com. All rights reserved.
//
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
/**
* 关闭按钮的位置
*/
typedef NS_ENUM(NSInteger, ButtonPositionType) {
/**
* 无
*/
ButtonPositionTypeNone = 0,
/**
* 左上角
*/
ButtonPositionTypeLeft = 1 << 0,
/**
* 右上角
*/
ButtonPositionTypeRight = 2 << 0
};
/**
* 蒙板的背景色
*/
typedef NS_ENUM(NSInteger, ShadeBackgroundType) {
/**
* 渐变色
*/
ShadeBackgroundTypeGradient = 0,
/**
* 固定色
*/
ShadeBackgroundTypeSolid = 1 << 0
};
typedef void(^completeBlock)(void);
@interface HWPopTool : NSObject
@property (strong, nonatomic) UIColor *popBackgroudColor;//弹出视图的背景色
@property (assign, nonatomic) BOOL tapOutsideToDismiss;//点击蒙板是否弹出视图消失
@property (assign, nonatomic) ButtonPositionType closeButtonType;//关闭按钮的类型
@property (assign, nonatomic) ShadeBackgroundType shadeBackgroundType;//蒙板的背景色
/**
* 创建一个实例
*
* @return CHWPopTool
*/
+ (HWPopTool *)sharedInstance;
/**
* 弹出要展示的View
*
* @param presentView show View
* @param animated 是否动画
*/
- (void)showWithPresentView:(UIView *)presentView animated:(BOOL)animated;
/**
* 关闭弹出视图
*
* @param complete complete block
*/
- (void)closeWithBlcok:(void(^)())complete;
@end
由于之前写的比较仓促,今天趁着空余时间又把代码整理了一遍,比如关闭之后的回调,之前用delegate实现的,今天又用block重新写的,简洁一点吧,另外基本上所有的方法、属性、枚举我都有注释,算是个个人习惯吧。
这里面有几点需要说明的是:
1.ShadeBackgroundType是蒙板的背景色属性,有固定的和渐变的(ShadeBackgroundTypeGradient),关于这个渐变,有兴趣的可以研究一下CAGradientLayer,还是很有趣的,在后来的文章中也会说到。
2.tapOutsideToDismiss这个是设置点击蒙板,popView消失不消失的属性,默认的是YES
3.- (void)closeWithBlcok:(void(^)())complete这个方法,是关闭后的回调,比如说发送红包以后,等popView消失以后回到上一页的这种。
三、最后
https://github.com/Loveway/HWPopTool
iOS实现自定义的弹出视图(popView)的更多相关文章
- SDMask(iOS蒙层遮罩弹出引导)
SDMask介绍 地址 针对iOS项目,大部分弹出视图三方都把弹出内容作为了项目的一部分,这种耦合局限性较大.该项目对此解耦,围绕我何时需要使用蒙层而展开设计.将弹出内容和动画和事件完全分离出去让co ...
- IOS弹出视图 LewPopupViewController
LewPopupViewController是一款IOS弹出视图软件.iOS 下的弹出视图.支持iPhone/iPad. 软件截图 使用方法 弹出视图 1 2 3 4 5 PopupView *vie ...
- iOS 仿看了吗应用、指南针测网速等常用工具、自定义弹出视图框架、图片裁剪、内容扩展等源码
iOS精选源码 扩展内容的cell - folding-cell 一个近乎完整的可识别中国身份证信息的Demo 可自动快速... JPImageresizerView 仿微信的图片裁剪 带年月和至今以 ...
- IOS开发之自定义系统弹出键盘上方的view(转载)
这篇文章解决的一个开发中的实际问题就是:当弹出键盘时,自定义键盘上方的view.目前就我的经验来看,有两种解决方法.一个就是利用UITextField或者UITextView的inputAccesso ...
- UIPresentationController - iOS自定义模态弹出框
参考: https://developer.apple.com/library/archive/featuredarticles/ViewControllerPGforiPhoneOS/Definin ...
- iOS自定义提示弹出框(类似UIAlertView)
菜鸟一枚,大神勿喷.自己在牛刀小试的时候,发现系统的UIAlertView有点不喜欢,然后就自己自定义了一个UIAlertView,基本上实现了系统的UIAlertView,可以根据项目的需求修改UI ...
- UIAlertView弹出视图动画效果
在App设计中为了加强用户体验,我们会常常加入一些友好的动画效果.比如类似UIAlertView弹出的动画效果,由于系统中并没有直接提供类似的动画API,如果我们想要做出一样的效果,那就得深入的研究一 ...
- ios下input focus弹出软键盘造成fixed元素位置移位
正常状态下 input focus软键盘弹出时 问题描述: 头部结构fixed,滚动到下部内容区域,input.textarea等focus弹出软键盘时,头部位置偏移被居中(该问题ios7 beta3 ...
- 自定义PopupWindow弹出框(带有动画)
使用PopupWindow来实现弹出框,并且带有动画效果 首先自定义PopupWindow public class LostPopupWindow extends PopupWindow { pub ...
随机推荐
- 踩刹车——regularization
从一个问题说起: 当我们使用H10去拟合曲线的时候,其实我们只想要H2的结果.如果从H10变回到H2呢? 所以我们只需要添加上限制条件:w3=...=w10=0即可.现在呢,我们可以放宽一点条件:任意 ...
- ASP.NET MVC3 系列教程 - Razor视图引擎基础语法
http://www.cnblogs.com/highend/archive/2011/04/09/aspnet_mvc3_razor_engine.html 4. 关于所有带"_" ...
- vi--文本编辑常用快捷键之复制-粘贴-替换-删除
这几天刚开始接触vi编辑器,慢慢开始熟悉vi,但是还是感觉诸多不便,比如说复制粘贴删除操作不能用鼠标总是感觉不自在,而且我一般习惯用方向键移动光标,更增加了操作的复杂度,今天在网上搜索了一下,vim编 ...
- ActiveReport资料
1. ActiveReports for .NET 2 Online | ActiveReports for .NET 3 Online 2.GroupHeader块 ①GroupHeader块为每个 ...
- OpenStack Cinder组件支持的块存储设备表
摘自恒天云官网:http://www.hengtianyun.com/download-show-id-18.html OpenStack的Cinder组件底层可以连接多种存储设备和方案,每一个Ope ...
- 从Count看Oracle执行计划的选择
一. 前言 在调查一个性能问题的时候,一个同事问道,为什么数据库有些时候这么不聪明,明明表上有索引,但是在执行一个简单的count的时候居然全表扫描了!难道不知道走索引更快么? 试图从最简单的coun ...
- USACO 2013 November Contest Gold 简要题解
Problem 1. Empty Stalls 扫两遍即可. Problem 2. Line of Sight 我们发现能互相看见的一对点一定能同时看见粮仓的某一段.于是转换成有n段线段,问有多少对线 ...
- DelphiXE8怎么使用调试模式
需求:在开发Android程序时,大家一直是使用ShowMessage.其实XE是支持下断点的. 操作: 1.小米手机用USB线,连到电脑上. 2.小米手机-设置-关于手机-"MIUI版本& ...
- CodeForces 706C Hard problem (水DP)
题意:对于给定的n个字符串,可以花费a[i] 将其倒序,问是否可以将其排成从大到小的字典序,且花费最小是多少. 析:很明显的水DP,如果不是水DP,我也不会做.... 这个就要二维,d[2][max ...
- 001_bytearray
bytearray([source [, encoding [, errors]]]) 中文说明: bytearray([source [, encoding [, errors]]])返回一个byt ...