iOS实现文字水平无间断滚动效果

时间:2022-01-01 08:45:38

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

viewcontroller.h

?
1
2
3
4
5
6
7
8
9
10
#import <uikit/uikit.h>
 
@interface viewcontroller : uiviewcontroller{
  nstimer      *timer;
  uiscrollview   *scrollviewtext;
}
 
@property (nonatomic ,strong) nsarray *arrdata;
 
@end

viewcontroller.m

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
//
// 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" :@"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%"
             }
           ];
  }
 
  //文字滚动
  [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 = sender.titlelabel.text;
 
  uialertview *alert = [[uialertview alloc] initwithtitle:@"系统提示"
                          message:strnewstitle
                          delegate:sender
                     cancelbuttontitle:@"确定"
                     otherbuttontitles:@"其他", nil];
  [alert show];
 
}
 
-(void)scrollerviewclick:(uitapgesturerecognizer*)gesture{
 
  cgpoint touchpoint = [gesture locationinview:scrollviewtext];
 
  for (uibutton *btn in scrollviewtext.subviews) {
 
    if ([btn.layer.presentationlayer hittest:touchpoint]) {
      [self btnnewsclick:btn];
      break;
    }
 
  }
 
}
 
@end

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

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

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

原文链接:https://blog.csdn.net/yimiyuangguang/article/details/46801803