温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

iOS如何实现循环滚动公告栏

发布时间:2021-03-20 10:21:21 来源:亿速云 阅读:354 作者:小新 栏目:开发技术

这篇文章将为大家详细讲解有关iOS如何实现循环滚动公告栏,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。

iOS实现循环滚动公告栏的具体代码如下:

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

关于“iOS如何实现循环滚动公告栏”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,使各位可以学到更多知识,如果觉得文章不错,请把它分享出去让更多的人看到。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

ios
AI