实现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];// 普通labelUILabel *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];// 更新的labellabel.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.5ftarget:selfselector:@selector(doAnimation)userInfo:nilrepeats: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.5ftarget:selfselector:@selector(doAnimation)userInfo:nilrepeats: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?
轉載于:https://www.cnblogs.com/YouXianMing/p/4098850.html
總結
以上是生活随笔為你收集整理的实现UILabel渐变色效果的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: bootstrap-nav属性详解
- 下一篇: Daily Scrum 11.16