iOS实现自定义的弹出视图(popView)
2017-10-11 08:28
525 查看
前段时间,在项目中有个需求是支付完成后,弹出红包,实现这么一个发红包的功能。做了最后,实现的效果大致如下:
效果图
一、使用方法
整个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
由于之前写的比较仓促,今天趁着空余时间又把代码整理了一遍,比如关闭之后的回调,之前用delegate实现的,今天又用block重新写的,简洁一点吧,另外基本上所有的方法、属性、枚举我都有注释,算是个个人习惯吧。这里面有几点需要说明的是:
1.ShadeBackgroundType是蒙板的背景色属性,有固定的和渐变的(ShadeBackgroundTypeGradient),关于这个渐变,有兴趣的可以研究一下CAGradientLayer,还是很有趣的,在后来的文章中也会说到。
2.tapOutsideToDismiss这个是设置点击蒙板,popView消失不消失的属性,默认的是YES
3.- (void)closeWithBlcok:(void(^)())complete这个方法,是关闭后的回调,比如说发送红包以后,等popView消失以后回到上一页的这种。
由于注释的基本都很清楚了,这里就不多说了,
三、最后
我一般写博客的时候,贴代码喜欢贴全部的代码,我认为这样会直观一点(当然非常多的除外),最后,所有的代码demo都可以在 这里 看到! https://github.com/loveway/HWPopTool#fork-destination-box
效果图
一、使用方法
整个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消失以后回到上一页的这种。
由于注释的基本都很清楚了,这里就不多说了,
三、最后
我一般写博客的时候,贴代码喜欢贴全部的代码,我认为这样会直观一点(当然非常多的除外),最后,所有的代码demo都可以在 这里 看到! https://github.com/loveway/HWPopTool#fork-destination-box
相关文章推荐
- iOS实现自定义的弹出视图(popView)
- iOS实现自定义的弹出视图(popView)
- iOS实现自定义的弹出视图(popView)
- 两行代码搞定iOS自定义HUD风格动画弹窗(支持选择记录) - SKChoosePopView的使用和实现思路
- iOS实现自定义的弹出视图(HWPopTool)
- iOS自定义提示弹出框实现类似UIAlertView的效果
- iOS 5 编程-实现自定义的选择器视图(UIPickerView)
- 【iOS开发-24】导航控制器下不同视图控制器之间切换:利用CATrasition和view的layer层来实现自定义的动画效果
- iOS开发导航控制器下不同视图控制器之间切换:利用CATrasition和view的layer层来实现自定义的动画效果
- iOS自定义提示弹出框实现类似UIAlertView的效果
- iOS - PopView简单弹出视图:
- [iOS]UIScrollview自定义分页的实现方法
- 自定义View和属性动画ValueAnimator实现圆点指示器——支持“纵向视图”
- 猫猫学iOS(四十四)之网易彩票自定义图片在右边的Button_弹出view_ios6,7简单适配
- ios学习笔记----实现一个带滑动手势的tabBarViewController,并可自定义tabBar
- iOS自定义弹出视图,以及简单的手势讲解
- iOS自定义弹出框(UIAleartView)
- iOS编程-------自定义视图CustomView 封装视图LTView 视图控制器UIViewController
- IOS 表视图(UITableVIew)的使用方法(4)自定义表视图单元
- ios开发学习-弹出视图(Popup View) 效果源码分享--系列教程2