iOS 循环渐变的Label标签

在工程的一些展示中,特别是广告、公告之类的动画显示中,往往会用到循环渐变的Label标签。

如果将一个数组赋给Label标签,Label标签就动态循环的展示这些数组中的每个值,像播放一个视频一样,是不是特别棒呢。

废话不多说,直接进入实现,直接上干货。。。


创建一个继承于UILabel类的Label类

@interface Label : UILabel


@end


搞两个属性,一个接收数组,一个设置动画延迟时间

@property(nonatomic,retain) NSArray *wordList;

@property(nonatomic,assign) double duration;


再搞个实现循环渐变的方法

- (void)animateWithWords:(NSArray *)words forDuration:(double)time;


下面就要到Label.m里去实现啦。实现方法就直接粘贴如下:

#import "Label.h"


@implementation Label

@synthesize wordList = _wordList;

@synthesize duration = _duration;

- (void)animateWithWords:(NSArray *)words forDuration:(double)time {

    self.duration = time;

    if(self.wordList){

        self.wordList = nil;

    }

    self.wordList = [[NSArray alloc] initWithArray:words];

    self.text = [self.wordList objectAtIndex:0];

    [NSThread detachNewThreadSelector:@selector(_startAnimations:) toTarget:self withObject:self.wordList];

}


- (void) _startAnimations:(NSArray *)images {

    for (uint i = 1; i < [images count]; i++) {

        sleep(self.duration);

        [self performSelectorOnMainThread:@selector(_animate:)withObject:[NSNumber numberWithInt:i] waitUntilDone:YES];

        sleep(self.duration);

        if (i == [images count] - 1) {

            i = -1;

        }

    }

}


//动作

- (void) _animate:(NSNumber*)num {

    

    [UIView animateWithDuration:self.duration/2 animations:^{

        self.alpha = 0.0;

    } completion:^(BOOL finished) {

        

        [UIView animateWithDuration:self.duration/2 animations:^{

            self.alpha = 1.0;

            self.text = [self.wordList objectAtIndex:[num intValue]];

        } completion:^(BOOL finished) {

            

        }];

    }];

}

@end


接着就可以导入Label.h头文件,创建能够循环渐变的Label标签啦

#import "Label.h"


IBOutlet Label *label;


[label animateWithWords:@[@"山西",@"河南",@"河北",@"内蒙古",@"西藏",@"浙江"] forDuration:1.0f];


OK,搞定。效果杠杠的。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值