前段时间,因项目需要,做了一个label上下滚动的效果,类似淘宝上的头条,小功能,觉得有点实用仅供参考,思路很简单就是用scrollerview,设置一个定时器3秒滚动一次,放了项目代码片段,所有代码都在上,源码就不贴上去了
- (void)mainNewsRefresh {
// noticeScroller ,xib里scrollerView
NSArray *subViews = [self.noticeScroller subviews];
for (UIView *view in subViews) {
[view removeFromSuperview];
}
float x = 0.0;
float y = 0;
float w = 275;
float h = 35;
UIButton *but = [UIButton buttonWithType:UIButtonTypeCustom];
but.frame = CGRectMake(x, y, w, h);
but.tag = countInt;
[but addTarget:self action:@selector(noticeButAction:) forControlEvents:UIControlEventTouchUpInside];
[self.noticeScroller addSubview:but];
y += h;
self.noticeScroller.contentSize = CGSizeMake(x, y);
// 计时器,3秒滚动一次
[NSTimer scheduledTimerWithTimeInterval:3.0 target:self selector:@selector(displayNews) userInfo:nil repeats:YES];
}
-(void)displayNews{
countInt++; // int类型,初始化为countInt=0
if (countInt >= [mubArray count])
countInt=0;
CATransition *animation = [CATransition animation];
animation.delegate = self;
animation.duration = 0.5f ;
animation.timingFunction = UIViewAnimationCurveEaseInOut;
animation.fillMode = kCAFillModeForwards;
animation.removedOnCompletion = YES;
animation.type = @"cube";
// 需要滚动显示的文字在mubArray数组里面,数组里面嵌套的是一个字典
Lab.text = [[mubArray objectAtIndex:countInt] objectForKey:@"title"];
// 视图_footAdvertUIViwView
[_footAdvertUIViw.layer addAnimation:animation forKey:@"animationID"];
}
// button点击事件
- (void)noticeButAction:(id)sender{
if (countInt >= [mubArray count])
countInt=0;
noticeDict = [mubArray objectAtIndex:countInt];
if (noticeDict != nil) {
WebView *WebView = [[WebView alloc] initWithNibName:@"WebView" bundle:nil];
[WebView initData:noticeDict];
[self presentViewController:WebView animated:YES completion:nil];
}
}
奉上一张项目效果图,时间效果和淘宝一样,只是它一次滚动两条,我只需要一条