iOS实现文字水平无间断滚动效果
IOS跑马灯效果,实现文字水平无间断滚动,示例代码如下:
ViewController.h
#import@interfaceViewController:UIViewController{ NSTimer*timer; UIScrollView*scrollViewText; } @property(nonatomic,strong)NSArray*arrData; @end
ViewController.m
//
//ViewController.m
//滚动
//
#import"ViewController.h"
#pragmamark-Classdefinevariable
#defineK_MAIN_VIEW_SCROLL_HEIGHT80.0f
#defineK_MAIN_VIEW_SCROLL_TEXT_TAG300
#defineK_MAIN_VIEW_TEME_INTERVAL0.35//计时器间隔时间(单位秒)
#defineK_MAIN_VIEW_SCROLLER_SPACE20.0f//每次移动的距离
#defineK_MAIN_VIEW_SCROLLER_LABLE_WIDTH18.0f//单个字符宽度(与你设置的字体大小一致)
#defineK_MAIN_VIEW_SCROLLER_LABLE_MARGIN20.0f//前后间隔距离
#defineK_MAIN_VIEW_SCROLLER_SLEEP_INTERVAL1//停留时间
@interfaceViewController()
@end
@implementationViewController
#pragmamark-Classproperty
@synthesizearrData;
-(void)viewDidLoad{
[superviewDidLoad];
[selfinitView];
}
-(void)didReceiveMemoryWarning{
[superdidReceiveMemoryWarning];
//Disposeofanyresourcesthatcanberecreated.
}
#pragmamark-Custommethod
//初始化数据
-(void)initView{
if(!self.arrData){
self.arrData=@[
@{
@"newsId":@"201507070942261935",
@"newsImg":@"http://bg.fx678.com/HTMgr/upload/UpFiles/20150707/sy_2015070709395519.jpg",
@"newsTitle":@"三大理由欧元任性抗跌,欧元区峰会将为希腊定调"
},
@{
@"newsId":@"201507070929021220",
@"newsImg":@"http://bg.fx678.com/HTMgr/upload/UpFiles/20150707/sy_2015070709273545.jpg",
@"newsTitle":@"欧盟峰会或现希腊转机,黄金打响1162保卫战"
},
@{
@"newsId":@"201507070656471857",
@"newsImg":@"http://bg.fx678.com/HTMgr/upload/UpFiles/20150707/2015070706533134.jpg",
@"newsTitle":@"希腊困局欧元不怕,油价服软暴跌8%"
}
];
}
//文字滚动
[selfinitScrollText];
//开启滚动
[selfstartScroll];
}
//文字滚动初始化
-(void)initScrollText{
//获取滚动条
scrollViewText=(UIScrollView*)[self.viewviewWithTag:K_MAIN_VIEW_SCROLL_TEXT_TAG];
if(!scrollViewText){
scrollViewText=[[UIScrollViewalloc]initWithFrame:CGRectMake(0,80,self.view.frame.size.width,K_MAIN_VIEW_SCROLL_HEIGHT)];
scrollViewText.showsHorizontalScrollIndicator=NO;//隐藏水平滚动条
scrollViewText.showsVerticalScrollIndicator=NO;//隐藏垂直滚动条
scrollViewText.scrollEnabled=NO;//禁用手动滑动
//横竖屏自适应
scrollViewText.autoresizingMask=UIViewAutoresizingFlexibleWidth;
scrollViewText.tag=K_MAIN_VIEW_SCROLL_TEXT_TAG;
[scrollViewTextsetBackgroundColor:[UIColorgrayColor]];
//给滚动视图添加事件
UITapGestureRecognizer*tapGesture=[[UITapGestureRecognizeralloc]initWithTarget:selfaction:@selector(scrollerViewClick:)];
[scrollViewTextaddGestureRecognizer:tapGesture];
//添加到当前视图
[self.viewaddSubview:scrollViewText];
}else{
//清除子控件
for(UIView*viewin[scrollViewTextsubviews]){
[viewremoveFromSuperview];
}
}
if(self.arrData){
CGFloatoffsetX=0,i=0,h=30;
//设置滚动文字
UIButton*btnText=nil;
NSString*strTitle=[[NSStringalloc]init];
for(NSDictionary*dicTempinself.arrData){
strTitle=dicTemp[@"newsTitle"];
btnText=[UIButtonbuttonWithType:UIButtonTypeCustom];
[btnTextsetFrame:CGRectMake([selfgetTitleLeft:i],
(K_MAIN_VIEW_SCROLL_HEIGHT-h)/2,
strTitle.length*K_MAIN_VIEW_SCROLLER_LABLE_WIDTH,
h)];
[btnTextsetTitleColor:[UIColorredColor]forState:UIControlStateNormal];
[btnTextsetTitle:strTitleforState:UIControlStateNormal];
//横竖屏自适应
btnText.autoresizingMask=UIViewAutoresizingFlexibleWidth;
offsetX+=btnText.frame.origin.x;
//设置为NO,否则无法响应点击事件
btnText.userInteractionEnabled=NO;
//添加到滚动视图
[scrollViewTextaddSubview:btnText];
i++;
}
//设置滚动区域大小
[scrollViewTextsetContentSize:CGSizeMake(offsetX,0)];
}
}
#pragmamark-滚动处理
//开始滚动
-(void)startScroll{
if(!timer)
timer=[NSTimerscheduledTimerWithTimeInterval:K_MAIN_VIEW_TEME_INTERVALtarget:selfselector:@selector(setScrollText)userInfo:nilrepeats:YES];
[timerfire];
}
//滚动处理
-(void)setScrollText{
[UIViewanimateWithDuration:K_MAIN_VIEW_TEME_INTERVAL*2animations:^{
CGRectrect;
CGFloatoffsetX=0.0,width=0.0;
for(UIButton*btnTextinscrollViewText.subviews){
rect=btnText.frame;
offsetX=rect.origin.x-K_MAIN_VIEW_SCROLLER_SPACE;
width=[btnText.titleLabel.textlength]*K_MAIN_VIEW_SCROLLER_LABLE_WIDTH;
btnText.frame=CGRectMake(offsetX,rect.origin.y,rect.size.width,rect.size.height);
NSLog(@"offsetX:%f",offsetX);
}
if(offsetX<-width){
[UIViewsetAnimationsEnabled:NO];
[selfinitScrollText];
}else
[UIViewsetAnimationsEnabled:YES];
}];
}
#pragmamark-动态获取左边位置
-(float)getTitleLeft:(CGFloat)i{
floatleft=i*K_MAIN_VIEW_SCROLLER_LABLE_MARGIN;
if(i>0){
for(intj=0;j
示例源码下载:文字水平无间断滚动效果
备注:该开发工具XCode版本为6.4,如无法直接运行,可新建项目将以上文件复制替换即可
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。
声明:本文内容来源于网络,版权归原作者所有,内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎发送邮件至:czq8825#qq.com(发邮件时,请将#更换为@)进行举报,并提供相关证据,一经查实,本站将立刻删除涉嫌侵权内容。