版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
【移动应用开发技术】怎么在iOS中实现一个文字水平无间断滚动效果
本篇文章为大家展示了怎么在iOS中实现一个文字水平无间断滚动效果,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。ViewController.h#import
<UIKit/UIKit.h>
@interface
ViewController
:
UIViewController{
NSTimer
*timer;
UIScrollView
*scrollViewText;
}
@property
(nonatomic
,strong)
NSArray
*arrData;
@endViewController.m//
//
ViewController.m
//
滚动
//
#import
"ViewController.h"
#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.0f
//每次移动的距离
#define
K_MAIN_VIEW_SCROLLER_LABLE_WIDTH
18.0f
//单个字符宽度(与你设置的字体大小一致)
#define
K_MAIN_VIEW_SCROLLER_LABLE_MARGIN
20.0f
//前后间隔距离
#define
K_MAIN_VIEW_SCROLLER_SLEEP_INTERVAL
1
//停留时间
@interface
ViewController
()
@end
@implementation
ViewController
#pragma
mark
-
Class
property
@synthesize
arrData;
-
(void)viewDidLoad
{
[super
viewDidLoad];
[self
initView];
}
-
(void)didReceiveMemoryWarning
{
[super
didReceiveMemoryWarning];
//
Dispose
of
any
resources
that
can
be
recreated.
}
#pragma
mark
-
Custom
method
//初始化数据
-(void)
initView{
if
(!self.arrData)
{
self.arrData
=
@[
@{
@"newsId"
:@"201507070942261935",
@"newsImg"
:@"/upload/information/20200623/126/118344.jpg",
@"newsTitle":@"三大理由欧元任性抗跌,欧元区峰会将为希腊定调"
},
@{
@"newsId"
:@"201507070929021220",
@"newsImg"
:@"/upload/information/20200623/126/118345.jpg",
@"newsTitle"
:@"欧盟峰会或现希腊转机,黄金打响1162保卫战"
},
@{
@"newsId"
:@"201507070656471857",
@"newsImg"
:@"/upload/information/20200623/126/118347.jpg",
@"newsTitle"
:@"希腊困局欧元不怕,油价服软暴跌8%"
}
];
}
//文字滚动
[self
initScrollText];
//开启滚动
[self
startScroll];
}
//文字滚动初始化
-(void)
initScrollText{
//获取滚动条
scrollViewText
=
(UIScrollView
*)[self.view
viewWithTag:K_MAIN_VIEW_SCROLL_TEXT_TAG];
if(!scrollViewText){
scrollViewText
=
[[UIScrollView
alloc]
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;
[scrollViewText
setBackgroundColor:[UIColor
grayColor]];
//给滚动视图添加事件
UITapGestureRecognizer
*tapGesture
=
[[UITapGestureRecognizer
alloc]
initWithTarget:self
action:@selector(scrollerViewClick:)];
[scrollViewText
addGestureRecognizer:tapGesture];
//添加到当前视图
[self.view
addSubview:scrollViewText];
}else{
//清除子控件
for
(UIView
*view
in
[scrollViewText
subviews])
{
[view
removeFromSuperview];
}
}
if
(self.arrData)
{
CGFloat
offsetX
=
0
,i
=
0,
h
=
30;
//设置滚动文字
UIButton
*btnText
=
nil;
NSString
*strTitle
=
[[NSString
alloc]
init];
for
(NSDictionary
*dicTemp
in
self.arrData)
{
strTitle
=
dicTemp[@"newsTitle"];
btnText
=
[UIButton
buttonWithType:UIButtonTypeCustom];
[btnText
setFrame:CGRectMake([self
getTitleLeft:i],
(K_MAIN_VIEW_SCROLL_HEIGHT
-
h)
/
2,
strTitle.length
*
K_MAIN_VIEW_SCROLLER_LABLE_WIDTH,
h)];
[btnText
setTitleColor:[UIColor
redColor]
forState:UIControlStateNormal];
[btnText
setTitle:strTitle
forState:UIControlStateNormal];
//横竖屏自适应
btnText.autoresizingMask
=
UIViewAutoresizingFlexibleWidth;
offsetX
+=
btnText.frame.origin.x;
//设置为
NO,否则无法响应点击事件
btnText.userInteractionEnabled
=
NO;
//添加到滚动视图
[scrollViewText
addSubview:btnText];
i++;
}
//设置滚动区域大小
[scrollViewText
setContentSize:CGSizeMake(offsetX,
0)];
}
}
#pragma
mark
-
滚动处理
//开始滚动
-(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{
[UIView
animateWithDuration:K_MAIN_VIEW_TEME_INTERVAL
*
2
animations:^{
CGRect
rect;
CGFloat
offsetX
=
0.0,width
=
0.0;
for
(UIButton
*btnText
in
scrollViewText.subviews)
{
rect
=
btnText.frame;
offsetX
=
rect.origin.x
-
K_MAIN_VIEW_SCROLLER_SPACE;
width
=
[btnText.titleLabel.text
length]
*
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){
[UIView
setAnimationsEnabled:NO];
[self
initScrollText];
}else
[UIView
setAnimationsEnabled:YES];
}];
}
#pragma
mark
-
动态获取左边位置
-(float)
getTitleLeft:(CGFloat)
i
{
float
left
=
i
*
K_MAIN_VIEW_SCROLLER_LABLE_MARGIN;
if
(i
>
0)
{
for
(int
j
=
0;
j
<
i;
j
++)
{
left
+=
[[self.arrData
objectAtIndex:j][@"newsTitle"]
length]
*
K_MAIN_VIEW_SCROLLER_LABLE_WIDTH;
}
}
return
left;
}
#pragma
mark
-
新闻点击事件
-(void)btnNewsClick:(UIButton
*)
sender{
NSString
*strNewsTitle
=
sende
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2024年羟丙基甲基纤维素项目投资申请报告代可行性研究报告
- 2023-2024学年云南省昆明市西山区高一上学期期末考试数学试卷(解析版)
- 人际关系处理方案
- 青岛科技大学信息学院862计算机网络历年考研真题汇编合集
- 电子商务进农村物流及电商物流服务站点补贴实施方案(试行)
- 道路劝导点信息统计表
- 科技与信息管理
- 浅谈公开课的评法
- 第14课 走一步再走一步(教师版)七年级语文上册同步培优拔尖精研专练(统编版)
- 低钾血症诊疗及护理考核试题及答案
- 幼儿园小班音乐《小老鼠上灯台》课件
- 六年级下册综合实践测试题及答案
- 2022年云南省三支一扶计划招募考试真题及答案
- 台湾大学欧丽娟老师的中国文学史讲义
- 安全员之江苏省C1证(机械安全员)试题+答案(得分题)
- 西方经济学试题库及答案解析
- 柴油发电机试车方案设计
- 英语阅读知到章节答案智慧树2023年北京大学
- 国家开放大学《哲学基础》形考任务1-3参考答案
- 2022年南京市雨花台区社会工作者招聘考试试题
- 2022-2023学年山东省临沂市统招专升本管理学测试题(含答案)
评论
0/150
提交评论