iOS實現循環滾動公告欄
本文實例為大傢分享瞭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];
運行後的效果視頻:
公告內容用的label,無點擊效果,若需要。替換為button,添加手勢,都可以。
以上就是本文的全部內容,希望對大傢的學習有所幫助,也希望大傢多多支持WalkonNet。
推薦閱讀:
- iOS UIBezierPath實現餅狀圖
- UICollectionView 實現圖片瀏覽效果
- iOS實現逐幀動畫做loading視圖
- iOS實現多控制器切換效果
- IOS開發自定義view方法規范示例