实现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渐变色效果的更多相关文章

  1. 通过CAGradientLayer制作渐变色效果(转)

    转载自:http://blog.it985.com/7986.html 看了极客学院的视频之后写的一篇博客,觉得不错,还是作为笔记使用. 简单介绍一下CAGradientLayer吧. Gradien ...

  2. iOS高仿微信项目、阴影圆角渐变色效果、卡片动画、波浪动画、路由框架等源码

    iOS精选源码 iOS高仿微信完整项目源码 Khala: Swift 编写的iOS/macOS 路由框架 微信左滑删除效果的实现与TableViewCell的常用样式介绍 实现阴影圆角并存,渐变色背景 ...

  3. Android背景渐变色效果

    Android设置背景色可以通过在res/drawable里定义一个xml,如下: [代码]xml代码: 1 <?xml version="1.0" encoding=&qu ...

  4. css实现背景渐变色效果

    webkit内核的浏览器,例如(chrome,safari等) background:-webkit-gradient(linear,0 0,0 100%,from(#000000),to(#ffff ...

  5. 兼容主流浏览器的css渐变色

    网页中的渐变色区域,渐变色背景,一般都是通过ps图片方法来实现,但是图片放得多了会影响网页的打开速度,本文介绍的就是用纯 CSS 实现 IE .Firefox.Chrome 和 和Safari都支持的 ...

  6. iOS - AutoLayout

    前言 NS_CLASS_AVAILABLE_IOS(6_0) @interface NSLayoutConstraint : NSObject @available(iOS 6.0, *) publi ...

  7. asp.net 创建文字特效

    相信word 中的 艺术字 功能大家都不陌生.今天, 我们就利用C#来制作几款自己的艺术字, 可能会对我们了解字体图像的制作原理有一些帮助. 至于有没有使用价值我保持沉默. 一. 投影效果 程序运行效 ...

  8. C#制作艺术字

    相信 Word  中的 艺术字 功能大家都不陌生, 前面这个 "Word" 单词就是它所为. 今天, 我们就利用C#来制作几款自己的艺术字, 可能会对我们了解字体图像的制作原理有一 ...

  9. iOS中常用技术链接

    1.弹幕技术 http://www.jianshu.com/p/f39b8abc8008 2.通过CAGradientLayer制作渐变色效果 http://blog.it985.com/7986.h ...

随机推荐

  1. Java框架-mybatis03使用注解实现mybatis

    1.面向接口编程: 好处:扩展性好,分层开发中,上层不用管具体的实现,都遵循共同的标准,使得开发变得容易.规范性更好 2.注解的实现 a)编写Dao接口 public interface UserDa ...

  2. 在VM虚拟机中彻底删除Linux系统

    前言:很久之前安装了Linux虚拟系统,然后用户名忘记了,想着重新安装个Ubuntu系统,就想着删除以前的系统. 删除方法如下: 1.点击打开该Linux系统. 2. 点击虚拟机的左上方“虚拟机”-& ...

  3. Git学习系列之Git是什么?

    前言 现在主流IDE里,都集成git了. https://git-scm.com/docs 史上最浅显易懂的Git教程! 为什么要编写这个教程?因为我在学习Git的过程中,买过书,也在网上Google ...

  4. linux系统下图片的路径

    1. 图片跟网页或者程序在同一目录下 直接 src="abc.jpg" 如果不行 就加多一个斜杠 src="/abc.jpg"

  5. vmstat命令——监控给定时间间隔的服务器的状态值

    vmstat n m 时间间隔为n秒,采集m组数据vmstat n     时间间隔为n秒 # vmstat 2 3 procs -----------memory---------- ---swap ...

  6. step6: item与pipeline

    目的:提取内容进行格式化输出,类似于字典 编写item文件 class JobBoleArticleItem(scrapy.Item): title = scrapy.Field() #支持传进任何数 ...

  7. java中四种引用类型(对象的强、软、弱和虚引用)

    对象的强.软.弱和虚引用在JDK 1.2以前的版本中,若一个对象不被任何变量引用,那么程序就无法再使用这个对象.也就是说,只有对象处于可触及(reachable)状态,程序才能使用它.从JDK 1.2 ...

  8. RabbitMQ - 任务队列

    这次我们试着实现这样一个小程序: 嗯,就是任务队列(task queue).不是将任务集中在一堆并一直等到所有任务一并完成为止,而是将每一个任务封装为一个消息,并将其发送到队列,后台的workers就 ...

  9. JMS - 基于JMS的RPC

    现在试试通过JMS,在应用程序之间发送消息.先看看spring提供的RPC方案(其实还有其他方案,只是没见过谁用).需要使用到这两个类:·org.springframework.jms.remotin ...

  10. JVM(一)

    Java 环境 Java 运行过程 下面几张图,我们可以了解到 Java 这门语言是如何进行运行的. java文件通过编译器编译成class文件,然后在虚拟机中转化为机器语言运行在机器上. 上图展示了 ...