iOS 循环滚动的UIScrollView
2014-05-12 14:00
477 查看
声明文件CircleScrollView.h
实现文件CircleScrollView.m
#import "CircleScrollView.h"
@implementation CircleScrollView
{
UIScrollView *_scrollView;
NSArray *_dataArray;
int curPage;
}
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
}
return self;
}
- (id)initWithFrame:(CGRect)frame andImageNames:(NSArray *) array
{
self = [super initWithFrame:frame];
curPage = 1;
_dataArray = [[NSArray alloc] initWithArray:array];
[self createUI];
return self;
}
- (void)createUI
{
_scrollView = [[UIScrollView alloc] initWithFrame:self.bounds];
//_scrollView.scrollEnabled = NO不让用户自己点击图片滚动,下面定义了NSTimer来自动滚动
_scrollView.scrollEnabled = YES;
_scrollView.contentSize = CGSizeMake(_scrollView.bounds.size.width * 3, _scrollView.bounds.size.height);
_scrollView.showsHorizontalScrollIndicator = NO;
_scrollView.showsVerticalScrollIndicator = NO;
_scrollView.pagingEnabled = YES;
_scrollView.delegate = self;
_scrollView.contentOffset = CGPointMake(_scrollView.bounds.size.width, 0);
[self addSubview:_scrollView];
[self refreshScrollView:[self getDisplayImages]];
//[NSTimer scheduledTimerWithTimeInterval:1.0f/60 target:self selector:@selector(autoPlayScrollView) userInfo:nil repeats:YES];
}
- (void)autoPlayScrollView
{
_scrollView.contentOffset = CGPointMake(_scrollView.contentOffset.x + 2.0f, 0);
if (_scrollView.contentOffset.x == _scrollView.bounds.size.width * 2) {
curPage ++;
[self refreshScrollView:[self getDisplayImages]];
_scrollView.contentOffset = CGPointMake(_scrollView.bounds.size.width, 0);
}
if (_scrollView.contentOffset.x == 0) {
curPage --;
[self refreshScrollView:[self getDisplayImages]];
_scrollView.contentOffset = CGPointMake(_scrollView.bounds.size.width, 0);
}
}
- (NSArray *)getDisplayImages
{
NSString *prepath = nil;
NSString *curPath = nil;
NSString *nexPath = nil;
if (curPage == _dataArray.count - 1) {
prepath = _dataArray[curPage - 1];
curPath = _dataArray[curPage];
nexPath = _dataArray[0];
}else if(curPage == _dataArray.count){
prepath = _dataArray[curPage - 1];
curPage = 0;
curPath = _dataArray[curPage];
nexPath = _dataArray[curPage + 1];
}else if(curPage == 0){
prepath = _dataArray[_dataArray.count - 1];
curPath = _dataArray[curPage];
nexPath = _dataArray[curPage + 1];
}
else if(curPage == -1){
nexPath = _dataArray[curPage + 1];
curPage = _dataArray.count - 1;
prepath = _dataArray[curPage - 1];
curPath = _dataArray[curPage];
}else{
prepath = _dataArray[curPage - 1];
curPath = _dataArray[curPage];
nexPath = _dataArray[curPage + 1];
}
return @[prepath, curPath, nexPath];
}
- (void)refreshScrollView:(NSArray *)array
{
NSArray *subViews = [_scrollView subviews];
if ([subViews respondsToSelector:@selector(makeObjectsPerformSelector:)]) {
[subViews makeObjectsPerformSelector:@selector(removeFromSuperview)];
}
int index = 0;
for (NSString *path in array) {
double w = _scrollView.bounds.size.width;
double h = _scrollView.bounds.size.height;
double x = w * index;
double y = 0;
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(x, y, w, h)];
UIImage *image = [UIImage imageNamed:path];
imageView.image = image;
[_scrollView addSubview:imageView];
index ++;
}
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
if (_scrollView.contentOffset.x == _scrollView.bounds.size.width * 2) {
curPage ++;
[self refreshScrollView:[self getDisplayImages]];
_scrollView.contentOffset = CGPointMake(_scrollView.bounds.size.width, 0);
}
if (_scrollView.contentOffset.x == 0) {
curPage --;
[self refreshScrollView:[self getDisplayImages]];
_scrollView.contentOffset = CGPointMake(_scrollView.bounds.size.width, 0);
}
}
@end
在任意一个controller中调用如下:
- (void)viewDidLoad
{
[super viewDidLoad];
NSMutableArray *array = [[NSMutableArray alloc] init];
for (int i = 0; i < 10; i ++) {
NSString *path = [NSString stringWithFormat:@"2_%d.jpg", i + 1];
[array addObject:path];
}
CircleScrollView *circleScrollView = [[CircleScrollView alloc] initWithFrame:CGRectMake(10, 50, 300, 100) andImageNames:array];
[self.view addSubview:circleScrollView];
}
#import <UIKit/UIKit.h> #pragma mark - 循环向左滚动的ScrollView @interface CircleScrollView : UIView<UIScrollViewDelegate> #pragma mark - frame: 滚动视图的frame; array:存有image地址的数组 - (id)initWithFrame:(CGRect)frame andImageNames:(NSArray *) array; @end
实现文件CircleScrollView.m
#import "CircleScrollView.h"
@implementation CircleScrollView
{
UIScrollView *_scrollView;
NSArray *_dataArray;
int curPage;
}
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
}
return self;
}
- (id)initWithFrame:(CGRect)frame andImageNames:(NSArray *) array
{
self = [super initWithFrame:frame];
curPage = 1;
_dataArray = [[NSArray alloc] initWithArray:array];
[self createUI];
return self;
}
- (void)createUI
{
_scrollView = [[UIScrollView alloc] initWithFrame:self.bounds];
//_scrollView.scrollEnabled = NO不让用户自己点击图片滚动,下面定义了NSTimer来自动滚动
_scrollView.scrollEnabled = YES;
_scrollView.contentSize = CGSizeMake(_scrollView.bounds.size.width * 3, _scrollView.bounds.size.height);
_scrollView.showsHorizontalScrollIndicator = NO;
_scrollView.showsVerticalScrollIndicator = NO;
_scrollView.pagingEnabled = YES;
_scrollView.delegate = self;
_scrollView.contentOffset = CGPointMake(_scrollView.bounds.size.width, 0);
[self addSubview:_scrollView];
[self refreshScrollView:[self getDisplayImages]];
//[NSTimer scheduledTimerWithTimeInterval:1.0f/60 target:self selector:@selector(autoPlayScrollView) userInfo:nil repeats:YES];
}
- (void)autoPlayScrollView
{
_scrollView.contentOffset = CGPointMake(_scrollView.contentOffset.x + 2.0f, 0);
if (_scrollView.contentOffset.x == _scrollView.bounds.size.width * 2) {
curPage ++;
[self refreshScrollView:[self getDisplayImages]];
_scrollView.contentOffset = CGPointMake(_scrollView.bounds.size.width, 0);
}
if (_scrollView.contentOffset.x == 0) {
curPage --;
[self refreshScrollView:[self getDisplayImages]];
_scrollView.contentOffset = CGPointMake(_scrollView.bounds.size.width, 0);
}
}
- (NSArray *)getDisplayImages
{
NSString *prepath = nil;
NSString *curPath = nil;
NSString *nexPath = nil;
if (curPage == _dataArray.count - 1) {
prepath = _dataArray[curPage - 1];
curPath = _dataArray[curPage];
nexPath = _dataArray[0];
}else if(curPage == _dataArray.count){
prepath = _dataArray[curPage - 1];
curPage = 0;
curPath = _dataArray[curPage];
nexPath = _dataArray[curPage + 1];
}else if(curPage == 0){
prepath = _dataArray[_dataArray.count - 1];
curPath = _dataArray[curPage];
nexPath = _dataArray[curPage + 1];
}
else if(curPage == -1){
nexPath = _dataArray[curPage + 1];
curPage = _dataArray.count - 1;
prepath = _dataArray[curPage - 1];
curPath = _dataArray[curPage];
}else{
prepath = _dataArray[curPage - 1];
curPath = _dataArray[curPage];
nexPath = _dataArray[curPage + 1];
}
return @[prepath, curPath, nexPath];
}
- (void)refreshScrollView:(NSArray *)array
{
NSArray *subViews = [_scrollView subviews];
if ([subViews respondsToSelector:@selector(makeObjectsPerformSelector:)]) {
[subViews makeObjectsPerformSelector:@selector(removeFromSuperview)];
}
int index = 0;
for (NSString *path in array) {
double w = _scrollView.bounds.size.width;
double h = _scrollView.bounds.size.height;
double x = w * index;
double y = 0;
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(x, y, w, h)];
UIImage *image = [UIImage imageNamed:path];
imageView.image = image;
[_scrollView addSubview:imageView];
index ++;
}
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
if (_scrollView.contentOffset.x == _scrollView.bounds.size.width * 2) {
curPage ++;
[self refreshScrollView:[self getDisplayImages]];
_scrollView.contentOffset = CGPointMake(_scrollView.bounds.size.width, 0);
}
if (_scrollView.contentOffset.x == 0) {
curPage --;
[self refreshScrollView:[self getDisplayImages]];
_scrollView.contentOffset = CGPointMake(_scrollView.bounds.size.width, 0);
}
}
@end
在任意一个controller中调用如下:
- (void)viewDidLoad
{
[super viewDidLoad];
NSMutableArray *array = [[NSMutableArray alloc] init];
for (int i = 0; i < 10; i ++) {
NSString *path = [NSString stringWithFormat:@"2_%d.jpg", i + 1];
[array addObject:path];
}
CircleScrollView *circleScrollView = [[CircleScrollView alloc] initWithFrame:CGRectMake(10, 50, 300, 100) andImageNames:array];
[self.view addSubview:circleScrollView];
}
相关文章推荐
- iOS开发-------滚动视图(UIScrollView)并实现循环滚动
- IOS开发学习笔记036-UIScrollView-循环自动滚动
- iOS之UIScrollView循环滚动
- iOS UISCrollView循环滚动 实现
- iOS uiscrollview 自动循环滚动
- iOS UIScrollView 循环滚动(loop)
- iOS UISCrollView循环滚动 实现
- iOS UIScrollView无限循环滚动和手动滚动
- ios UIScrollView自动滚动 循环滚动视图实现
- iOS UIImageView利用UIScrollView实现图片循环滚动
- 利用UIScrollView实现循环滚动翻页的方法
- ios中广告滚动页面(uiscrollview)
- IOS UI-滚动视图(UIScrollView)
- ios开发基础--滚动视图(UIScrollView)
- 李洪强iOS开发之后使用XIB实现横向滚动的UIScrollView
- IOS UIScrollView 自动布局的 滚动问题
- iOS-利用UIScrollView实现展示图片的无限滚动及自动滚动
- iOS实现无限循环滚动的TableView实战教程
- iOS开发——解决UIScrollView控件移动错位和无法滚动
- iOS滚动视图UIScrollView使用方法