欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

iOS实现循环滚动公告栏

程序员文章站 2022-07-06 13:18:18
本文实例为大家分享了ios实现循环滚动公告栏的具体代码,供大家参考,具体内容如下封装了一个继承于uiview的类,如下:#import ns_assume_...

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

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

#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
#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调用代码:

xtaynoticescrollview *notiview = [[xtaynoticescrollview alloc] initwithframe:cgrectmake(50, 100, self.view.frame.size.width-100, 50) titlearray:@[@"我是第一个数据-11111111111111", @"我是第二个数据-2222222", @"我是第三个数据-33333333"]];
  
[self.view addsubview:notiview];

运行后的效果视频:

iOS实现循环滚动公告栏

公告内容用的label,无点击效果,若需要。替换为button,添加手势,都可以。

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