常用属性
contentSize
内容尺寸
contentOffset
偏移量
contentInset
内边框
delegate
监听滚动
viewForZoomingInScrollView:
缩放
项目:轮播广告
looper.gif
第一步 新建一个类MOBLoopSrollView
继承自UIView
,包含:
一个
scrollView
, 显示轮播图片
一个pageControl
, 指示当前页
一个images
, 接收将来需要显示的图片
提供一个工厂方法用于创建控件,可自行选择使用xib
或者代码创建
/**
创建轮播器
@return 轮播器
*/
+ (MOBLoopScrollView *)loopScrollView
{
return [[[NSBundle mainBundle] loadNibNamed:NSStringFromClass(self) owner:nil options:nil] lastObject];
}
一些初始化工作
# pragma mark - 一些初始化操作
- (void)setup
{
// 开启翻页
self.scrollView.pagingEnabled = YES;
// 隐藏水平滚动条
self.scrollView.showsHorizontalScrollIndicator = NO;
// 设置代理以监听滚动
self.scrollView.delegate = self;
}
// 使用xib创建调用
- (void)awakeFromNib
{
[super awakeFromNib];
[self setup];
}
// 使用代码创建时调用
- (instancetype)initWithFrame:(CGRect)frame
{
if (self = [super initWithFrame:frame]) {
[self setup];
}
return self;
}
重写setter
,给scrollView
添加iamgeView
,并设置pageControl
的数量
/**
重写setter,给scroolView 添加 imageView
@param images 传入的图片数组
*/
- (void)setImages:(NSArray *)images
{
_images = images;
// 清空 防止多次对scrollView赋值造成多余数据
[self.scrollView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
NSInteger count = images.count;
for (int i = 0; i < count; i++) {
UIImageView *imageView = [[UIImageView alloc] init];
imageView.image = images[i];
[self.scrollView addSubview:imageView];
}
self.pageControl.numberOfPages = count;
}
重写layoutSubviews
,设置所有控件的frame
/**
设置frame
*/
- (void)layoutSubviews
{
[super layoutSubviews];
// 设置scrollView
self.scrollView.frame = self.bounds;
CGFloat scrollW = self.scrollView.frame.size.width;
CGFloat scrollH = self.scrollView.frame.size.height;
// 设置contentSize
self.scrollView.contentSize = CGSizeMake(self.images.count * scrollW, 0);
for (int i = 0; i < self.images.count; i++) {
UIImageView *imageView = self.scrollView.subviews[i];
imageView.frame = CGRectMake(scrollW * i, 0, scrollW, scrollH);
}
// 设置pageControl
CGFloat pageW = 100;
CGFloat pageH = 20;
CGFloat pageX = scrollW - pageW;
CGFloat pageY = scrollH - pageH;
self.pageControl.frame = CGRectMake(pageX, pageY, pageW, pageH);
}
通过代理方法监听滚动,设置pageControl
的值
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
// 监听滚动的幅度判断页面指示器
self.pageControl.currentPage = (int)(scrollView.contentOffset.x / scrollView.frame.size.width + 0.5);
}
创建定时器,在初始化中开启,并在开始拖拽时停止,停止拖拽后再开启
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
[self stopTimer];
}
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
[self startTimer];
}
# pragma mark - 定时器
- (void)startTimer
{
// 创建定时器
self.timer = [NSTimer scheduledTimerWithTimeInterval:3.0 target:self selector:@selector(nextPage) userInfo:nil repeats:YES];
// // 定时器开始
// [self.timer fire];
// 防止其他控件干扰
[[NSRunLoop mainRunLoop] addTimer:self.timer forMode:NSRunLoopCommonModes];
}
- (void)stopTimer
{
// 停止定时器
[self.timer invalidate];
self.timer = nil;
}
/**
offset.x = 页数 * scrollView的宽度
注意到最后一张需要回到第一张
*/
- (void)nextPage
{
NSUInteger index = self.pageControl.currentPage + 1;
// 如果是最后一页则回到第一页
if (index == self.pageControl.numberOfPages) {
index = 0;
}
// 对象的结构体属性的值不可以直接修改,需要先赋值给另外一个结构体修改后重新赋值回来
CGPoint offset = self.scrollView.contentOffset;
offset.x = self.scrollView.frame.size.width * index;
[self.scrollView setContentOffset:offset animated: YES];
}