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

本文详细介绍了如何自定义实现轮播图组件,包括点击图片触发事件、自动循环滚动、页面指示器和定时切换等功能。文章还讨论了在实现过程中使用的关键技术,如block、UIWebView和sdwebimage等,并提供了实例代码供读者参考。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

自动轮播图自己封装的文件其中包括轮播图的点击方法使用了 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 的数组

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值