本文实例为大家分享了iOS实现循环滚动公告栏的具体代码,供大家参考,具体内容如下
封装了一个继承于UIView的类,如下:
?
1
2
3
4
5
6
7
8
9
10
11
12
|
#import <UIKit/UIKit.h>
NS_ASSUME_NONNULL_BEGIN
@interface XtayNoticeScrollView : UIView
- (instancetype)initWithFrame:(CGRect)frame titleArray:(NSArray<NSString *> *)titleArray;
- ( void )openTimer;
- ( void )closeTimer;
@end
NS_ASSUME_NONNULL_END
|
?
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
|
#define ROW_H self.bounds.size.height
#import "XtayNoticeScrollView.h"
@interface XtayNoticeScrollView ()
/// scrollView
@property (nonatomic, strong) UIScrollView *bgScrollView;
/// titleArr
@property (nonatomic, copy) NSArray *titleArr;
/// timer
@property (nonatomic, strong) NSTimer *scrollTimer;
@end
@implementation XtayNoticeScrollView
- (instancetype)initWithFrame:(CGRect)frame titleArray:(NSArray<NSString *> *)titleArray {
self = [super initWithFrame:frame];
if (self) {
self.titleArr = titleArray;
[self addSubview:self.bgScrollView];
[self createBaseView];
[self openTimer];
}
return self;
}
// MARK: - 开启定时器
- ( void )openTimer {
if (!_scrollTimer) {
_scrollTimer = [NSTimer scheduledTimerWithTimeInterval:3 target:self selector:@selector(timerMoved) userInfo:nil repeats:YES];
[[NSRunLoop currentRunLoop] addTimer:_scrollTimer forMode:NSRunLoopCommonModes];
}
}
// MARK: - 关闭定时器
- ( void )closeTimer {
[_scrollTimer invalidate];
_scrollTimer = nil;
}
- (UIScrollView *)bgScrollView {
if (!_bgScrollView) {
_bgScrollView = [[UIScrollView alloc] initWithFrame:self.bounds];
_bgScrollView.scrollEnabled = NO;
_bgScrollView.showsVerticalScrollIndicator = NO;
_bgScrollView.showsHorizontalScrollIndicator = NO;
_bgScrollView.backgroundColor = UIColor.whiteColor;
}
return _bgScrollView;
}
// MARK: - 创建所有视图
- ( void )createBaseView {
// 安全判断
if (self.titleArr.count == 0) {
return ;
}
// 为了展示滑动过程的流畅性,重新处理数组
NSMutableArray *dataMArray = [NSMutableArray arrayWithCapacity:0];
[dataMArray addObjectsFromArray:_titleArr];
[dataMArray addObject:_titleArr.firstObject];
for ( int i = 0; i<dataMArray.count; i++) {
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, ROW_H*(i%dataMArray.count), self.bgScrollView.bounds.size.width, ROW_H)];
label.text = dataMArray[i];
label.font = [UIFont systemFontOfSize:15];
label.textColor = [UIColor blackColor];
label.numberOfLines = 0;
[_bgScrollView addSubview:label];
}
_bgScrollView.contentSize = CGSizeMake(0, ROW_H*dataMArray.count);
}
// MARK: - 定时器调用方法
- ( void )timerMoved {
CGFloat pageY = self.bgScrollView.contentOffset.y/ROW_H;
int pageIntY = pageY;
if (pageIntY >= self.titleArr.count) {
[self.bgScrollView setContentOffset:CGPointMake(0, 0) animated:NO];
} else {
[self.bgScrollView setContentOffset:CGPointMake(0, (pageIntY+1)*ROW_H) animated:YES];
}
}
|
VC调用代码:
?
1
2
3
|
XtayNoticeScrollView *notiView = [[XtayNoticeScrollView alloc] initWithFrame:CGRectMake(50, 100, self.view.frame.size.width-100, 50) titleArray:@[@ "我是第一个数据-11111111111111" , @ "我是第二个数据-2222222" , @ "我是第三个数据-33333333" ]];
[self.view addSubview:notiView];
|
运行后的效果视频:
公告内容用的label,无点击效果,若需要。替换为button,添加手势,都可以。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持快网idc。
相关文章
猜你喜欢
- 个人网站搭建:如何挑选具有弹性扩展能力的服务器? 2025-06-10
- 个人服务器网站搭建:如何选择适合自己的建站程序或框架? 2025-06-10
- 64M VPS建站:能否支持高流量网站运行? 2025-06-10
- 64M VPS建站:怎样选择合适的域名和SSL证书? 2025-06-10
- 64M VPS建站:怎样优化以提高网站加载速度? 2025-06-10
TA的动态
- 2025-07-10 怎样使用阿里云的安全工具进行服务器漏洞扫描和修复?
- 2025-07-10 怎样使用命令行工具优化Linux云服务器的Ping性能?
- 2025-07-10 怎样使用Xshell连接华为云服务器,实现高效远程管理?
- 2025-07-10 怎样利用云服务器D盘搭建稳定、高效的网站托管环境?
- 2025-07-10 怎样使用阿里云的安全组功能来增强服务器防火墙的安全性?
快网idc优惠网
QQ交流群
您的支持,是我们最大的动力!
热门文章
-
2025-05-29 74
-
2025-05-27 71
-
spring boot+vue 的前后端分离与合并方案实例详解
2025-05-29 76 -
2025-05-27 59
-
2025-05-29 62
热门评论