首页 > 代码库 > 实现UILabel渐变色效果

实现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(0, 0, 300, 60)];    label.center        = self.view.center;    label.textAlignment = NSTextAlignmentCenter;    label.font          = [UIFont fontWithName:@"HelveticaNeue-Thin" size:40];    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 = 0.f;    } completion:^(BOOL finished) {        [UIView animateWithDuration:1.5 animations:^{            self.tmpView.aboveViewAlpha = 1.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 = 0.f;    } completion:^(BOOL finished) {        [UIView animateWithDuration:1.5 animations:^{            self.tmpView.aboveViewAlpha = 1.f;        } completion:^(BOOL finished) {                    }];    }];}@end

 

实现UILabel渐变色效果