经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 移动开发 » iOS » 查看文章
iOS实现文字水平无间断滚动效果
来源:jb51  时间:2019/8/27 9:26:16  对本文有异议

IOS跑马灯效果,实现文字水平无间断滚动,示例代码如下:

ViewController.h

  1. #import <UIKit/UIKit.h>
  2. @interface ViewController : UIViewController{
  3. NSTimer *timer;
  4. UIScrollView *scrollViewText;
  5. }
  6. @property (nonatomic ,strong) NSArray *arrData;
  7. @end

ViewController.m

  1. //
  2. // ViewController.m
  3. // 滚动
  4. //
  5. #import "ViewController.h"
  6. #pragma mark - Class define variable
  7. #define K_MAIN_VIEW_SCROLL_HEIGHT 80.0f
  8. #define K_MAIN_VIEW_SCROLL_TEXT_TAG 300
  9. #define K_MAIN_VIEW_TEME_INTERVAL 0.35 //计时器间隔时间(单位秒)
  10. #define K_MAIN_VIEW_SCROLLER_SPACE 20.0f //每次移动的距离
  11. #define K_MAIN_VIEW_SCROLLER_LABLE_WIDTH 18.0f //单个字符宽度(与你设置的字体大小一致)
  12. #define K_MAIN_VIEW_SCROLLER_LABLE_MARGIN 20.0f //前后间隔距离
  13. #define K_MAIN_VIEW_SCROLLER_SLEEP_INTERVAL 1 //停留时间
  14. @interface ViewController ()
  15. @end
  16. @implementation ViewController
  17. #pragma mark - Class property
  18. @synthesize arrData;
  19. - (void)viewDidLoad {
  20. [super viewDidLoad];
  21. [self initView];
  22. }
  23. - (void)didReceiveMemoryWarning {
  24. [super didReceiveMemoryWarning];
  25. // Dispose of any resources that can be recreated.
  26. }
  27. #pragma mark - Custom method
  28. //初始化数据
  29. -(void) initView{
  30. if (!self.arrData) {
  31. self.arrData = @[
  32. @{
  33. @"newsId" :@"201507070942261935",
  34. @"newsImg" :@"http://bg.fx678.com/HTMgr/upload/UpFiles/20150707/sy_2015070709395519.jpg",
  35. @"newsTitle":@"三大理由欧元任性抗跌,欧元区峰会将为希腊定调"
  36. },
  37. @{
  38. @"newsId" :@"201507070929021220",
  39. @"newsImg" :@"http://bg.fx678.com/HTMgr/upload/UpFiles/20150707/sy_2015070709273545.jpg",
  40. @"newsTitle" :@"欧盟峰会或现希腊转机,黄金打响1162保卫战"
  41. },
  42. @{
  43. @"newsId" :@"201507070656471857",
  44. @"newsImg" :@"http://bg.fx678.com/HTMgr/upload/UpFiles/20150707/2015070706533134.jpg",
  45. @"newsTitle" :@"希腊困局欧元不怕,油价服软暴跌8%"
  46. }
  47. ];
  48. }
  49. //文字滚动
  50. [self initScrollText];
  51. //开启滚动
  52. [self startScroll];
  53. }
  54. //文字滚动初始化
  55. -(void) initScrollText{
  56. //获取滚动条
  57. scrollViewText = (UIScrollView *)[self.view viewWithTag:K_MAIN_VIEW_SCROLL_TEXT_TAG];
  58. if(!scrollViewText){
  59. scrollViewText = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 80, self.view.frame.size.width, K_MAIN_VIEW_SCROLL_HEIGHT)];
  60. scrollViewText.showsHorizontalScrollIndicator = NO; //隐藏水平滚动条
  61. scrollViewText.showsVerticalScrollIndicator = NO; //隐藏垂直滚动条
  62. scrollViewText.scrollEnabled = NO; //禁用手动滑动
  63. //横竖屏自适应
  64. scrollViewText.autoresizingMask = UIViewAutoresizingFlexibleWidth;
  65. scrollViewText.tag = K_MAIN_VIEW_SCROLL_TEXT_TAG;
  66. [scrollViewText setBackgroundColor:[UIColor grayColor]];
  67. //给滚动视图添加事件
  68. UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(scrollerViewClick:)];
  69. [scrollViewText addGestureRecognizer:tapGesture];
  70. //添加到当前视图
  71. [self.view addSubview:scrollViewText];
  72. }else{
  73. //清除子控件
  74. for (UIView *view in [scrollViewText subviews]) {
  75. [view removeFromSuperview];
  76. }
  77. }
  78. if (self.arrData) {
  79. CGFloat offsetX = 0 ,i = 0, h = 30;
  80. //设置滚动文字
  81. UIButton *btnText = nil;
  82. NSString *strTitle = [[NSString alloc] init];
  83. for (NSDictionary *dicTemp in self.arrData) {
  84. strTitle = dicTemp[@"newsTitle"];
  85. btnText = [UIButton buttonWithType:UIButtonTypeCustom];
  86. [btnText setFrame:CGRectMake([self getTitleLeft:i],
  87. (K_MAIN_VIEW_SCROLL_HEIGHT - h) / 2,
  88. strTitle.length * K_MAIN_VIEW_SCROLLER_LABLE_WIDTH,
  89. h)];
  90. [btnText setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
  91. [btnText setTitle:strTitle forState:UIControlStateNormal];
  92. //横竖屏自适应
  93. btnText.autoresizingMask = UIViewAutoresizingFlexibleWidth;
  94. offsetX += btnText.frame.origin.x;
  95. //设置为 NO,否则无法响应点击事件
  96. btnText.userInteractionEnabled = NO;
  97. //添加到滚动视图
  98. [scrollViewText addSubview:btnText];
  99. i++;
  100. }
  101. //设置滚动区域大小
  102. [scrollViewText setContentSize:CGSizeMake(offsetX, 0)];
  103. }
  104. }
  105. #pragma mark - 滚动处理
  106. //开始滚动
  107. -(void) startScroll{
  108. if (!timer)
  109. timer = [NSTimer scheduledTimerWithTimeInterval:K_MAIN_VIEW_TEME_INTERVAL target:self selector:@selector(setScrollText) userInfo:nil repeats:YES];
  110. [timer fire];
  111. }
  112. //滚动处理
  113. -(void) setScrollText{
  114. [UIView animateWithDuration:K_MAIN_VIEW_TEME_INTERVAL * 2 animations:^{
  115. CGRect rect;
  116. CGFloat offsetX = 0.0,width = 0.0;
  117. for (UIButton *btnText in scrollViewText.subviews) {
  118. rect = btnText.frame;
  119. offsetX = rect.origin.x - K_MAIN_VIEW_SCROLLER_SPACE;
  120. width = [btnText.titleLabel.text length] * K_MAIN_VIEW_SCROLLER_LABLE_WIDTH;
  121. btnText.frame = CGRectMake(offsetX, rect.origin.y, rect.size.width, rect.size.height);
  122. NSLog(@"offsetX:%f",offsetX);
  123. }
  124. if (offsetX < -width){
  125. [UIView setAnimationsEnabled:NO];
  126. [self initScrollText];
  127. }else
  128. [UIView setAnimationsEnabled:YES];
  129. }];
  130. }
  131. #pragma mark - 动态获取左边位置
  132. -(float) getTitleLeft:(CGFloat) i {
  133. float left = i * K_MAIN_VIEW_SCROLLER_LABLE_MARGIN;
  134. if (i > 0) {
  135. for (int j = 0; j < i; j ++) {
  136. left += [[self.arrData objectAtIndex:j][@"newsTitle"] length] * K_MAIN_VIEW_SCROLLER_LABLE_WIDTH;
  137. }
  138. }
  139. return left;
  140. }
  141. #pragma mark - 新闻点击事件
  142. -(void)btnNewsClick:(UIButton *) sender{
  143. NSString *strNewsTitle = sender.titleLabel.text;
  144. UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"系统提示"
  145. message:strNewsTitle
  146. delegate:sender
  147. cancelButtonTitle:@"确定"
  148. otherButtonTitles:@"其他", nil];
  149. [alert show];
  150. }
  151. -(void)scrollerViewClick:(UITapGestureRecognizer*)gesture{
  152. CGPoint touchPoint = [gesture locationInView:scrollViewText];
  153. for (UIButton *btn in scrollViewText.subviews) {
  154. if ([btn.layer.presentationLayer hitTest:touchPoint]) {
  155. [self btnNewsClick:btn];
  156. break;
  157. }
  158. }
  159. }
  160. @end

示例源码下载:文字水平无间断滚动效果

备注:该开发工具XCode 版本为 6.4,如无法直接运行,可新建项目将以上文件复制替换即可

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持w3xue。

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号