UIScrollView循环滚动

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


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值