实现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 Proxy使用

    转自: http://www.cnblogs.com/itech/archive/2011/09/22/2185365.html http://koda.iteye.com/blog/788862 h ...

  2. JS实现OO机制

    一.简单原型机制介绍 继承是OO语言的标配,基本所有的语言都有继承的功能,使用继承方便对象的一些属性和方法的共享,Javascript也从其他OO语言上借鉴了这种思想,当一个函数通过"new ...

  3. Robot Framework常用关键字介绍

    常用关键字介绍 在学习一门编程语言的时候,大多教材都是从打印“hello world”开始.我们可以像编程语言一样来学习 Robot Framework.虽然通过 RIDE 提供“填表”一样的写测试用 ...

  4. ps如何替换有透明图片的颜色

    修改透明图片的颜色 首先用魔棒工具点选颜色区域,然后再在菜单中找到 图像-调整-替换颜色,就可以选任意想要的颜色

  5. C++命名空间使用代码

    namesp.h #pragma once #include <string> namespace pers { using namespace std; struct Person { ...

  6. Node.js学习笔记(一) --- HTTP 模块、URL 模块、supervisor 工具

    一.Node.js创建第一个应用 如果我们使用 PHP 来编写后端的代码时,需要 Apache 或者 Nginx 的 HTTP 服务器, 来处理客户端的请求相应.不过对 Node.js 来说,概念完全 ...

  7. 简单的CRUD(二)

    一.重构简单的CRUD 1.JDBC工具类 1.因为在crud中都包含一些相同的代码所以可以提取出来,抽取代码重构为工具类. 2.将工具类设置为static静态类,方便调用,不需要new对象. pub ...

  8. pycharm下 os.system os.popen执行命令返回有中文乱码

    原文 settings:

  9. Django之路由、模板和模型系统 (转载)

    一.路由系统 浏览器会自动给url后加一个“/” django会自动给路由的正则表达式前面加一个“/” django会给任何不带“/”结尾的url语句添加“/”(可设置) 短路路由规则:匹配到第一条就 ...

  10. Algorithm——最长公共前缀

    一.问题 编写一个函数来查找字符串数组中的最长公共前缀. 如果不存在公共前缀,返回空字符串 "". 示例 1: 输入: ["flower","flow ...