渐变显示渐变消失的BackgroundView
渐变显示渐变消失的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的更多相关文章
- js渐变显示渐变消失
以下是渐变的js代码(表示多余三行的要隐藏,点击"more"显示剩下的,点击“less”要逐渐隐藏): function showAccomplishmentTableRow(){ ...
- 功能整合(一):滚动条的变相隐藏、js控制div的渐变显示、滚动条监听
1.滚动条的变相隐藏 思路: 1. 把body的横向,纵向的超出部分隐藏,宽设置100%:高设置100%.就没有body的滚动条了, 2. 然后把最外层的div的宽设置的比body的宽宽一点,把d ...
- WPF在3D Cad模型中利用TextureCoordinates实现颜色渐变显示偏差值的变化
原文:WPF在3D Cad模型中利用TextureCoordinates实现颜色渐变显示偏差值的变化 注:最近在做3D机械模型重建方面的软件,需要根据光栅传感器采集的数据绘制3D图形,并显示出色差以及 ...
- CSS3渐变——径向渐变
上节在<再说CSS3渐变——线性渐变>和大家一起学习了CSS3 Gradient中径向渐变最新语法(称得上是W3C的标准语法)相关知识以及其基本使用.今天我们在这一篇中主要和大家一起来了解 ...
- [js高手之路] html5 canvas系列教程 - 线形渐变,径向渐变与阴影设置
接着上文[js高手之路] html5 canvas系列教程 - 像素操作(反色,黑白,亮度,复古,蒙版,透明)继续. 一.线形渐变 线形渐变指的是一条直线上发生的渐变. 用法: var linear ...
- 使用jquery实现文本框输入特效:文字逐个显示逐个消失反复循环
前两天看到某个网站上的输入框有个小特效:文字逐个显示,并且到字符串最大长度后,逐个消失,然后重新循环显示消失,循环显示字符串数组.我对这个小特效有点好奇,于是今天自己尝试用jquery写一个简单的小d ...
- 【转】CSS设置DIV背景色渐变显示
[原链接]http://www.2cto.com/kf/201310/248187.html <style type="text/css"> .linear{ ...
- CSS设置DIV背景色渐变显示
本文转载自:http://blog.csdn.net/gingerredjade/article/details/12191741 <style type="text/css" ...
- jqury+animation+setTimeOut实现渐变显示与隐藏动画
初始效果 实现效果 1,编写HTMl结构代码 <div class="box"> <i class="icon"></i> ...
随机推荐
- java-jdk7-forkjoin带有返回值
来自并发编程网: http://ifeve.com/fork-join-3/ 如果这个任务必须解决一个超过预定义大小的问题,你应该将这个任务分解成更多的子任务,并且用Fork/Join框架来执行这些子 ...
- Nginx使用记录
配置常见解释: ########### 每个指令必须有分号结束.################# #user administrator administrators; #配置用户或者组,默认为no ...
- JAVA 导出 Excel, 将Excel下载到本地
昨天说了如何将数据导出成 excel 的两种方式,今天完善一下将 java 导出(文件在服务器)并下载到本地 1. 触发导出 ajax 代码 $.ajax({ type: "POST&quo ...
- Socket编程 - API
基础知识部分:http://www.cnblogs.com/Jimmy1988/p/7839940.html 1. 基本流程 Process Client Server Comment socket( ...
- Shell脚本编写2------有关变量
shell脚本中变量定义方式十分简单,直接将值赋值给变量较好例如 :name="tuanzhang"注意,变量名和等号之间不能有空格,这可能和你熟悉的所有编程语言都不一样.变量命名 ...
- 使用eclipse 开发lisp
https://www.ibm.com/developerworks/cn/opensource/os-eclipse-lispcusp/ eclipse有一个插件,可以支持lisp的开发,叫cusp ...
- DOM-使用节点
节点类型 DOM规定:整个文档是一个文档节点,每个标签是一个元素节点,元素包含的文本是文本节点,元素的属性是一个属性节点,注释属于注释节点,如此等等: 每个节点都有一个nodeType属性,用于标明节 ...
- VisualVM监控远程阿里云主机
一.前言 使用VisualVM监控远程主机,主要是要在远程主机上部署JMX服务和jstat服务,jstat服务的部署花了我半天的时间,而且,网上的资基本都是缺胳膊少腿的,没有一篇是一个整体(行得通的) ...
- spring boot在intellij idea下整合mybatis可能遇到的问题
org.apache.ibatis.builder.BuilderException: Wrong namespace. Expected 'com.sc.starry_sky.dao.UserMap ...
- JPA注解@GeneratedValue
@GeneratedValue是JPA的标准用法, JPA提供四种标准用法,由@GeneratedValue的源代码可以看出. public enum GenerationType { TABLE, ...