UIScrollView实现循环滚动(一)

来源:互联网 发布:java中public static 编辑:程序博客网 时间:2024/05/17 04:38

#import <UIKit/UIKit.h>

 

typedef  enum _CycleDirection

{ PortaitDirection,LandscapeDirection } CycleDirection;

 

 

@interface CycleScrollView : UIView<UIScrollViewDelegate> {

    UIScrollView *scrollView;

    UIImageView *curImageView;

    

    int totalPage;  

    int curPage;

    CGRect scrollFrame;

    

    CycleDirection scrollDirection;    // 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"

 

 

@implementation CycleScrollView

 

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

{

    self=[super initWithFrame:frame];

    if(self)

    {

        scrollFrame=frame;

        scrollDirection=direction;

        totalPage=[pictureArray count];

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

        curImages=[[NSMutableArray allocinit];

        imagesArray=[[NSArray allocinitWithArray:pictureArray];

        

        scrollView=[[UIScrollView allocinitWithFrame: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];

    }

    return self;

}

 

- (void) refreshScrollView

{

    NSArray *subViews=[scrollView subviews];

    if([subViews count]!=0)

    {

        [subViews makeObjectsPerformSelector:@selector(removeFromSuperview)];

    }

    

    [self getDisplayImagesWithCurpage:curPage];

    UIImage *preImage=[curImages objectAtIndex:0];

    UIImage *curImage=[curImages objectAtIndex:1];

    UIImage *lastImage=[curImages objectAtIndex:2];

    UIImageView *preView=[[UIImageView allocinitWithImage:preImage];

    UIImageView *curView=[[UIImageView allocinitWithImage:curImage];

    UIImageView *lastView=[[UIImageView allocinitWithImage:lastImage];

    [scrollView addSubview:preView];

    [scrollView addSubview:curView];

    [scrollView addSubview:lastView];

    [preView release];

    [curView release];

    [lastView release];

    if(scrollDirection==PortaitDirection)   //竖直滚动

    {

        preView.frame=CGRectOffset(preView.frame00);

        curView.frame=CGRectOffset(curView.frame0scrollFrame.size.height);

        lastView.frame=CGRectOffset(lastView.frame02*scrollFrame.size.height);

        [scrollView setContentOffset:CGPointMake(0scrollFrame.size.height)];

    }

    

    if(scrollDirection==LandscapeDirection//水平滚动

    {

        preView.frame=CGRectOffset(preView.frame00);

        curView.frame=CGRectOffset(curView.framescrollFrame.size.width0);

        lastView.frame=CGRectOffset(lastView.framescrollFrame.size.width*20);

        [scrollView setContentOffset:CGPointMake(scrollFrame.size.width0)];

    }

}

 

- (NSArray*) getDisplayImagesWithCurpage:(int)page

{

    int pre=[self validPageValue:curPage-1];

    int last=[self validPageValue:curPage+1];

    if([curImages count]!=0   [curImages removeAllObjects];

    [curImages addObject:[imagesArray objectAtIndex:pre-1]];

    [curImages addObject:[imagesArray objectAtIndex:curPage-1]];

    [curImages addObject:[imagesArray objectAtIndex:last-1]];

    return curImages;

}

 

- (int)validPageValue:(NSInteger)value

{

    if(value==0   value=totalPage   //value1为第一张,value=0为前面一张

    if(value==totalPage+1 value=1;

    return value;

}

 

 

- (void) scrollViewDidScroll:(UIScrollView *)crollView

{

    int x=crollView.contentOffset.x;

    int y=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];

        }

    }

}

 

 

 

- (void)dealloc

{

    [imagesArray release];

    [curImages release];

    [super dealloc];

}

 

@end


0 0
原创粉丝点击