轮播图播放的主要技术在于:

cell的封装。这里采用UICollectionViewCell实现。

#import <UIKit/UIKit.h>

@interface CircleViewCell : UICollectionViewCell

@property (nonatomic, strong) UIImage* image;

@property (nonatomic, assign) NSInteger index;

@end

//

//  CircleViewCell.m

//  lunbo

//

//  Created by  JIAOXIANGJIE on 16/10/12.

//  Copyright © 2016年 lumen. All rights reserved.

//

#import "CircleViewCell.h"

@interface CircleViewCell()

@property (nonatomic, strong) UIImageView* imageview;

@end

@implementation CircleViewCell

-(instancetype)init{

if(self = [super init]){

[self myInit];

}

return self;

}

-(instancetype)initWithCoder:(NSCoder *)aDecoder

{

if(self = [super initWithCoder:aDecoder])

[self myInit];

return self;

}

-(instancetype)initWithFrame:(CGRect)frame

{

if(self = [super initWithFrame:frame])

[self myInit];

return self;

}

-(void)layoutSubviews

{

[super layoutSubviews];

self.imageview.frame = self.contentView.bounds;

}

-(UIImageView*)imageview

{

if(_imageview == nil){

_imageview = [[UIImageView alloc] init];

_imageview.contentMode = UIViewContentModeScaleAspectFill;

_imageview.userInteractionEnabled = YES;

_imageview.backgroundColor = [UIColor whiteColor];

}

return _imageview;

}

-(void)setImage:(UIImage *)image

{

_image = image;

self.imageview.image = image;

}

//

//  CircleScrollView.h

//  lunbo

//

//  Created by  JIAOXIANGJIE on 16/10/12.

//  Copyright © 2016年 lumen. All rights reserved.

//

#import <UIKit/UIKit.h>

@class CircleScrollView;

@protocol CircleScrollViewDelegate <NSObject>

- (void)didClickImageAtIndex:(NSInteger)index scrollView:(CircleScrollView *)scrollView;

@end

@interface CircleScrollView : UIView

@property (nonatomic, weak) id<CircleScrollViewDelegate> delegate;//设置代理

@property (nonatomic, assign) NSTimeInterval duringTimel;//间隔时间

-(void)images:(NSArray*)images;

-(void)closeTimer;

-(void)openTimer;

@end

-(void)myInit{

[self.contentView addSubview:self.imageview];

}

@end

//

//  CircleScrollView.m

//  lunbo

//

//  Created by  JIAOXIANGJIE on 16/10/12.

//  Copyright © 2016年 lumen. All rights reserved.

//

#import "CircleScrollView.h"

#import "CircleViewCell.h"

@interface CircleScrollView()<UICollectionViewDataSource,UICollectionViewDelegate>

@property (nonatomic, strong) UICollectionView* collectionView;

@property (nonatomic, strong) NSArray* images;//图片数组

@property (nonatomic, assign) NSInteger imagecount;//图片数量

@property (nonatomic, strong) NSMutableArray* cellData;//数组

@property (nonatomic, strong) UIPageControl* pageControl;

@property (nonatomic, strong) NSTimer* timer;

@property (nonatomic, strong) NSLock* mlock;//加锁 用于多线程

@end

@implementation CircleScrollView

static NSString *CollectionCellID = @"CollectionCellID";

-(instancetype)initWithFrame:(CGRect)frame

{

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

//初始化数据信息

UICollectionViewFlowLayout* flowLayout = [[UICollectionViewFlowLayout alloc] init];

flowLayout.itemSize = frame.size;

flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;

flowLayout.minimumLineSpacing = 0;

flowLayout.minimumInteritemSpacing = 0;

flowLayout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);

self.collectionView = [[UICollectionView alloc] initWithFrame:self.bounds

collectionViewLayout:flowLayout];

self.collectionView.delegate = self;

self.collectionView.dataSource = self;

self.collectionView.pagingEnabled = YES;

self.collectionView.showsHorizontalScrollIndicator = NO;

self.collectionView.showsVerticalScrollIndicator = NO;

self.collectionView.alwaysBounceHorizontal = YES;

self.collectionView.alwaysBounceVertical = NO;

self.collectionView.backgroundColor = [UIColor whiteColor];

[self.collectionView registerClass:[CircleViewCell class]

forCellWithReuseIdentifier:CollectionCellID];

[self addSubview:self.collectionView];

self.pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake( 0, self.bounds.size.height - 30, self.bounds.size.width, 30)];

self.pageControl.tag = 100;

self.pageControl.userInteractionEnabled = NO;

[self addSubview:self.pageControl];

[self bringSubviewToFront:self.pageControl];

self.backgroundColor = [UIColor whiteColor];

self.mlock = [[NSLock alloc] init];

}

return self;

}

-(void)setDuringTimel:(NSTimeInterval)duringTimel

{

_duringTimel = duringTimel;

if(duringTimel < 0.001)

return;

[self closeTimer];

[self openTimer];

}

-(void)images:(NSArray*)images

{

[self.mlock lock];

[self closeTimer];

_images = images;

_imagecount = images.count;

self.cellData = [[NSMutableArray alloc] init];

for(NSInteger i = 0; i < 100; i++){

for (NSInteger j = 0; j < _imagecount; j++) {

[self.cellData addObject:@(j)];

}

}

[self.collectionView reloadData];

[self.collectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForRow:50 * _imagecount inSection:0]

atScrollPosition:UICollectionViewScrollPositionLeft

animated:NO];

//设置

self.pageControl.hidden = _imagecount > 0 ? NO: YES;

self.pageControl.numberOfPages = _imagecount;

self.pageControl.currentPage = 0;

[self openTimer];

[self.mlock unlock];

}

-(void)closeTimer

{

if(self.timer)

[self.timer invalidate];

}

-(void)openTimer

{

if(_duringTimel > 0.8)

self.timer = [NSTimer scheduledTimerWithTimeInterval:_duringTimel

target:self

selector:@selector(onTimer)

userInfo:nil

repeats:YES];

}

-(void)onTimer

{

if(self.cellData.count > 0){

NSArray* array = [self.collectionView indexPathsForVisibleItems];

if(array.count == 0)

return;

NSIndexPath* indexPath = array[0];

NSInteger row = indexPath.row;

if(row % _imagecount == 0){

row = 50 * _imagecount;

[self.collectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForRow:row inSection:0] atScrollPosition:UICollectionViewScrollPositionLeft animated:NO];

}

[self.collectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForRow:row + 1 inSection:0] atScrollPosition:UICollectionViewScrollPositionLeft animated:YES];

self.pageControl.currentPage = (row + 1) % _imagecount;

}

}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {

return 1;

}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {

return self.cellData.count;

}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath

{

CircleViewCell* cell = [collectionView dequeueReusableCellWithReuseIdentifier:CollectionCellID forIndexPath:indexPath];

NSInteger index = [_cellData[indexPath.row] integerValue];

cell.image = _images[index];

cell.index = index;

return cell;

}

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

// 动画停止, 重新定位到第 50 组模型

int inc = ((int)(scrollView.contentOffset.x / scrollView.frame.size.width)) % _imagecount;

[self.collectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForRow:50 * _imagecount + inc inSection:0] atScrollPosition:UICollectionViewScrollPositionLeft animated:NO];

// 设置 PageControl

self.pageControl.currentPage = inc;

}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {

CircleViewCell *cell = (CircleViewCell *)[collectionView cellForItemAtIndexPath:indexPath];

if ([self.delegate respondsToSelector:@selector(didClickImageAtIndex:scrollView:)]) {

[self.delegate didClickImageAtIndex:cell.index scrollView:self];

}

}

@end

//

//  ViewController.m

//  lunbo

//

//  Created by  JIAOXIANGJIE on 16/10/12.

//  Copyright © 2016年 lumen. All rights reserved.

//

#import "ViewController.h"

#import "CircleScrollView.h"

@interface ViewController ()<CircleScrollViewDelegate>

@end

@implementation ViewController

- (void)viewDidLoad {

[super viewDidLoad];

self.view.backgroundColor = [UIColor lightGrayColor];

CGSize size = [UIScreen mainScreen].bounds.size;

CircleScrollView* scrollview = [[CircleScrollView alloc] initWithFrame:CGRectMake(0, 20, size.width, size.width * 504 / 1080)];

scrollview.delegate = self;

[scrollview images:@[[UIImage imageNamed:@"photo_loading"]]];      // 占位图

[self.view addSubview:scrollview];

UIImage *m1 = [UIImage imageNamed:@"m1.jpg"];

UIImage *m2 = [UIImage imageNamed:@"m2.jpg"];

UIImage *m3 = [UIImage imageNamed:@"m3.jpg"];

UIImage *m4 = [UIImage imageNamed:@"m4.jpg"];

UIImage *m5 = [UIImage imageNamed:@"m5.jpg"];

CircleScrollView* scroll1 = [[CircleScrollView alloc] initWithFrame:CGRectMake(0, 220, size.width, size.width * 504 / 1080)];

scroll1.delegate = self;

scroll1.duringTimel = 1.0;

[scroll1 images:@[m1,m2]];

CircleScrollView* view = [[CircleScrollView alloc] initWithFrame:CGRectMake( 0, 420, size.width, size.width * 504 / 1080)];

view.delegate = self;

[view images:@[m1, m2, m3, m4, m5]];

[self.view addSubview:view];

}

- (void)didReceiveMemoryWarning {

[super didReceiveMemoryWarning];

}

- (void)didClickImageAtIndex:(NSInteger)index scrollView:(CircleScrollView *)scrollView {

NSLog(@"%li", index);

}

@end

效果图:

IOS轮播图的更多相关文章

  1. 一步一步拆解一个简单的iOS轮播图(三图)

    导言(可以不看): 不吹不黑,也许是东半球最简单的iOS轮播图拆分注释(讲解不敢当)了(tree new bee).(一句话包含两个人,你能猜到有谁吗?提示:一个在卖手机,一个最近在卖书)哈哈... ...

  2. iOS -- 轮播图

    UIScrollView + 多张 ImageView 实现轮播 实现原理: 将所有图片的名字储存在数组 imageAry 中,imageAry 的元素个数为 num,在 scrollView 上添加 ...

  3. ReactNative新手学习之路04 组件化开发轮播图swiper支持安卓和IOS

    react native 新手之路04 组件化开发轮播图swiper支持安卓和IOS npm install react-native-carousel --save git 地址Properties ...

  4. iOS 图片轮播图(自动滚动)

    iOS 图片轮播图(自动滚动) #import "DDViewController.h" #define DDImageCount 5 @interface DDViewContr ...

  5. iOS回顾笔记(05) -- 手把手教你封装一个广告轮播图框架

    html,body,div,span,applet,object,iframe,h1,h2,h3,h4,h5,h6,p,blockquote,pre,a,abbr,acronym,address,bi ...

  6. iOS最笨的办法实现无限轮播图(网络加载)

    iOS最笨的办法实现无限轮播图(网络加载) 简单的做了一下: 使用方法: 把 请求返回的 图片地址(字符串类型)放进数组中就行 可以使用SDWebImage(我就是用的这个)等..需要自己导入并引用, ...

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

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

  8. iOS中 轮播图放哪最合适? 技术分享

    我们知道,轮播图放在cell或collectionViewCell上会影响用户层级交互事件,并且实现起来比较麻烦,现在推出一个技术点:答题思路是:将UIScrollView放在UIView或UICol ...

  9. iOS swift版本无限滚动轮播图

    之前写过oc版本的无限滚动轮播图,现在来一个swift版本全部使用snapKit布局,数字还是pageConrrol样式可选 enum typeStyle: Int { case pageContro ...

随机推荐

  1. CSS Table Gallery

    http://icant.co.uk/csstablegallery/tables/99.php

  2. Java 实现文件上传、下载、打包、文件copy、文件夹copy。

    文件and文件夹copy package org.test; import java.io.*; public class FileCopy { /** * 复制单个文件 * * @param old ...

  3. C# 可视化读取文件、文件夹

    OpenFileDialog fd = new OpenFileDialog(); fd.Filter = "txt files (*.txt)|*.txt|All files(*.*)|* ...

  4. jvm的垃圾回收原理

    什么是垃圾回收? 垃圾回收是Java中自动内存管理的另一种叫法.垃圾回收的目的是为程序保持尽可能多的可用堆(heap). JVM会删除堆上不再需要从堆引用的对象. 用一个例子解释垃圾回收? 比方说,下 ...

  5. 用介个新的blog咯..

    之前csdn实在是太卡了.. 只要一写比较长的blog就卡的要死.. 转过来这吧,比较好吧.. 原blog地址 啊为啥域名叫darklove呢.. 这是很久之前创建的.. 简单来说是一个和clearl ...

  6. 点 击 直 接加我QQ的功能

    <a target="_blank" href="tencent://message/?uin=2814920598&Site=&Menu=yes& ...

  7. [NOIP2011]Mayan游戏 题解

    题目大意: 有一个5*7的方格,上面有几种颜色的方块,如果在一横行或者竖列上有连续三个或者三个以上相同颜色的方块,则它们将立即被消除,方块消除之后,消除位置之上的方块将掉落.每步移动可以且仅可以沿横向 ...

  8. java并发编程(十)使用wait/notify/notifyAll实现线程间通信

    转载请注明出处:http://blog.csdn.net/ns_code/article/details/17225469 wait()方法:public final void wait()  thr ...

  9. 第一章-第七题( 有人认为,“中文编程”, 是解决中国程序员编程效率一个秘密武器,请问它是一个 “银弹” 么? )--By 侯伟婷

    首先,“银弹”在百度百科中的解释是银色的子弹,我们更熟知的“银弹”一词,应该是在<人月神话>中提到的.银弹原本应该是指某种策略.技术或者技巧可以极大地提高程序员的生产力[1].此题目中关于 ...

  10. 本地测试SMTP服务器

    一年前看计算机网络里的电子邮件协议的时候,想自己建一个本地SMTP服务器,然后用telnet发送消息,后来是没找到.这次又翻看了一下,找了个Windows平台下的hMailServer,挺方便的.步骤 ...