循环滚动UIScrollView(无点击事件处理)

1.创建一个继承UIView的名为CycleScrollView的类(.h和.m文件)
2.将下面代码替换原默认代码
3.在你的类里面实例化一个CycleScrollView类的对象并用

-(id)initWithFrame:(CGRect)framecycleDirection:(CycleDirection)directionpictures: (NSArray*)pictureArray
进行初始化
参数介绍:
frame 页面的位置和大小
direction 滚动的方向,0垂直滚动,1水平滚动
pictures 滚动的图片数组
比如:

 
- (void)viewDidLoad
 
{
 
[super viewDidLoad];
 
NSArray *imagesArray=[[NSArray alloc] initWithObjects:[UIImage           imageNamed:@"test1.jpg"],[UIImageimageNamed:@"test2.jpg"],[UIImageimageNamed:@"test3.jpg"],nil];
 
CycleScrollView *cycle=[[CycleScrollView alloc]initWithFrame:CGRectMake(0, 0, 320, 480)     cycleDirection:0pictures:imagesArray];
 
cycle.delegate=self;
 
[self.view addSubview:cycle];
 
[imagesArray release];
 
[cycle release];
 
}
/////////////////////////////////////////////////////////////////////////////

.h文件
#import <UIKit/UIKit.h>

typedef 
enum _CycleDirection
{ PortaitDirection,LandscapeDirection } CycleDirection;

@interface CycleScrollView :UIView<UIScrollViewDelegate> {

 
UIScrollView*scrollView;
 
UIImageView*curImageView;
 
inttotalPage; 
 
intcurPage;
 
CGRectscrollFrame;
 
CycleDirectionscrollDirection; // scrollView滚动的方向
 
NSArray*imagesArray;  //存放所有需要滚动的图片
 
NSMutableArray *curImages; //存放当前滚动的三张图片
}
- (int) validPageValue:(NSInteger)value;

- (id) initWithFrame:(CGRect)framecycleDirection:(CycleDirection)directionpictures:(NSArray*)pictureArray;

- (NSArray*) getDisplayImagesWithCurp
age:(int)page;
- (void) refreshScrollView;

@end

/////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////

.m文件
#import "CycleScrollView.h"

@implementation CycleScrollView

- (id) initWithFrame:(CGRect)framecycleDirection:(CycleDirection)directionpictures:(NSArray*)pictureArray

{

 
self=[superinitWithFrame:frame];
 
if(self)
 
{
 
scrollFrame=frame;
 
scrollDirection=direction;
 
totalPage=[pictureArray count];
 
curPage=1;  //当前显示的是图片数组里的第一张图片
 
curImages=[[NSMutableArray alloc] init];
 
imagesArray=[[NSArray alloc] initWithArray:pictureArray];
 
scrollView=[[UIScrollView alloc] initWithFrame:frame];
 
scrollView.backgroundColor=[UIColor blueColor];
 
scrollView.showsHorizontalScrollIndicator=NO;
 
scrollView.showsVerticalScrollIndicator=NO;
 
scrollView.pagingEnabled=YES;
 
scrollView.delegate=self;
 
[self addSubview:scrollView];
 
if(scrollDirection==PortaitDirection) //在竖直方向滚动
 
{
 
scrollView.contentSize=CGSizeMake(scrollView.frame.size.width,scrollView.frame.size.height*3); //竖直方法可以存放三张图片
 
}
 
if(scrollDirection==LandscapeDirection) //在水平方向滚动
 
{
 
scrollView.contentSize=CGSizeMake(scrollView.frame.size.width*3,scrollView.frame.size.height);
 
}
 
[self addSubview:scrollView];
 
[self refreshScrollView];
 
}
 
returnself;
}
- (void) refreshScrollView

{

 
NSArray*subViews=[scrollView subviews];
 
if([subViewscount]!=0)
 
{
 
[subViewsmakeObjectsPerformSelector:@selector(removeFromSuperview)];
 
}
 
[selfgetDisplayImagesWithCurpage:curPage];
  
UIImage*preImage=[curImages objectAtIndex:0];
 
UIImage*curImage=[curImages objectAtIndex:1];
 
UIImage*lastImage=[curImages objectAtIndex:2];
 
UIImageView*preView=[[UIImageView alloc] initWithImage:preImage];
 
UIImageView*curView=[[UIImageView alloc] initWithImage:curImage];
 
UIImageView*lastView=[[UIImageView alloc] initWithImage:lastImage];
 
[scrollViewaddSubview:preView];
 
[scrollViewaddSubview:curView];
 
[scrollViewaddSubview:lastView];
 
[preView  release];
 
[curView  release];
 
[lastView  release];
 
if(scrollDirection==PortaitDirection) //竖直滚动
 
{
 
preView.frame=CGRectOffset(preView.frame, 0, 0);
     
curView.frame=CGRectOffset(curView.frame, 0,scrollFrame.size.height);
 
lastView.frame=CGRectOffset(lastView.frame, 0,2*scrollFrame.size.height);
 
[scrollView setContentOffset:CGPointMake(0,scrollFrame.size.height)];
 
}
 
if(scrollDirection==LandscapeDirection) //水平滚动
 
{
 
preView.frame=CGRectOffset(preView.frame, 0, 0);
 
curView.frame=CGRectOffset(curView.frame, scrollFrame.size.width,0);
 
lastView.frame=CGRectOffset(lastView.frame,scrollFrame.size.width*2, 0);
     
[scrollView setContentOffset:CGPointMake(scrollFrame.size.width,0)];
 
}
}
- (NSArray*) getDisplayImagesWithCurp
age:(int)page
{

 
intpre=[self validPageValue:curPage-1];
 
intlast=[self validPageValue:curPage+1];
 
if([curImagescount]!=0) [curImages removeAllObjects];
 
[curImagesaddObject:[imagesArray objectAtIndex:pre-1]];
 
[curImagesaddObject:[imagesArray objectAtIndex:curPage-1]];
 
[curImagesaddObject:[imagesArray objectAtIndex:last-1]];
 
returncurImages;
}
- (int)validPageValue:(NSInteger)value

{

 
if(value==0) value=totalPage; //value=1为第一张,value=0为前面一张
 
if(value==totalPage+1)
value=1;
 
returnvalue;
}
- (void) scrollViewDidScroll:(UIScrollView *)crollView

{

 
intx=crollView.contentOffset.x;
 
inty=crollView.contentOffset.y;
 
if(scrollDirection==LandscapeDirection) //水平滚动
 
{
 
if(x>=2*scrollFrame.size.width) //往下翻一张
 
{
 
curPage=[self validPageValue:curPage+1];
 
[self refreshScrollView];
 
}
 
if(x<=0)
 
{
 
curPage=[self validPageValue:curPage-1];
    
[self refreshScrollView];
      
}
}
 
//竖直滚动
 
if(scrollDirection==PortaitDirection)
 
{
 
if(y>=2*scrollFrame.size.height){
 
curPage=[self validPageValue:curPage+1];
 
[self refreshScrollView];
 
}
 
if (y<=0) {
    
curPage=[self validPageValue:curPage-1];
 
[self refreshScrollView];
 
}
}
}
- (void)dealloc

{

 
[imagesArray release];
 
[curImages  release];
 
[super  dealloc];
}
@end

posted @ 2015-12-24 09:29  Bo-tree  阅读(146)  评论(0)    收藏  举报