您的位置:首页 > 产品设计 > UI/UE

iOS 循环滚动的UIScrollView

2014-05-12 14:00 477 查看
声明文件CircleScrollView.h

#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];
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息