实现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. (转)MySQL 日志组提交

    原文:https://jin-yang.github.io/post/mysql-group-commit.html 组提交 (group commit) 是为了优化写日志时的刷磁盘问题,从最初只支持 ...

  2. Robot Framework(Databaselibrary库操作)

    1.安装 DatabaseLibrary 库 DatabaseLibrary 下载地址:https://pypi.python.org/pypi/robotframework-databaselibr ...

  3. HTML基础笔记

    html基础 (1)什么是html?超文本标记语言 用于开发网页的语言,由浏览器解释执行 (2)html文件的基本结构 <html> <head> <title>& ...

  4. javascript记住用户名和登录密码

    javascript记住用户名和登录密码 下面主要通过代码给大家展示下javascript记住用户名和登录密码,具体代码内容请看下文. <script type="text/javas ...

  5. 关于在真实物理机器上用cloudermanger或ambari搭建大数据集群注意事项总结、经验和感悟心得(图文详解)

    写在前面的话 (1) 最近一段时间,因担任我团队实验室的大数据环境集群真实物理机器工作,至此,本人秉持负责.认真和细心的态度,先分别在虚拟机上模拟搭建ambari(基于CentOS6.5版本)和clo ...

  6. Struts中Validate()和validateXxx的使用

    Struts中Validate()和validateXxx的使用 学习struts2之后,你会发现validate在之前是没有的!它是怎么实现的呢? validate和validateXxxx都是拦截 ...

  7. 【debian】解决debian中文安装后出现乱码的问题

    由于安装debian选择语言时选择了简体中文安装,但内核没有中文字库,导致某些字符显示为乱码(菱形,方块). 解决办法: 普通用户如果没有设置sudo权限,首先切换到root权限.然后: apt-ge ...

  8. MySQL按照月进行统计

    MySQL按照月进行统计 今天需要后台提供一个按月统计的API.所以查了一下SQL语句的实现方法. 按月统计SQL select date_format(createtime, '%Y-%m') as ...

  9. (C++学习)关于CString的一些疑问

    #include <iostream> #include <string> #include <afx.h> #include <vector> usi ...

  10. 关于逻辑删除标识字段value的设定

    为了容易记忆,项目里所有表记录的逻辑删除可以用“-1”(或其他值)来表示.