渐变显示渐变消失的BackgroundView

效果如下:

源码:

BackgroundView.h 与 BackgroundView.m

//
//  BackgroundView.h
//  TestHUD
//
//  Created by YouXianMing on 14-9-30.
//  Copyright (c) 2014年 YouXianMing. All rights reserved.
//#import <UIKit/UIKit.h>@interface BackgroundView : UIView@property (nonatomic, assign) NSTimeInterval  startDuration; // 动画开始时持续时间
@property (nonatomic, assign) NSTimeInterval  endDuration;   // 动画结束时持续时间- (instancetype)initInView:(UIView *)view;// 开始附加到view中
- (void)addToView;// 将自己从view中移除掉
- (void)removeSelf;@end
//
//  BackgroundView.m
//  TestHUD
//
//  Created by YouXianMing on 14-9-30.
//  Copyright (c) 2014年 YouXianMing. All rights reserved.
//#import "BackgroundView.h"#ifdef DEBUG
#define BackgroundView_DLog(fmt, ...) NSLog((@"BackgroundView.m:%s:%d" fmt), __PRETTY_FUNCTION__, __LINE__, ##__VA_ARGS__);
#else
#define BackgroundView_DLog(...)
#endif@interface BackgroundView ()@property (nonatomic, weak) UIView   *inView;@end@implementation BackgroundView- (instancetype)initInView:(UIView *)view
{self = [super initWithFrame:view.bounds];if (self) {_inView              = view;self.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.5f];self.alpha           = 0;}return self;
}- (void)addToView
{// 添加到view中[_inView addSubview:self];// 开始执行动画NSTimeInterval duration = 0.2;if (_startDuration > 0) {duration = _startDuration;}[UIView animateWithDuration:duration animations:^{self.alpha = 1.f;}];
}- (void)removeSelf
{// 开始执行动画NSTimeInterval duration = 0.2;if (_endDuration > 0) {duration = _endDuration;}[UIView animateWithDuration:duration animations:^{self.alpha = 0.f;} completion:^(BOOL finished) {[self removeFromSuperview];}];
}- (void)dealloc
{BackgroundView_DLog(@" 安全释放");
}@end

category文件:

//
//  UIView+BackgroundView.h
//  BackgroundView
//
//  Created by YouXianMing on 14-10-3.
//  Copyright (c) 2014年 YouXianMing. All rights reserved.
//#import <UIKit/UIKit.h>
#import "BackgroundView.h"typedef void (^ConfigBackgroundViewBlock)(BackgroundView *configView);@interface UIView (BackgroundView)- (void)showBackgroundViewAndConfig:(ConfigBackgroundViewBlock)block;
- (void)removeBackgroundView;@end
//
//  UIView+BackgroundView.m
//  BackgroundView
//
//  Created by YouXianMing on 14-10-3.
//  Copyright (c) 2014年 YouXianMing. All rights reserved.
//#import "UIView+BackgroundView.h"#define TAG_DATA  0x3d2894@implementation UIView (BackgroundView)- (void)showBackgroundViewAndConfig:(ConfigBackgroundViewBlock)block
{if (self == nil) {return;}BackgroundView *backView = [[BackgroundView alloc] initInView:self];backView.tag = TAG_DATA;block(backView);[backView addToView];
}- (void)removeBackgroundView
{BackgroundView *backView = (BackgroundView *)[self viewWithTag:TAG_DATA];if (backView) {[backView removeSelf];}
}@end

使用的源码:

//
//  ViewController.m
//  BackgroundView
//
//  Created by YouXianMing on 14-10-3.
//  Copyright (c) 2014年 YouXianMing. All rights reserved.
//#import "ViewController.h"
#import "UIView+BackgroundView.h"@interface ViewController ()@end@implementation ViewController- (void)viewDidLoad {[super viewDidLoad];// 添加手势UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:selfaction:@selector(tapEvent:)];[self.view addGestureRecognizer:tap];UILabel *label      = [[UILabel alloc] initWithFrame:self.view.bounds];label.text          = @"YouXianMing";label.textAlignment = NSTextAlignmentCenter;label.font          = [UIFont fontWithName:@"HelveticaNeue-Thin" size:40.f];label.textColor     = [UIColor redColor];[self.view addSubview:label];
}- (void)tapEvent:(UITapGestureRecognizer *)tap
{// 显示[self.view showBackgroundViewAndConfig:^(BackgroundView *configView) {configView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.6];configView.startDuration = 0.4f;configView.endDuration   = 0.4f;}];// 延迟3s执行[self performSelector:@selector(affterDelay)withObject:nilafterDelay:3.f];
}- (void)affterDelay
{// 隐藏[self.view removeBackgroundView];
}@end

以下是需要注意的地方:

渐变显示渐变消失的BackgroundView相关推荐

  1. JQuery 渐变显示隐藏动画 ( fadeIn(),fadeOut(),fadeTo() )

    $('#div1').fadeIn(1000);  //渐变显示.1000毫秒,表示动画显示过程时间. $('#div1').fadeOut(1000);  //渐变隐藏 $('#div1').fad ...

  2. gradient设置上下渐变_CSS3 渐变

    CSS3 渐变(Gradients) 渐变效果 CSS3 渐变(gradients)可以让你在两个或多个指定的颜色之间显示平稳的过渡. 以前,你必须使用图像来实现这些效果.但是,通过使用 CSS3 渐 ...

  3. html渐变色css3渐变,css3渐变

    渐变 在两个或者多个指定的颜色之间显示平稳的过渡.渐变的效果是由浏览器生成的.渐变的类型主要分为两种:linear-gradient(线性渐变)和radial-gradient(径向渐变) 线性渐变是 ...

  4. html设置一个正方形渐变,CSS3 渐变

    CSS3 渐变(Gradients) 渐变效果 CSS3 渐变(gradients)可以让你在两个或多个指定的颜色之间显示平稳的过渡. 以前,你必须使用图像来实现这些效果.但是,通过使用 CSS3 渐 ...

  5. gradient设置上下渐变_CSS3 渐变(Gradients)

    CSS3 渐变(Gradients) CSS3渐变功能使您可以创建从一种颜色到另一种颜色的渐变,而无需使用任何图像. 使用CSS3渐变 CSS3渐变功能提供了一种灵活的解决方案,可以在两种或多种颜色之 ...

  6. css 两边到中间 渐变_CSS3 渐变

    CSS3 渐变 渐变(gradients)可以在两个或多个指定的颜色之间显示平稳的过渡. 兼容性:IE10+,Chrome26+,FireFox16+,Safari6.1+,Opera12.1+ CS ...

  7. (七)渐变 矩形渐变 放射渐变

    createLinearGradient() [4个参数,1起点x坐标,2起点y坐标,3终点x 4终点y] addColorStop(0,"white")[接收两个参数,1开始渐变 ...

  8. ProgressBar(圆形、水平进度条)的基本使用(点击Button1按钮,显示或消失圆形ProgressBar)(每点击一次Button2按钮,水平进度条增加10%)

    ProgressBar用于在界面上显示一个进度条,表示我们的程序正在加载一些数据.它的用法也非常简单. 首先它有两种形式,一种是圆形进度条,另一种是水平进度条. 效果图: activity_main. ...

  9. CSS3 渐变: 线性渐变 linear-gradient 和 径向渐变 radial-gradient

    渐变背景也是CSS3 的新特性,通过它可以在不使用图像的情况下,创建从一种颜色到另一种颜色的过渡.使用渐变的好处是,可以减少对图像的依赖,进而降低浏览网页时的带宽消耗,提高网站的访问速度. 渐变,就是 ...

最新文章

  1. java表或视图不存在_Error querying database. Cause: java.sql.SQLSyntaxErrorException: ORA-00942: 表或视图不存在...
  2. tomcat部署下的web项目网页更改不能自动刷新
  3. 【NOIP2007】第三题·守望者的逃离
  4. c++ stl 标准库容器的选择原则(图)
  5. Linux用户管理与权限
  6. hdoj-3342-Legal or Not(拓扑排序)
  7. 在Docker上部署NGINX和NGINX Plus
  8. 拼多多上架专供湖北平价口罩 每天300万只
  9. 单例对象会被jvm的gc时回收吗_【PHP设计模式】单例模式
  10. AMD显卡超频显示器方法
  11. 腾讯云国际版注册流程详解
  12. 如何根据DBC计算CAN与CANFD的负载率
  13. 比肩Sci-hub的论文下载神器——Library Genesis
  14. PyTorch基础:数据处理(数据加载、GPU加速)
  15. 河北源达靠谱吗?股市如战场,可靠的选股软件很关键
  16. Drupal9.1.8通过phpStudy安装后除首页其他页面均404处理
  17. 包载羟基喜树碱的聚乙二醇化聚十六烷基氰基丙烯酸酯(PEG-PHDCA)纳米囊泡
  18. vue使用WangEditor富文本编辑器(批量上传图片到服务器)
  19. 各种门平面图画法_cad门的画法_用CAD画门的平面图的方法步骤图
  20. 服务器许可证密钥,如何生成和验证软件许可证密钥?

热门文章

  1. ^_^ 真是Android Framework的BUG
  2. C#调用DataV token代码
  3. yii2的model数据库配置以及应用(主从数据库配置)
  4. 【程序员归家计划】放假回家之前拜服务器?不存在的,这才是保证程序员过好年的正确打开方式...
  5. How can I pretty-print JSON in python?
  6. Java基础部分笔记----------网络编程
  7. 一个关于思科路由器ping的有趣现象
  8. SQL Server 镜像构建
  9. 企业的失败,是一种必然
  10. 图文详解Silverlight访问MSSQL数据库