渐变显示渐变消失的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 = ;
}
return self;
} - (void)addToView
{
// 添加到view中
[_inView addSubview:self]; // 开始执行动画
NSTimeInterval duration = 0.2;
if (_startDuration > ) {
duration = _startDuration;
}
[UIView animateWithDuration:duration animations:^{
self.alpha = .f;
}];
} - (void)removeSelf
{
// 开始执行动画
NSTimeInterval duration = 0.2;
if (_endDuration > ) {
duration = _endDuration;
}
[UIView animateWithDuration:duration animations:^{
self.alpha = .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:self
action:@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:.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:nil
afterDelay:.f];
} - (void)affterDelay
{
// 隐藏
[self.view removeBackgroundView];
} @end

以下是需要注意的地方:

渐变显示渐变消失的BackgroundView的更多相关文章

  1. js渐变显示渐变消失

    以下是渐变的js代码(表示多余三行的要隐藏,点击"more"显示剩下的,点击“less”要逐渐隐藏): function showAccomplishmentTableRow(){ ...

  2. 功能整合(一):滚动条的变相隐藏、js控制div的渐变显示、滚动条监听

    1.滚动条的变相隐藏 思路: 1.  把body的横向,纵向的超出部分隐藏,宽设置100%:高设置100%.就没有body的滚动条了, 2.  然后把最外层的div的宽设置的比body的宽宽一点,把d ...

  3. WPF在3D Cad模型中利用TextureCoordinates实现颜色渐变显示偏差值的变化

    原文:WPF在3D Cad模型中利用TextureCoordinates实现颜色渐变显示偏差值的变化 注:最近在做3D机械模型重建方面的软件,需要根据光栅传感器采集的数据绘制3D图形,并显示出色差以及 ...

  4. CSS3渐变——径向渐变

    上节在<再说CSS3渐变——线性渐变>和大家一起学习了CSS3 Gradient中径向渐变最新语法(称得上是W3C的标准语法)相关知识以及其基本使用.今天我们在这一篇中主要和大家一起来了解 ...

  5. [js高手之路] html5 canvas系列教程 - 线形渐变,径向渐变与阴影设置

    接着上文[js高手之路] html5 canvas系列教程 - 像素操作(反色,黑白,亮度,复古,蒙版,透明)继续. 一.线形渐变 线形渐变指的是一条直线上发生的渐变. 用法: var linear ...

  6. 使用jquery实现文本框输入特效:文字逐个显示逐个消失反复循环

    前两天看到某个网站上的输入框有个小特效:文字逐个显示,并且到字符串最大长度后,逐个消失,然后重新循环显示消失,循环显示字符串数组.我对这个小特效有点好奇,于是今天自己尝试用jquery写一个简单的小d ...

  7. 【转】CSS设置DIV背景色渐变显示

     [原链接]http://www.2cto.com/kf/201310/248187.html <style type="text/css">     .linear{ ...

  8. CSS设置DIV背景色渐变显示

    本文转载自:http://blog.csdn.net/gingerredjade/article/details/12191741 <style type="text/css" ...

  9. jqury+animation+setTimeOut实现渐变显示与隐藏动画

    初始效果 实现效果 1,编写HTMl结构代码 <div class="box"> <i class="icon"></i> ...

随机推荐

  1. facebook 摘要生成阅读笔记(二) Abstractive Sentence Summarization with Attentive Recurrent Neural Networks

    整体流程与第一篇差不多,只是在encoder和decoder加入了RNN Encoder: 1. ai=xi+li ai=词向量+词在序列中的位置信息(相当于一个权重,[M, 1]) 流程: 先是CN ...

  2. mongodb-添加或删除字段

    1 .添加一个字段.  url 代表表名 , 添加字段 content. 字符串类型. db.url.update({}, {$set: {content:""}}, {multi ...

  3. ABP-JavaScript API (转)

    转自:http://www.cnblogs.com/zd1994/p/7689164.html 因经常使用,备查 一.AJAX 1,ABP采用的方式 ASP.NET Boilerplate通过用abp ...

  4. ARP地址解析协议原理

    概述 网络层以上的协议用IP地址来标识网络接口,但以太数据帧传输时,以物理地址来标识网络接口.因此我们需要进行IP地址与物理地址之间的转化. 对于IPv4来说,我们使用ARP地址解析协议来完成IP地址 ...

  5. 不会几个框架,都不好意思说搞过前端: Node.js & angular.js

    Node.js  菜鸟教程 :http://www.runoob.com/nodejs/nodejs-install-setup.html angular.js  菜鸟教程 :http://www.r ...

  6. [转] Hadoop 2.0 详细安装过程

    1. 准备 创建用户 useradd hadoop passwd hadoop 创建相关的目录 定义代码及工具存放的路径 mkdir -p /home/hadoop/source mkdir -p / ...

  7. MVC部分视图

    // 以视图名使用当前文件夹下的视图 // 如果没有找到,则搜索 Shared 文件夹 @Html.Partial("ViewName")   @Html.Partial(“视图” ...

  8. CSS Sprite 精灵图

    .bg_sprite{background-image:url(/整图地址); background-repeat:no-repeat} 引用该类 .. 然后在元素中逐一定义背景坐标 .. 以下为关键 ...

  9. Xshell基础入门

    启动 双击快捷方式,启动 新建回话 建立连接 在这里需要填写的是: 1. 连接名称 2. 服务器IP 3. 服务器端口(默认22) 填写完毕后,点击确定,保存配置,回到连接页面,可以看到多了一个测试服 ...

  10. Java的工厂模式(三)

    除了一般的工厂模式之外,还有抽象工厂模式,抽象工厂模式更强调产品族的概念,一个具体工厂生产出来的系列商品都是一个产品族的. 假设我们有两个具体工厂,分别是袋装水果工厂和罐装水果工厂,它们都能生产苹果和 ...