(广告栏常用的)循环滚动的scrollview

#import <UIKit/UIKit.h>

typedefenum_CycleDirection

{ PortaitDirection,LandscapeDirection } CycleDirection;

@interfaceCycleScrollView : UIView<UIScrollViewDelegate> {

UIScrollView*scrollView;

UIImageView*curImageView;

inttotalPage;  

intcurPage;

CGRectscrollFrame;

CycleDirectionscrollDirection; // scrollView滚动的方向

NSArray*imagesArray;  // 存放所有需要滚动的图片

NSMutableArray*curImages; //存放当前滚动的三张图片

}

- (int) validPageValue:(NSInteger)value;

- (id) initWithFrame:(CGRect)frame cycleDirection:(CycleDirection)direction pictures:(NSArray*)pictureArray;

- (NSArray*) getDisplayImagesWithCurpage:(int)page;

- (void) refreshScrollView;

@end


//

// CycleScrollView.m

// CycleScrollView

//

// Created by mini5 on 28/07/2011.

// Copyright 2011 __MyCompanyName__. All rights reserved.

//

#import "CycleScrollView.h"

@implementationCycleScrollView

- (id) initWithFrame:(CGRect)frame cycleDirection:(CycleDirection)direction pictures:(NSArray*)pictureArray

{

self=[superinitWithFrame:frame];

if(self)

{

scrollFrame=frame;

scrollDirection=direction;

totalPage=[pictureArray count];

curPage=1; //当前显示的是图片数组里的第一张图片

curImages=[[NSMutableArrayalloc] init];

imagesArray=[[NSArrayalloc] initWithArray:pictureArray];

scrollView=[[UIScrollViewalloc] initWithFrame:frame];

scrollView.backgroundColor=[UIColorblueColor];

scrollView.showsHorizontalScrollIndicator=NO;

scrollView.showsVerticalScrollIndicator=NO;

scrollView.pagingEnabled=YES;

scrollView.delegate=self;

[selfaddSubview: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);

}

[selfaddSubview:scrollView];

[selfrefreshScrollView];

}

returnself;

}

- (void) refreshScrollView

{

NSArray*subViews=[scrollViewsubviews];

if([subViews count]!=0)

{

[subViews makeObjectsPerformSelector:@selector(removeFromSuperview)];

}

[selfgetDisplayImagesWithCurpage:curPage];

UIImage*preImage=[curImagesobjectAtIndex:0];

UIImage*curImage=[curImagesobjectAtIndex:1];

UIImage*lastImage=[curImagesobjectAtIndex:2];

UIImageView*preView=[[UIImageViewalloc] initWithImage:preImage];

UIImageView*curView=[[UIImageViewalloc] initWithImage:curImage];

UIImageView*lastView=[[UIImageViewalloc] 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);

[scrollViewsetContentOffset: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);

[scrollViewsetContentOffset:CGPointMake(scrollFrame.size.width, 0)];

}

}

- (NSArray*) getDisplayImagesWithCurpage:(int)page

{

intpre=[selfvalidPageValue:curPage-1];

intlast=[selfvalidPageValue:curPage+1];

if([curImagescount]!=0) [curImagesremoveAllObjects];

[curImagesaddObject:[imagesArrayobjectAtIndex:pre-1]];

[curImagesaddObject:[imagesArrayobjectAtIndex:curPage-1]];

[curImagesaddObject:[imagesArrayobjectAtIndex:last-1]];

returncurImages;

}

- (int)validPageValue:(NSInteger)value

{

if(value==0) value=totalPage; //value1为第一张,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=[selfvalidPageValue:curPage+1];

[selfrefreshScrollView];

}

if(x<=0)

{

curPage=[selfvalidPageValue:curPage-1];

[selfrefreshScrollView];

}

}

//竖直滚动

if(scrollDirection==PortaitDirection)

{

if(y==2*scrollFrame.size.height)

{

curPage=[selfvalidPageValue:curPage+1];

[selfrefreshScrollView];

}

}

}

- (void)dealloc

{

[imagesArrayrelease];

[curImagesrelease];

[superdealloc];

}

@end

转载自http://blog.sina.com.cn/s/blog_93f39bc20100vgsd.html

你可能感兴趣的:(scorllview,循环滚动)