ScrollView无限滚动

#import <UIKit/UIKit.h>






typedef enum {
   


 CycleDirectionPortait,         
// 垂直滚动
    CycleDirectionLandscape         // 水平滚动
}CycleDirection;






@protocol CycleScrollViewDelegate;






@interface CycleScrollView : UIView <UIScrollViewDelegate> {
    
    
UIScrollView *scrollView;
  


  UIImageView *curImageView;
    
 


   int totalPage;  
 


   int curPage;
 


   CGRect scrollFrame;
 


   CycleDirection scrollDirection;     // scrollView滚动的方向
    


NSArray *imagesArray;               // 存放所有需要滚动的图片 UIImage
   
 NSMutableArray *curImages;          // 存放当前滚动的三张图片
    
    id delegate;
}






@property (nonatomic, assign) id delegate;






- (int)validPageValue:(NSInteger)value;




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




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




- (void)refreshScrollView;


@end






@protocol CycleScrollViewDelegate <NSObject>




@optional




- (void)cycleScrollViewDelegate:(CycleScrollView *)cycleScrollView didSelectImageView:(int)index;




- (void)cycleScrollViewDelegate:(CycleScrollView *)cycleScrollView didScrollImageView:(int)index;






@end




#import "CycleScrollView.h"






@implementation CycleScrollView




@synthesize delegate;






- (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 alloc] init];
     
//总共图片数组
   imagesArray = [[NSArray alloc] initWithArray:pictureArray];
        
       
 scrollView = [[UIScrollView alloc] initWithFrame:frame];
        
scrollView.backgroundColor = [UIColor blackColor];
        
scrollView.showsHorizontalScrollIndicator = NO;
        
scrollView.showsVerticalScrollIndicator = NO;
        
scrollView.pagingEnabled = YES;
     


   scrollView.delegate = self;
    


    [self addSubview:scrollView];
        
        // 在水平方向滚动
        if(scrollDirection == CycleDirectionLandscape) {
            scrollView.contentSize = CGSizeMake(scrollView.frame.size.width * 3,
                                                scrollView.frame.size.height);
        }
        // 在垂直方向滚动 
        if(scrollDirection == CycleDirectionPortait) {
            scrollView.contentSize = CGSizeMake(scrollView.frame.size.width,
                                                scrollView.frame.size.height * 3);
        }
        
      


  [self addSubview:scrollView];
     


   [self refreshScrollView];
    }
    
    return self;
}
//--------------------------------------------------






- (void)refreshScrollView {
    
   
 NSArray *subViews = [scrollView subviews];
   


 if([subViews count] != 0) {
       


 [subViews makeObjectsPerformSelector:@selector(removeFromSuperview)];
    }
    
  


  [self getDisplayImagesWithCurpage:curPage];
    




    for (int i = 0; i < 3; i++) {
       


 UIImageView *imageView = [[UIImageView alloc] initWithFrame:scrollFrame];
      


  imageView.userInteractionEnabled = YES;
       


 imageView.image = [curImages objectAtIndex:i];
        
        
UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self
                                                                                    action:@selector(handleTap:)];
        [imageView addGestureRecognizer:singleTap];
      


  [singleTap release];
        
       


 // 水平滚动
      


  if(scrollDirection == CycleDirectionLandscape) {
            
imageView.frame = CGRectOffset(imageView.frame, scrollFrame.size.width * i, 0);
        }
       


 // 垂直滚动
       


 if(scrollDirection == CycleDirectionPortait) {
            
imageView.frame = CGRectOffset(imageView.frame, 0, scrollFrame.size.height * i);
        }
        
        
        
[scrollView addSubview:imageView];
    }
   






 if (scrollDirection == CycleDirectionLandscape) {
      


  [scrollView setContentOffset:CGPointMake(scrollFrame.size.width, 0)];
    }
   
 if (scrollDirection == CycleDirectionPortait) {
       


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




//--------------------------------------------------




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


// value=1为第一张,value = 0为前面一张
    


if(value == totalPage + 1) 


value = 1;
    
   


 return value;
}










- (void)scrollViewDidScroll:(UIScrollView *)aScrollView {
    
    int 
x = aScrollView.contentOffset.x;
  


  int y = aScrollView.contentOffset.y;
   


 NSLog(@"did  x=%d  y=%d", x, y);
    
    // 水平滚动
   


 if(scrollDirection == CycleDirectionLandscape) {
        // 往下翻一张
        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 == CycleDirectionPortait) {
        // 往下翻一张
        if(y >= 2 * (scrollFrame.size.height)) { 
          


  curPage = [self validPageValue:curPage+1];
           


 [self refreshScrollView];
        }
       


 if(y <= 0) {
            curPage = [self validPageValue:curPage-1];
            [self refreshScrollView];
        }
    }
    
   


 if ([delegate respondsToSelector:@selector(cycleScrollViewDelegate:didScrollImageView:)]) {
      


  [delegate cycleScrollViewDelegate:self didScrollImageView:curPage];
    }
}






- (void)scrollViewDidEndDecelerating:(UIScrollView *)aScrollView {
    
    int x = aScrollView.contentOffset.x;
    


int y = aScrollView.contentOffset.y;
    
    


NSLog(@"--end  x=%d  y=%d", x, y);
    
  


  if (scrollDirection == CycleDirectionLandscape) {
            [scrollView setContentOffset:CGPointMake(scrollFrame.size.width, 0) animated:YES];
    }
   


 if (scrollDirection == CycleDirectionPortait) {
       


 [scrollView setContentOffset:CGPointMake(0, scrollFrame.size.height) animated:YES];
    }
}






- (void)handleTap:(UITapGestureRecognizer *)tap {
    
   


 if ([delegate respondsToSelector:@selector(cycleScrollViewDelegate:didSelectImageView:)]) {
        


[delegate cycleScrollViewDelegate:self didSelectImageView:curPage];
    }
}








- (void)dealloc
{
    [imagesArray release];
    [curImages release];
    
    [super dealloc];
}


@end

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值