利用定時(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