渐变显示渐变消失的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> ...
随机推荐
- python笔记09-----装饰器,生成器,迭代器
1.装饰器 定义:本质是函数,(装饰其他函数)就是为其他函数添加附加功能 原则:1.不能修改被装饰的函数的源代码 2.不能修改被装饰的函数的调用方式 实现装饰器的知识储备: 1. 函数即“变 ...
- 机器学习--boosting家族之XGBoost算法
一.概念 XGBoost全名叫(eXtreme Gradient Boosting)极端梯度提升,经常被用在一些比赛中,其效果显著.它是大规模并行boosted tree的工具,它是目前最快最好的开源 ...
- CentOS Maven 删除 *.lastUpdated文件
find . -name "*.lastUpdated" -exec rm -rf {} \;
- C#语法之泛型
前面两篇C#语法主要是回顾委托相关的.这篇主要回顾了泛型. 一.为什么要有泛型? 我们在写一些方法时可能会方法名相同,参数类型不同的方法,这种叫做重载.如果只是因为参数类型不同里面做的业务逻辑都是相同 ...
- Pivot
测试数据 Create Table TPivot ( ID ,), ProductName ), SalesMonth int, SalesCount int ) insert into TPivot ...
- SpringMVC的controller层接收来自jsp页面通过<a href="/user/userUpdateInfo/>的中文乱码问题
这种情况是,jsp页面的中文正常显示,数据的中文也是正常显示,但是在Controller层接收到的中文是乱码,如下图所示: 解决方法:在Controller层对前台传递的中文乱码进行处理,将它转换成u ...
- jstack,jmap,jstat分别的意义
1.Jstack 1.1 jstack能得到运行java程序的java stack和native stack的信息.可以轻松得知当前线程的运行情况.如下图所示 注:这个和thread dump是同 ...
- 动态We API(ABP官方文档翻译)
动态Web API层 创建动态Web API控制器 ForAll方法 重写ForAll ForMethods Http动词 WithVerb方法 HTTP特性 命名约定 API管理器 RemoteSe ...
- Cocoa pod导入第三方框架遇到的那点事儿
废话不多说,直接上干货. 以下所有操作均是在Cocoapod导入第三方库,并且是.xcworkspace的工程里面操作的, 1.导入头文件找不到,也就是所谓的,not find ''AFNetwork ...
- CodeForces765A
A. Neverending competitions time limit per test:2 seconds memory limit per test:512 megabytes input: ...