自动轮播图(可以有点击事件的其中点击事件用 block 传值)

来源:互联网 发布:深圳分数据海关 编辑:程序博客网 时间:2024/05/24 04:31

自动轮播图自己封装的文件其中包括轮播图的点击方法使用了 block
在. h 文件中

import

import “AutoScrollView.h”

import “UIImageView+WebCache.h”

@interface AutoScrollView ()
@property (nonatomic, strong) UIScrollView *scrollView;
@property (nonatomic, strong) UIPageControl *pageControl;
@property (nonatomic, strong) NSTimer *timer;
@property (nonatomic, assign) NSInteger currentIndex;
@end
@implementation AutoScrollView

pragma mark - 初始化

  • (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {

    [self pageControllBuild];[self timerAction];

    }
    return self;
    }

pragma mark - 创建 scrollView

  • (void)scrollViewBuild {
    UIScrollView *sc = [[UIScrollView alloc] initWithFrame:self.bounds];
    [self addSubview:sc];

    self.scrollView = sc;
    self.scrollView.contentSize = CGSizeMake(self.scrollView.frame.size.width * (self.picMutableArray.count + 2), 0);
    // 循环附图片
    for (int i = 0; i < self.picMutableArray.count + 2; i++) {
    UIImageView imageView = [[UIImageView alloc] initWithFrame:CGRectMake(self.scrollView.frame.size.width i, 0, self.scrollView.frame.size.width, self.scrollView.frame.size.height)];

    imageView.userInteractionEnabled = YES;[imageView addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(imageViewDidTap:)]];if (i == 0) {    [imageView sd_setImageWithURL:[NSURL URLWithString:self.picMutableArray[self.picMutableArray.count - 1]]];} else if (i == self.picMutableArray.count + 1) {    [imageView sd_setImageWithURL:[NSURL URLWithString:self.picMutableArray[0]]];} else {    [imageView sd_setImageWithURL:[NSURL URLWithString:self.picMutableArray[i - 1]]];}[self.scrollView addSubview:imageView];

    }
    self.scrollView.delegate = self;
    self.scrollView.bounces = NO;
    self.scrollView.pagingEnabled = YES;
    self.scrollView.showsHorizontalScrollIndicator = NO;
    self.scrollView.contentOffset = CGPointMake(self.scrollView.frame.size.width, 0);
    }
    /*点击图片将会被 block/

  • (void)imageViewDidTap:(UITapGestureRecognizer *)tap {

    self.block(self.scrollView.contentOffset.x / self.scrollView.frame.size.width);

}
- (void)scrollViewAction {
if (self.scrollView.contentOffset.x / self.scrollView.frame.size.width == (self.picMutableArray.count)) {
self.scrollView.contentOffset = CGPointMake(0, 0);
} else {
self.scrollView.contentOffset = CGPointMake(self.scrollView.contentOffset.x + self.scrollView.frame.size.width, 0);
}

if (self.scrollView.contentOffset.x / self.scrollView.frame.size.width == self.picMutableArray.count) {    self.pageControl.currentPage = (self.picMutableArray.count - 1);    _currentIndex = self.pageControl.currentPage;} else {    self.pageControl.currentPage = self.scrollView.contentOffset.x / self.scrollView.frame.size.width - 1;    _currentIndex = self.pageControl.currentPage;}

}

pragma mark - 创建 pageControl

  • (void)pageControllBuild {
    UIPageControl pc = [[UIPageControl alloc] initWithFrame:CGRectMake(self.scrollView.frame.size.width 2 / 5, self.scrollView.frame.size.height - 20, self.scrollView.frame.size.width / 5, 10)];
    [self addSubview:pc];
    _pageControl = pc;
    self.pageControl.numberOfPages = self.picMutableArray.count;
    [self.pageControl addTarget:self action:@selector(pagecontrolAction:) forControlEvents:UIControlEventValueChanged];
    }

pragma mark - NSTimer

  • (void)timerAction {
    self.timer = [NSTimer scheduledTimerWithTimeInterval:3 target:self selector:@selector(scrollViewAction) userInfo:nil repeats:YES];
    }

  • (void)removeTimer {
    [self.timer invalidate];
    }

  • (void)dealloc {
    [self removeTimer];
    }

pragma mark - UIScrollViewDelegate

  • (UIView )viewForZoomingInScrollView:(UIScrollView )scrollView {
    return [scrollView.subviews firstObject];
    }
  • (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    if (scrollView.contentOffset.x / scrollView.frame.size.width == (self.picMutableArray.count + 1)) {
    [scrollView setContentOffset:CGPointMake(self.scrollView.frame.size.width, 0) animated:NO];
    _pageControl.currentPage = 0;

    } else if (scrollView.contentOffset.x / scrollView.frame.size.width == 0) {
    [scrollView setContentOffset:CGPointMake(self.picMutableArray.count * self.scrollView.frame.size.width, 0) animated:NO];
    _pageControl.currentPage = self.picMutableArray.count;
    } else {
    _pageControl.currentPage = scrollView.contentOffset.x / scrollView.frame.size.width - 1;
    }
    }

  • (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
    [self removeTimer];
    }
  • (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate {
    [self timerAction];
    }

pragma mark - pageControl 事件

  • (void)pagecontrolAction:(UIPageControl *)pageControl {

}
其中调用了 sdwebimage的第三方所以穿的数组是图片 url 的数组

0 0
原创粉丝点击