码迷,mamicode.com
首页 > 其他好文 > 详细

UIScrollView循环滚动

时间:2014-12-15 15:31:00      阅读:154      评论:0      收藏:0      [点我收藏+]

标签:blog   http   io   ar   color   os   sp   for   on   

UIScrollView循环滚动

.h文件

#import <UIKit/UIKit.h>

@protocol EScrollerViewDelegate <NSObject>
@optional
-(void)scrollImageViewDelegateClickImage:(NSInteger)index withView:(UIImageView*)imgView;
@end

@interface SYScrollerView : UIView<UIScrollViewDelegate> {
	CGRect viewSize;
	UIScrollView *scrollView;
	NSArray *imageArray;
    NSArray *titleArray;
    UIPageControl *pageControl;
    int currentPageIndex;
}
@property(nonatomic,assign)id<EScrollerViewDelegate> delegate;
-(id)initWithFrameRect:(CGRect)rect imageArray:(NSArray *)imgArr;
@end

.m文件

#import "SYScrollerView.h"
#import "UIImageView+WebCache.h"
#import "MyDefine.h"
#define IMAGE_PLACEHOLDER @"activity_background"
#define H_LB 15
@implementation SYScrollerView
@synthesize delegate;

-(id)initWithFrameRect:(CGRect)rect imageArray:(NSArray *)imgArr
{
    
	if ((self=[super initWithFrame:rect])) {
        self.userInteractionEnabled=YES;
        //创建一个arr存放imaArr,这样滑动的可以流畅些
        NSMutableArray *tempArray=[NSMutableArray arrayWithArray:imgArr];
        [tempArray insertObject:[imgArr objectAtIndex:([imgArr count]-1)] atIndex:0];//最后一张
        [tempArray addObject:[imgArr objectAtIndex:0]];//第一张
		imageArray=[NSArray arrayWithArray:tempArray];
		viewSize=rect;
        NSUInteger pageCount=[imageArray count];
        scrollView=[[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, viewSize.size.width, viewSize.size.height)];
        scrollView.pagingEnabled = YES;
        scrollView.contentSize = CGSizeMake(viewSize.size.width * pageCount, viewSize.size.height);
        scrollView.showsHorizontalScrollIndicator = NO;
        scrollView.showsVerticalScrollIndicator = NO;
        scrollView.scrollsToTop = NO;
        scrollView.delegate = self;
        
        for (int i=0; i<pageCount; i++) {
            NSString *imgURL=[imageArray objectAtIndex:i];
            UIImageView *imgView=[[UIImageView alloc] init];
            //区分下是不是网络图
            if ([imgURL hasPrefix:@"http://"]) {
                //网络图片
                [imgView sd_setImageWithURL: [NSURL URLWithString:imgURL] placeholderImage:[UIImage imageNamed:IMAGE_PLACEHOLDER]];
            }else{
                UIImage *img=[UIImage imageNamed:[imageArray objectAtIndex:i]];
                [imgView setImage:img];
            }
            
            [imgView setFrame:CGRectMake(viewSize.size.width*i, 0,viewSize.size.width, viewSize.size.height)];
            imgView.tag=i;
            UITapGestureRecognizer *Tap =[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleSingleFingerEvent:)] ;
            [Tap setNumberOfTapsRequired:1];
            [Tap setNumberOfTouchesRequired:1];
            imgView.userInteractionEnabled=YES;
            [imgView addGestureRecognizer:Tap];
            [scrollView addSubview:imgView];
        }
        [scrollView setContentOffset:CGPointMake(viewSize.size.width, 0)];
        [self addSubview:scrollView];

        pageControl=[[UIPageControl alloc]initWithFrame:CGRectMake(0,viewSize.size.height - H_LB, viewSize.size.width, H_LB)];
        pageControl.currentPage=0;
        pageControl.numberOfPages=(pageCount-2);
        pageControl.pageIndicatorTintColor = [UIColor lightGrayColor];
        pageControl.currentPageIndicatorTintColor = [UIColor whiteColor];
        [self addSubview:pageControl];

	}
	return self;
}

- (void)scrollViewDidScroll:(UIScrollView *)sender
{
    CGFloat pageWidth = scrollView.frame.size.width;
    int page = floor((scrollView.contentOffset.x - pageWidth / 2) / pageWidth) + 1;
    currentPageIndex=page;
       
    pageControl.currentPage=(page-1);

}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)_scrollView
{
    //判断下当前图片的位置
    if (currentPageIndex==0) {
      
        [_scrollView setContentOffset:CGPointMake(([imageArray count]-2)*viewSize.size.width, 0)];
    }
    if (currentPageIndex==([imageArray count]-1)) {
       
        [_scrollView setContentOffset:CGPointMake(viewSize.size.width, 0)];
    }
}
- (void)handleSingleFingerEvent:(UITapGestureRecognizer *)gesture
{
    if ([delegate respondsToSelector:@selector(scrollImageViewDelegateClickImage:withView:)]) {
         [self.delegate scrollImageViewDelegateClickImage:[gesture view].tag withView:(UIImageView*)[gesture view]];
    }
}
@end


UIScrollView循环滚动

标签:blog   http   io   ar   color   os   sp   for   on   

原文地址:http://blog.csdn.net/quanqinyang/article/details/41943127

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!