iOS scrollerView 滚动 工具类

//

//  EScrollerView.h

//  icoiniPad

//

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

//

//


#import <UIKit/UIKit.h>


@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



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值