iOS scrollerView 滚动 工具类

//

//  EScrollerView.h

//  icoiniPad

//

//  Created by Ethan on 12-11-24.

//

//


#import


@protocol EScrollerViewDelegate <NSObject>

@optional

-(void)EScrollerViewDidClicked:(NSUInteger)index;

@end


@interface EScrollerView : UIView<UIScrollViewDelegate>

@property (nonatomic,assign) id<EScrollerViewDelegate> delegate;

@property (nonatomic, strong) UIScrollView * scrollView;

@property (nonatomic, strong) UIPageControl * pageControl;

@property (nonatomic, assign) NSInteger count;

@property (nonatomic , strong) NSMutableArray * imageArray;


-(id)initWithFrameRect:(CGRect)rect ImageArray:(NSArray *)imgArr TitleArray:(NSArray *)titArr;

@end

/

//  EScrollerView.m

//  icoiniPad

//

//  Created by Ethan on 12-11-24.

//

//


#import "EScrollerView.h"

@implementation EScrollerView

@synthesize delegate;


-(id)initWithFrameRect:(CGRect)rect ImageArray:(NSArray *)imgArr TitleArray:(NSArray *)titArr

{

    

if ((self=[super initWithFrame:rect])) {

        self.userInteractionEnabled=YES;

        

        self.scrollView = [[[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, VIEW_WIDTH, self.height)] autorelease];

        _scrollView.backgroundColor = [UIColor whiteColor];

        _scrollView.contentSize = CGSizeMake(imgArr.count * VIEW_WIDTH, self.height);

        _scrollView.pagingEnabled = YES;

        _scrollView.delegate = self;

        for (int i = 0; i < imgArr.count; i++) {

            UIImageView * image = [[UIImageView alloc] initWithFrame:CGRectMake(VIEW_WIDTH * i, 0, self.width, self.height)];

            [_scrollView addSubview:image];

            UITapGestureRecognizer * tapGR = [[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap:)]autorelease];

            [image setImageWithURL:[NSURL URLWithString:[imgArr objectAtIndex:i]]];

            //打开界面交互

            image.userInteractionEnabled = YES;

            //添加手势

            [image addGestureRecognizer:tapGR];

            image.tag = 1000 + i;


        }

        [self addSubview:_scrollView];

        

        self.pageControl = [[[UIPageControl alloc] initWithFrame:CGRectMake(110, 100, 100, 30)] autorelease];

        _pageControl.numberOfPages = imgArr.count;

        // _pageControl.backgroundColor = [UIColor cyanColor];

        [_pageControl addTarget:self action:@selector(changePhoto:) forControlEvents:UIControlEventValueChanged];

        [self addSubview:_pageControl];

        

        self.imageArray = [NSMutableArray arrayWithArray:imgArr];

        //定时器, 循环滚动

        _count = 0;

        [NSTimer scheduledTimerWithTimeInterval:2 target:self selector:@selector(loopScroll) userInfo:nil repeats:YES];

}

return self;

}


- (void)tap:(UITapGestureRecognizer *)tap

{

    [self.delegate EScrollerViewDidClicked:tap.view.tag];

}


//滚动视图的循环滚动

- (void)loopScroll

{

    //NSLog(@"repeat");

    if (self.imageArray.count) {

        int a = _count % self.imageArray.count;

        _pageControl.currentPage = a;

        _count++;

        // NSLog(@"%d", _pageControl.currentPage);

        //[self change];

        //scrollView随着page改变

        [_scrollView setContentOffset:CGPointMake(VIEW_WIDTH * _pageControl.currentPage, 0) animated:YES];        

    }

}


//点击pageControl改变滚动视图图片

- (void)changePhoto:(UIPageControl *)pageControl

{

    int page = pageControl.currentPage;

    NSLog(@"%ld", (long)pageControl.currentPage);

    [_scrollView setContentOffset:CGPointMake(VIEW_WIDTH * page, 0) animated:YES];

    _count = page;

}


#pragma mark - UIScrollViewDelegate

//视图结束减速时改变page

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView

{

    int x = scrollView.contentOffset.x / VIEW_WIDTH;

    _pageControl.currentPage = x;

    _count = x;

}



@end



你可能感兴趣的:(iOS)