实现UILabel渐变色效果
实现UILabel渐变色效果

效果如下图:


源码:
//
// CombinationView.h
// ChangeColorLabel
//
// Created by YouXianMing on 14/11/15.
// Copyright (c) 2014年 YouXianMing. All rights reserved.
// #import <UIKit/UIKit.h> @interface CombinationView : UIView /**
* 上面的view与下面的view
*/
@property (nonatomic, strong) UIView *bottomView;
@property (nonatomic, strong) UIView *aboveView; /**
* 上面view的透明度
*/
@property (nonatomic, assign) CGFloat aboveViewAlpha; @end
//
// CombinationView.m
// ChangeColorLabel
//
// Created by YouXianMing on 14/11/15.
// Copyright (c) 2014年 YouXianMing. All rights reserved.
// #import "CombinationView.h" typedef enum : NSUInteger {
Above_View = 0x11,
Bottom_View,
} ENUM_VIEW; @implementation CombinationView #pragma mark - 上面的view与下面的view
@synthesize bottomView = _bottomView;
@synthesize aboveView = _aboveView;
@synthesize aboveViewAlpha = _aboveViewAlpha; - (void)setBottomView:(UIView *)bottomView {
self.bounds = bottomView.bounds;
bottomView.frame = bottomView.bounds;
_bottomView = bottomView; _aboveView.tag = Above_View;
_bottomView.tag = Bottom_View; [self addSubview:bottomView];
[self bringSubviewToFront:[self viewWithTag:Above_View]];
}
- (UIView *)bottomView {
return _bottomView;
} - (void)setAboveView:(UIView *)aboveView {
self.bounds = aboveView.bounds;
aboveView.frame = aboveView.bounds;
_aboveView = aboveView; _aboveView.tag = Above_View;
_bottomView.tag = Bottom_View; [self addSubview:aboveView];
[self bringSubviewToFront:[self viewWithTag:Above_View]];
}
- (UIView *)aboveView {
return _aboveView;
} - (void)setAboveViewAlpha:(CGFloat)aboveViewAlpha {
_aboveView.alpha = aboveViewAlpha;
}
- (CGFloat)aboveViewAlpha {
return _aboveView.alpha;
} @end
显示时候的源码:
//
// ViewController.m
// ChangeColorLabel
//
// Created by YouXianMing on 14/11/15.
// Copyright (c) 2014年 YouXianMing. All rights reserved.
// #import "ViewController.h"
#import "CombinationView.h" @interface ViewController () @property (nonatomic, strong) NSTimer *timer;
@property (nonatomic, strong) CombinationView *tmpView; @end @implementation ViewController - (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor blackColor]; // 普通label
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(, , , )];
label.center = self.view.center;
label.textAlignment = NSTextAlignmentCenter;
label.font = [UIFont fontWithName:@"HelveticaNeue-Thin" size:];
label.text = @"YouXianMing";
label.textColor = [UIColor whiteColor]; // 截图
UIView *snapShot = [label snapshotViewAfterScreenUpdates:YES]; // 更新的label
label.textColor = [UIColor redColor]; // 组合器
self.tmpView = [CombinationView new];
self.tmpView.aboveView = label;
self.tmpView.bottomView = snapShot;
self.tmpView.center = self.view.center; // 添加view
[self.view addSubview:self.tmpView]; // 定时器
_timer = [NSTimer scheduledTimerWithTimeInterval:3.5f
target:self
selector:@selector(doAnimation)
userInfo:nil
repeats:YES];
} - (void)doAnimation {
// 做动画测试
[UIView animateWithDuration:1.5 animations:^{
self.tmpView.aboveViewAlpha = .f;
} completion:^(BOOL finished) {
[UIView animateWithDuration:1.5 animations:^{
self.tmpView.aboveViewAlpha = .f;
} completion:^(BOOL finished) { }];
}];
} @end
手机图片源码:
//
// ViewController.m
// ChangeColorLabel
//
// Created by YouXianMing on 14/11/15.
// Copyright (c) 2014年 YouXianMing. All rights reserved.
// #import "ViewController.h"
#import "CombinationView.h" @interface ViewController () @property (nonatomic, strong) NSTimer *timer;
@property (nonatomic, strong) CombinationView *tmpView; @end @implementation ViewController - (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor blackColor]; UIImageView *imageView1 = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"iPhone"]];
UIImageView *imageView2 = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"iPhoneOne"]]; // 组合器
self.tmpView = [CombinationView new];
self.tmpView.aboveView = imageView1;
self.tmpView.bottomView = imageView2;
self.tmpView.center = self.view.center; // 添加view
[self.view addSubview:self.tmpView]; // 定时器
_timer = [NSTimer scheduledTimerWithTimeInterval:3.5f
target:self
selector:@selector(doAnimation)
userInfo:nil
repeats:YES];
} - (void)doAnimation {
// 做动画测试
[UIView animateWithDuration:1.5 animations:^{
self.tmpView.aboveViewAlpha = .f;
} completion:^(BOOL finished) {
[UIView animateWithDuration:1.5 animations:^{
self.tmpView.aboveViewAlpha = .f;
} completion:^(BOOL finished) { }];
}];
} @end
实现UILabel渐变色效果的更多相关文章
- 通过CAGradientLayer制作渐变色效果(转)
转载自:http://blog.it985.com/7986.html 看了极客学院的视频之后写的一篇博客,觉得不错,还是作为笔记使用. 简单介绍一下CAGradientLayer吧. Gradien ...
- iOS高仿微信项目、阴影圆角渐变色效果、卡片动画、波浪动画、路由框架等源码
iOS精选源码 iOS高仿微信完整项目源码 Khala: Swift 编写的iOS/macOS 路由框架 微信左滑删除效果的实现与TableViewCell的常用样式介绍 实现阴影圆角并存,渐变色背景 ...
- Android背景渐变色效果
Android设置背景色可以通过在res/drawable里定义一个xml,如下: [代码]xml代码: 1 <?xml version="1.0" encoding=&qu ...
- css实现背景渐变色效果
webkit内核的浏览器,例如(chrome,safari等) background:-webkit-gradient(linear,0 0,0 100%,from(#000000),to(#ffff ...
- 兼容主流浏览器的css渐变色
网页中的渐变色区域,渐变色背景,一般都是通过ps图片方法来实现,但是图片放得多了会影响网页的打开速度,本文介绍的就是用纯 CSS 实现 IE .Firefox.Chrome 和 和Safari都支持的 ...
- iOS - AutoLayout
前言 NS_CLASS_AVAILABLE_IOS(6_0) @interface NSLayoutConstraint : NSObject @available(iOS 6.0, *) publi ...
- asp.net 创建文字特效
相信word 中的 艺术字 功能大家都不陌生.今天, 我们就利用C#来制作几款自己的艺术字, 可能会对我们了解字体图像的制作原理有一些帮助. 至于有没有使用价值我保持沉默. 一. 投影效果 程序运行效 ...
- C#制作艺术字
相信 Word 中的 艺术字 功能大家都不陌生, 前面这个 "Word" 单词就是它所为. 今天, 我们就利用C#来制作几款自己的艺术字, 可能会对我们了解字体图像的制作原理有一 ...
- iOS中常用技术链接
1.弹幕技术 http://www.jianshu.com/p/f39b8abc8008 2.通过CAGradientLayer制作渐变色效果 http://blog.it985.com/7986.h ...
随机推荐
- idea常用快捷汇总
目录 1.查询 2. 自动提示 3. 代码生成 4. 摆脱鼠标 5.源码阅读 1.查询 Shift+ Ctrl + F 全文搜索 Shift + Shift 文件名搜索 Ctrl + F 在当前文件进 ...
- 对Map的一些总结
1:Map接口. Collection体系中存储的是单个元素,单身汉,而Map中存储的是2个元素,存储的是成对的元素. Map和Collection是没有联系的!!不要以为Map是Collection ...
- Android学习系列--App列表之拖拽ListView(下)
接着上篇Android学习系列(10)--App列表之拖拽ListView(上)我们继续实现ListView的拖拽效果. 7.重写onTouchEvent()方法. 在这个方法中我们主要是处理 ...
- 【Javascript】Javascript原型与继承
一切都是对象! 以下的四种(undefined, number, string, boolean)属于简单的值类型,不是对象.剩下的几种情况——函数.数组.对象.null.new Number(10) ...
- 关于new Option()
先来了解下,如何运用js实现select动态添加option. //1.动态创建select function createSelect(){ var mySelect = document.crea ...
- 用.net 2.0(或.net 3.5)开发的程序在.net 4.0的环境中运行的解决方案
引用:.NET2.0程序集无法在.net 4.0 中运行的解决方案 1. 原来是在.net 2.0 (VS2008)的环境下的程序,通过升级到.net 4.0 (VS2013)导致程序运行不了 提示需 ...
- html中超链接的target属性
<a> 标签的 target 属性规定在何处打开链接文档.(target就是目标的意思) 一共有(4+1种选择): 用法:<a target="value"> ...
- javascript typeof 和 instanceof 的区别和联系
这篇文章是我看完<JavaScript高级程序设计(第2版)>书籍的随笔文章,目的只有一个,以备自己和网友们方便参考和记忆! typeof是什么? typeof 是一个操作 ...
- 获取URL中某个参数的值
JS代码: function getQueryString(name){ var reg = new RegExp("(^|&)" + name + "=([^& ...
- [转]Web API Introduction to OData Services using ASP.NET Web API
本文转自:http://mahedee.net/tag/web-api/ What is OData? OData Stands for Open Data Protocol. It is a dat ...