渐变显示渐变消失的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. JQCloud: 一个前端生成美化标签云的简单JQuery插件

    本文原文地址:https://jiang-hao.com/articles/2018/blog-JQCloud.html 因为博客需要,发现了一个生成美化简约风格的标签云的JQuery插件. 官网地址 ...

  2. 机器学习入门学习笔记:(一)BP神经网络原理推导及程序实现

    机器学习中,神经网络算法可以说是当下使用的最广泛的算法.神经网络的结构模仿自生物神经网络,生物神经网络中的每个神经元与其他神经元相连,当它“兴奋”时,想下一级相连的神经元发送化学物质,改变这些神经元的 ...

  3. java面试⑤前端部分

    2.4.1 简单说一下HTML,CSS,javaScript在网页开发中的定位? 2.4.2简单介绍一下AJAX 2.4.3 JS和JQuery的关系 2.4.4 JQuery的常用选择器 2.4.5 ...

  4. linux下在线升级nodejs

    因现有项目需要用一个截屏node包,此包必须新版本,所以紧急升级下测试环境nodejs,后续再升级线上,小小试了下node在线升级 方案1,使用npm安装n模块,使用n来升级nodejs 首先要知道n ...

  5. Linux中Redis的安装

    一.下载redis redis官网地址:http://www.redis.io/ 下载地址:http://download.redis.io/releases/ redis中文文档地址:http:// ...

  6. PHP 类与对象 全解析(三)

    目录 PHP 类与对象 全解析( 一) PHP 类与对象 全解析( 二) PHP 类与对象 全解析(三 ) 13.魔术方法 定义:PHP把所有以__(两个下划线)开头的类方法当成魔术方法     __ ...

  7. C# 小软件部分(二)

     此次又新增了一些新的功能,直接接着上次的介绍吧 上次博客介绍地址:http://www.cnblogs.com/Liyuting/p/8540592.html 这次新增了三个功能,具体如下: 一.网 ...

  8. 【拓扑 && 模板】Kosaraju算法

    #include<bits/stdc++.h> using namespace std; ; vector <int> g1[maxn],g2[maxn]; stack < ...

  9. 【原】Shiro框架基础搭建[2]

    简介: 关于搭建一个最基础的shiro网上的例子有很多,这里是记录一下自己尝试去看官方文档所搭建的一个小demo,项目采用的是原始的java静态工程,导入相关jar包后就能运行. 首先进入官网http ...

  10. Spring系列之——springboot解析resources.application.properties文件

    摘要:本文通过讲解如何解析application.properties属性,介绍了几个注解的运用@Value @ConfigurationProperties @EnableConfiguration ...