自动轮播图(可以有点击事件的其中点击事件用 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 的数组
- 自动轮播图(可以有点击事件的其中点击事件用 block 传值)
- 非常简单的图片轮播,自动轮播,按下和移动停止轮播,有点击事件
- Android Studio 实现下拉列表刷新并嵌套轮播图(自动轮播+手动轮播+点击事件)
- MUi-个人整理(图片自动轮播 上、下拉刷新 点击事件)
- js点击轮播或者自动轮播图代码
- viewPager自动轮播+触摸事件
- 很简单的图片自动轮播,点击焦点就显示点击的那个
- 点击左右箭头播放和自动轮播
- UIButton添加block点击事件
- 带边框有点击事件的表格
- 自动轮播图
- 用block改写UIButton点击事件,block改写UIAlerView的代理
- 用block改写UIButton点击事件,block改写UIAlerView的代理
- viewpager的无限轮播,能够响应点击事件
- 图片轮播添加点击事件遇到的问题
- 如何在轮播的图片上添加点击事件
- 原生javascript实现图片自动轮播和点击轮播代码
- 自制人物卡片轮播组件,自动轮播,点击切换
- 删除一个文件夹
- iOS关于应用图标的警告解决方案(图解)Applications using Launch Screen Files and targetting iOS 7.1 and earlier need
- 键盘接收2个文件路径,将一个 拷贝到另一个中
- 文件夹按层级打印
- CSUST 第九届校赛H-最小差值
- 自动轮播图(可以有点击事件的其中点击事件用 block 传值)
- 文件递归改后缀
- 数组冒泡排序
- android去除标题栏
- 编写 三个类 ticket,sealWindow,ticketSealCenter,代表票信息,售票窗口,售票中心, 售票中心分配一定数量的票(多个窗口实现多线程售票)
- 操作系统面试题总结
- struts2标签在jsp不显示问题(不经过action跳转)
- 编写一个程序,这个程序把一个整数数组中的每个元素用逗号连接成一个字符串,例如,根 据内容为[1][2][3]的数组形成内容为"1,2,3"的字符串。
- select模型实现分析