iOS实现循环滚动公告栏

这篇文章主要为大家详细介绍了iOS实现循环滚动公告栏,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

本文实例为大家分享了iOS实现循环滚动公告栏的具体代码,供大家参考,具体内容如下

封装了一个继承于UIView的类,如下:

 #import  NS_ASSUME_NONNULL_BEGIN @interface XtayNoticeScrollView : UIView - (instancetype)initWithFrame:(CGRect)frame titleArray:(NSArray *)titleArray; - (void)openTimer; - (void)closeTimer; @end NS_ASSUME_NONNULL_END
 #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 *)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= self.titleArr.count) { [self.bgScrollView setContentOffset:CGPointMake(0, 0) animated:NO]; } else { [self.bgScrollView setContentOffset:CGPointMake(0, (pageIntY+1)*ROW_H) animated:YES]; } }

VC调用代码:

 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,添加手势,都可以。

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

以上就是iOS实现循环滚动公告栏的详细内容,更多请关注0133技术站其它相关文章!

赞(0) 打赏
未经允许不得转载:0133技术站首页 » 移动