UILabel混合顯示動畫效果

UILabel混合顯示動畫效果git

 

效果github

 

源碼dom

https://github.com/YouXianMing/Animations動畫

//
//  MixedColorProgressViewController.m
//  Animations
//
//  Created by YouXianMing on 16/1/5.
//  Copyright © 2016年 YouXianMing. All rights reserved.
//

#import "MixedColorProgressViewController.h"
#import "UIView+SetRect.h"
#import "GCD.h"

@interface MixedColorProgressViewController ()

@property (nonatomic, strong)  UIView   *upView;
@property (nonatomic, strong)  UILabel  *upLabel;
@property (nonatomic, strong)  UIView   *downView;
@property (nonatomic, strong)  UILabel  *downLabel;

@property (nonatomic, strong)  GCDTimer *timer;

@end

@implementation MixedColorProgressViewController

- (void)setup {

    [super setup];
    
    /*
     給upView的frame值作動畫纔是label可以混色顯示的核心
     
     upView(紅色背景)   ===>  upLabel(白色底字)
     |                       |
     |                       |
     |                       |
     |                       |
     downView(白色背景) ===> downLabel(紅色底字)
     
     */
    
    // 上面一層
    {
        // 紅色背景
        _upView                     = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 220, 17)];
        _upView.center              = self.view.center;
        _upView.layer.cornerRadius  = 2.f;
        _upView.backgroundColor     = [UIColor redColor];
        _upView.layer.masksToBounds = YES; // 核心(不讓subview顯示超出範圍)
        [self.view addSubview:_upView];
        
        // 白色底字
        _upLabel                    = [[UILabel alloc] initWithFrame:_upView.bounds];
        _upLabel.font               = [UIFont fontWithName:@"HelveticaNeue-Thin" size:13];
        _upLabel.text               = @"YouXianMing - iOS Programmer";
        _upLabel.textColor          = [UIColor whiteColor];
        _upLabel.textAlignment      = NSTextAlignmentCenter;
        [_upView addSubview:_upLabel];
    }
    
    // 下面一層
    {
        // 白色背景
        _downView                     = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 220, 17)];
        _downView.center              = self.view.center;
        _downView.layer.cornerRadius  = 2.f;
        _downView.backgroundColor     = [UIColor whiteColor];
        [self.view addSubview:_downView];
        
        // 紅色底字
        _downLabel                    = [[UILabel alloc] initWithFrame:_downView.bounds];
        _downLabel.textColor          = [UIColor redColor];
        _downLabel.font               = [UIFont fontWithName:@"HelveticaNeue-Thin" size:13];
        _downLabel.text               = @"YouXianMing - iOS Programmer";
        _downLabel.textAlignment = NSTextAlignmentCenter;
        [_downView addSubview:_downLabel];
    }
    
    // 顯示上面一層
    [self.view bringSubviewToFront:_upView];
    
    // 給上面一層的frame值作動畫
    _timer = [[GCDTimer alloc] initInQueue:[GCDQueue mainQueue]];
    [_timer event:^{
        
        [UIView animateWithDuration:0.5f delay:0.f usingSpringWithDamping:3.f initialSpringVelocity:0 options:0 animations:^{
            
            _upView.width = arc4random() % 220;
            
        } completion:nil];
        
    } timeInterval:NSEC_PER_SEC];
    [_timer start];
}

@end

 

細節atom

相關文章
相關標籤/搜索