上下輪番圖

NSArray *images = @[@"聞喜縣交警隊測速儀采購項目 再次競爭性談判公告",@"我要展現(xiàn)給用戶的內(nèi)容放在scrollview中",@"讓內(nèi)容從上到底自動滾動,我最開始用的是DDAutoscrollview",@"如果需要開場動畫的效果,在scrollView的viewcontroller實現(xiàn)"];

_scyleSV = [[HU_ScycleScrollView alloc]initWithFrame:CGRectMake(0, 80, self.view.bounds.size.width, 44)];

_scyleSV.backgroundColor = [UIColor redColor];

_scyleSV.titles = images;

_scyleSV.userInteractionEnabled = YES;

_scyleSV.delegate = self;

[self.view addSubview:_scyleSV];

#pragma mark ScyleScrollViewDelegate

- (void)scyleScrollView:(HU_ScycleScrollView *)scyleView index:(NSInteger)index{

NSLog(@"----- %ld",index);

}

HU_ScycleScrollView.h

typedef NS_ENUM(NSInteger, pageControlAligment){? ? ? pageControlAligmentCenter = 0,? ? pageControlAligmentLeft};@class HU_ScycleScrollView;@protocol ScyleScrollViewDelegate- (void)scyleScrollView:(HU_ScycleScrollView *)scyleView index:(NSInteger)index;@end@interface HU_ScycleScrollView : UIView/** 標題數(shù)組 */@property (nonatomic,strong)NSArray *titles;/** 協(xié)議 */@property (nonatomic,assign)iddelegate;

HU_ScycleScrollView.m

#define SCYLE_WIDTH CGRectGetWidth(self.frame)#define SCYLE_HEIGHT CGRectGetHeight(self.frame)@interface HU_ScycleScrollView()/** 延遲時間 */

@property (nonatomic,assign)NSTimeInterval intervalTime;

/** 滑動視圖 */

@property (nonatomic,strong)UIScrollView *scrollView;

/** 延時器 */

@property (nonatomic,strong)NSTimer *delayTimer;

/** 目前標題 */

@property (nonatomic,strong)UILabel *currentTitleView;

/** 目前標題位置 */

@property (nonatomic,assign)NSInteger currentTitIndex;

/** 下一個標題 */

@property (nonatomic,strong)UILabel *nextTitleView;

/** 下一個標題的位置 */

@property (nonatomic,assign)NSInteger nextTitIndex;

@end

@implementation HU_ScycleScrollView

#pragma mark 初始化

- (instancetype)initWithFrame:(CGRect)frame{

if (self = [super initWithFrame:frame]) {

self.intervalTime = 3;

[self setupScycleView];

}

return self;

}

#pragma mark 創(chuàng)建視圖

- (void)setupScycleView{

//添加scrollView

UIScrollView *scrollView = [[UIScrollView alloc]initWithFrame:self.bounds];

scrollView.pagingEnabled = YES;

scrollView.scrollEnabled = YES;

scrollView.userInteractionEnabled = NO;

scrollView.bounces = NO;

scrollView.delegate = self;

scrollView.contentSize = CGSizeMake(SCYLE_WIDTH, SCYLE_HEIGHT * 2);

scrollView.contentOffset = CGPointMake(0, 0);

scrollView.showsHorizontalScrollIndicator = NO;

scrollView.showsVerticalScrollIndicator = NO;

[self addSubview:scrollView];

self.scrollView = scrollView;

//創(chuàng)建3個UILabel

[self setupThreeTitleView];

}

- (void)setupThreeTitleView{

//目前標題

UILabel *currentTitleView = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, SCYLE_WIDTH, SCYLE_HEIGHT)];

currentTitleView.userInteractionEnabled = YES;

[self.scrollView addSubview:currentTitleView];

self.currentTitleView = currentTitleView;

//給目前標題添加手勢

UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(clickTheCurrentImgAction:)];

[self addGestureRecognizer:tap];

//下一個標題

UILabel *nextTitleView = [[UILabel alloc]initWithFrame:CGRectMake(0, SCYLE_HEIGHT, SCYLE_WIDTH, SCYLE_HEIGHT)];

[self.scrollView addSubview:nextTitleView];

self.nextTitleView = nextTitleView;

}

- (void)setTitles:(NSArray *)titles{

_titles = titles;

//創(chuàng)建延時器

[self renewSetDelayTimer];

//更新圖片位置

[self updateScycelScrollViewTitleIndex];

}

#pragma mark 更新圖片位置

- (void)updateScycelScrollViewTitleIndex{

if (self.titles.count > 0) {

[self addTheTitleUrlStr:self.titles[self.currentTitIndex] titleView:_currentTitleView];

[self addTheTitleUrlStr:self.titles[self.nextTitIndex] titleView:_nextTitleView];

}

}

#pragma mark 解析圖片并添加到imageView上

- (void)addTheTitleUrlStr:(NSString *)title titleView:(UILabel *)titleView{

titleView.text = title;

}

#pragma mark 延時器執(zhí)行方法

- (void)useTimerIntervalUpdateScrollViewContentOffSet:(NSTimer *)timer{

[_scrollView setContentOffset:CGPointMake(0, SCYLE_HEIGHT) animated:YES];

}

#pragma mark 點擊圖片執(zhí)行方法

- (void)clickTheCurrentImgAction:(UITapGestureRecognizer *)tap{

if ([_delegate respondsToSelector:@selector(scyleScrollView:index:)]) {

[self.delegate scyleScrollView:self index:_currentTitIndex];

}

}

#pragma mark 滑動結(jié)束時停止動畫

- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView{

[self scrollViewDidEndDecelerating:scrollView];

}

#pragma mark 減速滑動時

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{

int offSet = floor(scrollView.contentOffset.y);

if (offSet == SCYLE_HEIGHT){

self.currentTitIndex = self.nextTitIndex;

}

//更新標題位置

[self updateScycelScrollViewTitleIndex];

//設(shè)置偏移量

scrollView.contentOffset = CGPointMake(0, 0);

}

#pragma mark 重新設(shè)置延時器

- (void)renewSetDelayTimer{

//添加延遲器

self.delayTimer = [NSTimer scheduledTimerWithTimeInterval:self.intervalTime target:self selector:@selector(useTimerIntervalUpdateScrollViewContentOffSet:) userInfo:nil repeats:YES];

//加入事件循環(huán)中

[[NSRunLoop mainRunLoop] addTimer:self.delayTimer forMode:NSRunLoopCommonModes];

}

//上一個標題位置

- (NSUInteger)beforeTitIndex{

if (self.currentTitIndex == 0) {

return self.titles.count - 1;

}else{

return self.currentTitIndex - 1;

}

}

//下一個標題的位置

- (NSInteger)nextTitIndex{

if (self.currentTitIndex < (self.titles.count - 1)) {

return self.currentTitIndex + 1;

}else{

return 0;

}

}

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

推薦閱讀更多精彩內(nèi)容