循环滚动的ScrollView

//.h文件

/*
 *  循环滚动的scrollView
 */

#import <UIKit/UIKit.h>

@protocol DDCycleScrollViewDelegate;

@interface DDCycleScrollView : UIView <UIScrollViewDelegate>
{
    UIScrollView *scrollView_;
}

@property (nonatomic,strong) NSArray *displayViews;
@property (nonatomic,assign) id<DDCycleScrollViewDelegate> delegate;

- (id)initWithFrame:(CGRect)frame;

@end

@protocol DDCycleScrollViewDelegate <NSObject>

- (void)cycleScrollViewDidEndDeceleratingWithCurrentPage:(NSInteger)currentPage
                                         cycleScrollView:(DDCycleScrollView *)scrollView;

@end

//.m文件

#import "DDCycleScrollView.h"

@interface DDCycleScrollView ()
{
    NSInteger currentContentPage_;
    NSInteger totalPage_;
}
@end

@implementation DDCycleScrollView

- (void)dealloc
{
#if !__has_feature(objc_arc)
    [scrollView_ release];
    [_displayViews release];
    [super dealloc];
#endif
}

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self sharedInit];
    }
    return self;
}

- (void)awakeFromNib {
    [super awakeFromNib];
    [self sharedInit];
}

- (void)sharedInit {
    
    currentContentPage_ = 0;
    totalPage_ = 0;
    
    //=======================
    
    scrollView_ = [[UIScrollView alloc] initWithFrame:self.bounds];
    scrollView_.pagingEnabled = YES;
    scrollView_.showsHorizontalScrollIndicator = NO;
    scrollView_.showsVerticalScrollIndicator = NO;
    scrollView_.delegate = self;
    
    CGSize contentSize = CGSizeZero;
    contentSize = CGSizeMake(self.bounds.size.width * 3, self.bounds.size.height);
    
    scrollView_.contentSize = contentSize;
    [self addSubview:scrollView_];
}

#pragma mark - displayViews set
- (void)setDisplayViews:(NSArray *)displayViews {
    if (_displayViews == displayViews) {
        return;
    }
    else {
#if !__has_feature(objc_arc)
        if (_displayViews != nil) {
            [_displayViews release];
        }
        _displayViews = [displayViews retain];
#else
        _displayViews = displayViews;
#endif
        totalPage_ = _displayViews.count;
        
        [self refreshScrollView];
    }
}

#pragma mark - Refresh Content View
- (void)refreshScrollView {
    NSArray *subViews = scrollView_.subviews;
    [subViews makeObjectsPerformSelector:@selector(removeFromSuperview)];
    
    NSMutableArray *currentViews = [[NSMutableArray alloc] init];
    [currentViews setArray:[self curDisplayViewsWithCurPage:currentContentPage_]];
    
    int count = currentViews.count;
    for (int i = 0;i < count;i ++) {
        UIView *aView = [currentViews objectAtIndex:i];
        aView.frame = CGRectMake(self.bounds.size.width * i,
                                 0,
                                 self.bounds.size.width,
                                 self.bounds.size.height);
        [scrollView_ addSubview:aView];
    }
    
#if !__has_feature(objc_arc)
    [currentViews release];
#endif
    
    [scrollView_ setContentOffset:CGPointMake(self.bounds.size.width, 0.0f)];
}

- (NSArray *)curDisplayViewsWithCurPage:(NSInteger)curPage {
    int backPage = [self pageNumber:curPage - 1];
    int forwardPage = [self pageNumber:curPage + 1];
    
    NSMutableArray *currentViews = [[NSMutableArray alloc] init];
    [currentViews addObject:[_displayViews objectAtIndex:backPage]];
    [currentViews addObject:[_displayViews objectAtIndex:curPage]];
    [currentViews addObject:[_displayViews objectAtIndex:forwardPage]];
    
#if !__has_feature(objc_arc)
    return [currentViews autorelease];
#endif
    return currentViews;
    
}

- (int)pageNumber:(NSInteger)num {
    int temp = 0;
    if (num == -1) {
        temp = totalPage_ - 1;
    }
    else if (num == totalPage_) {
        temp = 0;
    }
    else {
        temp = num;
    }
    return temp;
}

#pragma mark - UIScrollViewDelegate
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    
    int x = scrollView.contentOffset.x;
    
    if(x >= (2 * scrollView_.bounds.size.width)) {
        currentContentPage_ = [self pageNumber:currentContentPage_ + 1];
        [self refreshScrollView];
    }
    if(x <= 0) {
        currentContentPage_ = [self pageNumber:currentContentPage_ - 1];
        [self refreshScrollView];
    }
    
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    if ([self.delegate respondsToSelector:@selector(cycleScrollViewDidEndDeceleratingWithCurrentPage:cycleScrollView:)]) {
        [self.delegate cycleScrollViewDidEndDeceleratingWithCurrentPage:currentContentPage_
                                                        cycleScrollView:self];
    }
}

@end

    

    demo下载地址:https://github.com/JinLeiWang/CycleScrollView



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