iOS scrollerView 滚动 工具类

来源:互联网 发布:淘宝网烟花烫女装 编辑:程序博客网 时间:2024/06/05 09:54

//

//  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=[superinitWithFrame:rect])) {

        self.userInteractionEnabled=YES;

        

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

        _scrollView.backgroundColor = [UIColorwhiteColor];

       _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 = [[UIImageViewalloc] initWithFrame:CGRectMake(VIEW_WIDTH * i,0, self.width,self.height)];

            [_scrollViewaddSubview:image];

            UITapGestureRecognizer * tapGR = [[[UITapGestureRecognizeralloc] initWithTarget:selfaction:@selector(tap:)]autorelease];

            [imagesetImageWithURL:[NSURLURLWithString:[imgArr objectAtIndex:i]]];

           //打开界面交互

            image.userInteractionEnabled =YES;

           //添加手势

            [imageaddGestureRecognizer:tapGR];

            image.tag =1000 + i;


        }

        [selfaddSubview:_scrollView];

        

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

       _pageControl.numberOfPages = imgArr.count;

        // _pageControl.backgroundColor = [UIColor cyanColor];

        [_pageControladdTarget:selfaction:@selector(changePhoto:)forControlEvents:UIControlEventValueChanged];

        [selfaddSubview:_pageControl];

        

       self.imageArray = [NSMutableArrayarrayWithArray:imgArr];

        //定时器,循环滚动

       _count = 0;

        [NSTimerscheduledTimerWithTimeInterval:2target:selfselector:@selector(loopScroll)userInfo:nilrepeats:YES];

}

returnself;

}


- (void)tap:(UITapGestureRecognizer *)tap

{

    [self.delegateEScrollerViewDidClicked: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改变

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

    }

}


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

- (void)changePhoto:(UIPageControl *)pageControl

{

   int page = pageControl.currentPage;

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

    [_scrollViewsetContentOffset: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



0 0
原创粉丝点击