日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

实现UILabel渐变色效果

發布時間:2025/3/20 编程问答 33 豆豆
生活随笔 收集整理的這篇文章主要介紹了 实现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渐变色效果的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。