实现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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值