(廣告欄常用的)循環滾動的scrollview

#import <UIKit/UIKit.h>

typedefenum_CycleDirection

{ PortaitDirection,LandscapeDirection } CycleDirection;

@interfaceCycleScrollView : UIView<UIScrollViewDelegate> {

UIScrollView*scrollView;

UIImageView*curImageView;

inttotalPage;  

intcurPage;

CGRectscrollFrame;

CycleDirectionscrollDirection; // scrollView滾動的方向

NSArray*p_w_picpathsArray;  // 存放所有需要滾動的圖片

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];

p_w_picpathsArray=[[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:[p_w_picpathsArrayobjectAtIndex:pre-1]];

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

[curImagesaddObject:[p_w_picpathsArrayobjectAtIndex: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

{

[p_w_picpathsArrayrelease];

[curImagesrelease];

[superdealloc];

}

@end

轉載自http://blog.sina.com.cn/s/blog_93f39bc20100vgsd.html

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章