IOS使用定时器循环展示中奖名单

利用定时器(NSTimer)和 UIScrollView 循环展示文本

即粘即用,效果如下图(不会截动图),但是一定会动(礼貌而又不失尴尬的微笑)

你仅需要复制下面的代码后,初始化LotteryScrollView和arrData。

并且调用以下两个方法

-(void)initView;

-(void)shutdown;//退出页面时,一定要调用这个方法,不然定时器不会自动自动释放。

IOS使用定时器循环展示中奖名单_第1张图片

新建LotteryScrollView.h文件,代码如下


#import@interface LotteryScrollView : UIView{

NSTimer          *timer;

UIScrollView      *scrollViewText;

}

@property (nonatomic ,strong) NSArray *arrData;

-(void)initView;

-(void)shutdown;

@end


新建LotteryScrollView.m文件,代码如下


#import "LotteryScrollView.h"

@implementation LotteryScrollView

#pragma mark - Class define variable

#define K_MAIN_VIEW_SCROLL_HEIGHT 80.0f

#define K_MAIN_VIEW_SCROLL_TEXT_TAG 300

#define K_MAIN_VIEW_TEME_INTERVAL 0.35        //计时器间隔时间(单位秒)

#define K_MAIN_VIEW_SCROLLER_SPACE 20          //每次移动的距离

#define K_MAIN_VIEW_SCROLLER_LABLE_WIDTH  300  //字体宽度

#define K_MAIN_VIEW_SCROLLER_LABLE_MARGIN 20  //前后间隔距离

-(id)initWithFrame:(CGRect)frame

array:(NSArray*)array{

self = [super initWithFrame:frame];

self.arrData = array;

return self;

}

//初始化数据

-(void) initView{

//文字滚动

[self initScrollText];

}

//文字滚动初始化

-(void) initScrollText{

//获取滚动条

scrollViewText = (UIScrollView *)[self viewWithTag:K_MAIN_VIEW_SCROLL_TEXT_TAG];

if(!scrollViewText){

scrollViewText = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 30, self.frame.size.width, 160)];

scrollViewText.showsHorizontalScrollIndicator = NO;  //隐藏水平滚动条

scrollViewText.showsVerticalScrollIndicator = NO;    //隐藏垂直滚动条

scrollViewText.tag = K_MAIN_VIEW_SCROLL_TEXT_TAG;

scrollViewText.userInteractionEnabled = NO;

[scrollViewText setBackgroundColor:[UIColor clearColor]];

//清除子控件

for (UIView *view in [scrollViewText subviews]) {

[view removeFromSuperview];

}

//添加到当前视图

[self addSubview:scrollViewText];

}

if (self.arrData) {

CGFloat offsety = 0 ,i = 0, h = 30;

//设置滚动文字

UILabel *labText = nil;

for (NSString *dicTemp in self.arrData) {

labText = [[UILabel alloc] initWithFrame:CGRectMake(

(K_MAIN_VIEW_SCROLLER_LABLE_WIDTH + K_MAIN_VIEW_SCROLLER_LABLE_MARGIN),

(K_MAIN_VIEW_SCROLL_HEIGHT - h) / 2+i*h,

K_MAIN_VIEW_SCROLLER_LABLE_WIDTH,

h)];

[labText setFont:[UIFont systemFontOfSize:15]];

[labText setTextColor:[UIColor blackColor]];

[labText setText:dicTemp];

offsety = labText.frame.origin.y;

//添加到滚动视图

[scrollViewText addSubview:labText];

i++;

}

//开启滚动

[self startScroll];

//设置滚动区域大小

[scrollViewText setContentSize:CGSizeMake(0, offsety)];

}

}

//开始滚动

-(void) startScroll{

if (!timer)

timer = [NSTimer scheduledTimerWithTimeInterval:K_MAIN_VIEW_TEME_INTERVAL target:self selector:@selector(setScrollText) userInfo:nil repeats:YES];

[timer fire];

}

//滚动处理

-(void) setScrollText{

[self setHidden:NO];

CGFloat starty = scrollViewText.contentSize.height-25;

[UIView animateWithDuration:K_MAIN_VIEW_TEME_INTERVAL * 2 animations:^{

CGRect rect;

CGFloat offsety = starty;

int i ;

for ( i= 0; i

UILabel *lab = (UILabel *)[scrollViewText.subviews objectAtIndex:i];

rect = lab.frame;

offsety = rect.origin.y - 10;

if (offsety<-100) {

[lab setTextColor:[UIColor clearColor]];

offsety = starty;

}

else{

[lab setTextColor:[Utils getColor:@"666666"]];

}

lab.frame = CGRectMake(20, offsety, rect.size.width, rect.size.height);

}

NSLog(@"offsety:%f",offsety);

}];

}

-(void)shutdown{

[timer invalidate];

timer = nil;

}

@end

你可能感兴趣的:(IOS使用定时器循环展示中奖名单)