//

//  CycleScrollView.h

//  PagedScrollView

//

//  Created by 李洪强 on 16-1-23.

//  Copyright (c) 2016年 李洪强. All rights reserved.

//

#import <UIKit/UIKit.h>

@interface CycleScrollView : UIView

@property (nonatomic , strong) NSTimer *animationTimer;

@property (nonatomic , readonly) UIScrollView *scrollView;

/**

*  初始化

*

*  @param frame             frame

*  @param animationDuration 自动滚动的间隔时长。如果<=0,不自动滚动。

*

*  @return instance

*/

- (id)initWithFrame:(CGRect)frame animationDuration:(NSTimeInterval)animationDuration;

- (void)pause;

- (void)restart;

/**

数据源:获取总的page个数

**/

@property (nonatomic ,copy) NSInteger (^totalPagesCount)(void);

/**

数据源:获取第pageIndex个位置的contentView

**/

@property (nonatomic , copy) UIView *(^fetchContentViewAtIndex)(NSInteger pageIndex);

/**

当点击的时候,执行的block

**/

@property (nonatomic , copy) void (^TapActionBlock)(NSInteger pageIndex);

@end



-------------------------------------------------------------------

//

//  CycleScrollView.m

//  PagedScrollView

//

//  Created by 李洪强 on 16-1-23.

//  Copyright (c) 2014年 李洪强. All rights reserved.

//

#import "CycleScrollView.h"

#import "NSTimer+Addition.h"

@interface CycleScrollView () <UIScrollViewDelegate>

{

BOOL onlyOnePage;

}

@property (nonatomic , assign) NSInteger currentPageIndex;

@property (nonatomic , assign) NSInteger totalPageCount;

@property (nonatomic , strong) NSMutableArray *contentViews;

@property (nonatomic , strong) UIScrollView *scrollView;

@property (nonatomic , assign) NSTimeInterval animationDuration;

@property (nonatomic,strong) UIPageControl *pageControl;

@end

@implementation CycleScrollView

- (void)dealloc{

[self.animationTimer invalidate];

self.animationTimer = nil;

}

- (void)setTotalPagesCount:(NSInteger (^)(void))totalPagesCount{

_totalPageCount = totalPagesCount();

_pageControl.numberOfPages  = _totalPageCount;

if (_totalPageCount > 0) {

if (_totalPageCount > 1) {

[self startTimerWithRepeats:YES];

}

else{

self.animationDuration = 0.01;

onlyOnePage = YES;

[self startTimerWithRepeats:NO];

}

}

}

- (void)startTimerWithRepeats:(BOOL)repeats{

if (!self.animationTimer){

self.animationTimer = [NSTimer scheduledTimerWithTimeInterval:self.animationDuration

target:self

selector:@selector(animationTimerDidFired:)

userInfo:nil

repeats:repeats];

}

[self.animationTimer pauseTimer];

CGPoint newOffset = CGPointMake(self.scrollView.contentOffset.x + CGRectGetWidth(self.scrollView.frame), self.scrollView.contentOffset.y);

[self.scrollView setContentOffset:newOffset animated:repeats];

[self configContentViews];

[self.animationTimer resumeTimerAfterTimeInterval:self.animationDuration];

}

- (id)initWithFrame:(CGRect)frame animationDuration:(NSTimeInterval)animationDuration{

self = [self initWithFrame:frame];

if (animationDuration > 0.0) {

self.animationDuration = animationDuration;

}

return self;

}

- (id)initWithFrame:(CGRect)frame{

self = [super initWithFrame:frame];

if (self) {

// Initialization code

self.autoresizesSubviews = YES;

self.scrollView = [[UIScrollView alloc] initWithFrame:self.frame];

self.scrollView.showsHorizontalScrollIndicator = NO;

self.scrollView.autoresizingMask = 0xFF;

self.scrollView.contentMode = UIViewContentModeCenter;

self.scrollView.contentSize = CGSizeMake(3 * CGRectGetWidth(self.frame), 0);//.scrollView.frame

self.scrollView.delegate = self;

self.scrollView.contentOffset = CGPointMake(CGRectGetWidth(self.frame), 0);//self.scrollView.frame

self.scrollView.pagingEnabled = YES;

[self addSubview:self.scrollView];

_scrollView.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin;

self.currentPageIndex = 0;

_pageControl = [[UIPageControl alloc]initWithFrame:CGRectMake(0, frame.size.height - 20, 100, 20)];

CGFloat centerY = _pageControl.center.y;

_pageControl.center = CGPointMake(self.width/2,centerY);

[self addSubview:_pageControl];

}

return self;

}

#pragma mark -

#pragma mark - 私有函数

- (void)configContentViews{

[self.scrollView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];

[self setScrollViewContentDataSource];

NSInteger counter = 0;

for (UIView *contentView in self.contentViews) {

contentView.userInteractionEnabled = YES;

UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(contentViewTapAction:)];

[contentView addGestureRecognizer:tapGesture];

CGRect rightRect = contentView.frame;

rightRect.origin = CGPointMake((CGRectGetWidth(self.scrollView.frame)) * (counter++), 0);

contentView.frame = rightRect;

[self.scrollView addSubview:contentView];

}

[_scrollView setContentOffset:CGPointMake(_scrollView.frame.size.width, 0)];

if (onlyOnePage) {

[_scrollView setContentSize:CGSizeMake(_scrollView.size.width, _scrollView.size.height)];

}

}

/**

*  设置scrollView的content数据源,即contentViews

*/

- (void)setScrollViewContentDataSource{

NSInteger previousPageIndex = [self getValidNextPageIndexWithPageIndex:self.currentPageIndex - 1];

NSInteger rearPageIndex = [self getValidNextPageIndexWithPageIndex:self.currentPageIndex + 1];

if (self.contentViews == nil) {

self.contentViews = [@[] mutableCopy];

}

[self.contentViews removeAllObjects];

if (self.fetchContentViewAtIndex) {

[self.contentViews addObject:self.fetchContentViewAtIndex(previousPageIndex)];

[self.contentViews addObject:self.fetchContentViewAtIndex(_currentPageIndex)];

[self.contentViews addObject:self.fetchContentViewAtIndex(rearPageIndex)];

}

}

- (NSInteger)getValidNextPageIndexWithPageIndex:(NSInteger)currentPageIndex;

{

if(currentPageIndex == -1) {

return self.totalPageCount - 1;

} else if (currentPageIndex == self.totalPageCount) {

return 0;

} else {

return currentPageIndex;

}

}

#pragma mark -

#pragma mark - UIScrollViewDelegate

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView

{

[self.animationTimer pauseTimer];

}

- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate

{

[self.animationTimer resumeTimerAfterTimeInterval:self.animationDuration];

}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView

{

int contentOffsetX = scrollView.contentOffset.x;

if(contentOffsetX >= (2 * CGRectGetWidth(scrollView.frame))) {

self.currentPageIndex = [self getValidNextPageIndexWithPageIndex:self.currentPageIndex + 1];

self.pageControl.currentPage = self.currentPageIndex;

//        NSLog(@"next,当前页:%d",self.currentPageIndex);

[self configContentViews];

}

if(contentOffsetX <= 0) {

self.currentPageIndex = [self getValidNextPageIndexWithPageIndex:self.currentPageIndex - 1];

self.pageControl.currentPage = self.currentPageIndex;

//        NSLog(@"previous,当前页:%d",self.currentPageIndex);

[self configContentViews];

}

}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView

{

[scrollView setContentOffset:CGPointMake(CGRectGetWidth(scrollView.frame), 0) animated:YES];

}

#pragma mark -

#pragma mark - 响应事件

- (void)animationTimerDidFired:(NSTimer *)timer

{

CGPoint newOffset = CGPointMake(self.scrollView.contentOffset.x + CGRectGetWidth(self.scrollView.frame), self.scrollView.contentOffset.y);

[self.scrollView setContentOffset:newOffset animated:YES];

}

- (void)contentViewTapAction:(UITapGestureRecognizer *)tap

{

if (self.TapActionBlock) {

self.TapActionBlock(self.currentPageIndex);

}

}

#pragma mark - 暂停  开始

- (void)pause{

[self.animationTimer pauseTimer];

}

- (void)restart{

[self.animationTimer resumeTimer];

}

@end

CycleScrollView实现轮播图的更多相关文章

  1. 李洪强iOS开发之使用CycleScrollView实现轮播图

    01 导入头文件,并且定义CycleScrollView属性 02 初始化,设置frame并且添加到collectionView上 03 调用方法并且设置轮播的图片

  2. iOS开发之 用第三方类库实现轮播图

    在github上面有很多的第三方类库,大大节约了大家的开发时间 下载地址:https://github.com/gsdios/SDCycleScrollView 现已支持cocoapods导入:pod ...

  3. js 基础篇(点击事件轮播图的实现)

    轮播图在以后的应用中还是比较常见的,不需要多少行代码就能实现.但是在只掌握了js基础知识的情况下,怎么来用较少的而且逻辑又简单的方法来实现呢?下面来分析下几种不同的做法: 1.利用位移的方法来实现 首 ...

  4. 实现下来ScrollView放大轮播图

    创建工程,创建一个UIScrollView属性,并遵循其协议: #define kWidth self.view.frame.size.width//屏幕宽 #define kHeight self. ...

  5. ViewPager轮播图

    LoopViewPagerLayout无限轮播 项目地址:https://github.com/why168/LoopViewPagerLayout 支持三种动画: 支持修改轮播的速度: 支持修改滑动 ...

  6. CSS-用伪类制作小箭头(轮播图的左右切换btn)

    先上学习地址:http://www.htmleaf.com/Demo/201610234136.html 作者对轮播图左右按钮的处理方法一改往常,不是简单地用btn.prev+btn.next的图片代 ...

  7. phpcms首页实现轮播图

    1.在你想要加轮播图的位置加入以下 <div id="flowDiagram" > <div id="button"> <span ...

  8. React视角下的轮播图

    天猫购物网站最显眼的就是轮播图了.我在学习一样新js库,一个新框架或新的编程思想的时候,总是感叹"入门必做选项卡,进阶须撸轮播图."作为一个React组件,它是状态操控行为的典型, ...

  9. Jquery 轮播图简易框架

    =====================基本结构===================== <div class="carousel" style="width: ...

随机推荐

  1. Java client 访问 memcached

    在测试项目中引入了memcached作为缓存层,以下是memcached的缓存配置和调用过程. linux下memcached安装过程 直接参考以前的博文linux下安装memcached过程  不再 ...

  2. My97DatePicker 没有权限问题

    引自:http://blog.sina.com.cn/s/blog_4b7809800100wkv4.html 今天遇到了My97DatePicker在不同IE版本中使用时有时出现没有权限错误的问题, ...

  3. SecurityException:Not allowed to start service Intent ,without permission not exported from

    本来是学长以前的项目,我正在重做一遍.结果突然出现了异常,我很是不解啊,怎么莫名其妙的就出现异常了呢?我昨天用还是好好的,根本就没动过源代码.于是在网上开始了一遍又一遍的查询,有的说要加权限.有的说这 ...

  4. 几种工具反编译被编译好的DLL文件

    我们平时在工作中经常会遇到一些已经被编译后的DLL,而且更加麻烦是没有源代码可以进行修改,只能针对这个DLL的文件进行修改才能得到我们想要的结果:本文将通过一个实例来演示如果完成一个简单的修改;我们将 ...

  5. DLL使用总结

    最近项目中使用到了DLL,因此就把最近一段时间的学习总结一下,以备不时之需. 一.相关概念 1.动态链接库 自从微软推出第一个版本的Windows操作系统以来,动态链接库(DLL)一直是Windows ...

  6. java swing窗口放置屏幕中央问题思考

    java swing窗口放置屏幕中央问题思考 以前总是尝试各种方法都没有能把组件放到屏幕中央,只能用死办法,设置绝对坐标,但这样就失去了可移植性,而且繁琐.今天仔细思考了一番,终于被我找出问题所在. ...

  7. ServiceStack.Redis 破解

    在github上下载了ServiceStack.Redis,做测试发现有限制,居然从v4开始就收费,无聊时,做了个源码分析 废话不多,上测试代码 try { ; i < ; i++) { red ...

  8. 02_Jquery_02_元素选择器

    [简述] 元素选择器就是通过元素名来查询元素 $("elementName")这里就可以通过元素名来获取jquery元素了. 但与id选择器不同的是,名称相同的元素有很多,所以获取 ...

  9. IOS 学习笔记 2015-03-27 我理解的OC-代理模式

    案例1 KCButton.h // // KCButton.h // Protocol&Block&Category // // Created by Kenshin Cui on 1 ...

  10. ASP.NET数据绑定控件

    数据绑定控件简介 数据绑定分为:数据源 和 数据绑定控件 两部分,数据绑定控件通过数据源来获得数据,通过数据源来隔离数据提供者和数据使用者,数据源有:SqlDataSource,AccessDataS ...