IOS使用定時(shí)器循環(huán)展示中獎(jiǎng)名單

利用定時(shí)器(NSTimer)和 UIScrollView 循環(huán)展示文本

即粘即用,效果如下圖(不會(huì)截動(dòng)圖),但是一定會(huì)動(dòng)(禮貌而又不失尷尬的微笑)

你僅需要復(fù)制下面的代碼后,初始化LotteryScrollView和arrData。

并且調(diào)用以下兩個(gè)方法

-(void)initView;

-(void)shutdown;//退出頁面時(shí),一定要調(diào)用這個(gè)方法,不然定時(shí)器不會(huì)自動(dòng)自動(dòng)釋放。

新建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? ? ? ? //計(jì)時(shí)器間隔時(shí)間(單位秒)

#define K_MAIN_VIEW_SCROLLER_SPACE 20? ? ? ? ? //每次移動(dòng)的距離

#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;

}

//初始化數(shù)據(jù)

-(void) initView{

//文字滾動(dòng)

[self initScrollText];

}

//文字滾動(dòng)初始化

-(void) initScrollText{

//獲取滾動(dòng)條

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;? //隱藏水平滾動(dòng)條

scrollViewText.showsVerticalScrollIndicator = NO;? ? //隱藏垂直滾動(dòng)條

scrollViewText.tag = K_MAIN_VIEW_SCROLL_TEXT_TAG;

scrollViewText.userInteractionEnabled = NO;

[scrollViewText setBackgroundColor:[UIColor clearColor]];

//清除子控件

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

[view removeFromSuperview];

}

//添加到當(dāng)前視圖

[self addSubview:scrollViewText];

}

if (self.arrData) {

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

//設(shè)置滾動(dòng)文字

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;

//添加到滾動(dòng)視圖

[scrollViewText addSubview:labText];

i++;

}

//開啟滾動(dòng)

[self startScroll];

//設(shè)置滾動(dòng)區(qū)域大小

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

}

}

//開始滾動(dòng)

-(void) startScroll{

if (!timer)

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

[timer fire];

}

//滾動(dòng)處理

-(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<scrollViewText.subviews.count ; 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

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容