常用屬性
contentSize
內容尺寸
contentOffset
偏移量
contentInset
內邊框
delegate
監(jiān)聽滾動
viewForZoomingInScrollView:
縮放
項目:輪播廣告
looper.gif
第一步 新建一個類MOBLoopSrollView
繼承自UIView
,包含:
一個
scrollView
, 顯示輪播圖片
一個pageControl
, 指示當前頁
一個images
, 接收將來需要顯示的圖片
提供一個工廠方法用于創(chuàng)建控件,可自行選擇使用xib
或者代碼創(chuàng)建
/**
創(chuàng)建輪播器
@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;
// 設置代理以監(jiān)聽滾動
self.scrollView.delegate = self;
}
// 使用xib創(chuàng)建調用
- (void)awakeFromNib
{
[super awakeFromNib];
[self setup];
}
// 使用代碼創(chuàng)建時調用
- (instancetype)initWithFrame:(CGRect)frame
{
if (self = [super initWithFrame:frame]) {
[self setup];
}
return self;
}
重寫setter
,給scrollView
添加iamgeView
,并設置pageControl
的數(shù)量
/**
重寫setter,給scroolView 添加 imageView
@param images 傳入的圖片數(shù)組
*/
- (void)setImages:(NSArray *)images
{
_images = images;
// 清空 防止多次對scrollView賦值造成多余數(shù)據(jù)
[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);
}
通過代理方法監(jiān)聽滾動,設置pageControl
的值
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
// 監(jiān)聽滾動的幅度判斷頁面指示器
self.pageControl.currentPage = (int)(scrollView.contentOffset.x / scrollView.frame.size.width + 0.5);
}
創(chuàng)建定時器,在初始化中開啟,并在開始拖拽時停止,停止拖拽后再開啟
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
[self stopTimer];
}
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
[self startTimer];
}
# pragma mark - 定時器
- (void)startTimer
{
// 創(chuàng)建定時器
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 = 頁數(shù) * 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];
}