MG--UIScrollView無限滾動

UIScrollView無限滾動的圖解:

UIScrollView無限滾動的思路:就是每一次滑動之后顯示圖片都是最中間的那張圖片,每次滑動都要改變圖片下標(biāo)的索引,第一張和最后一張要做特殊判斷


UIScrollView無限滾動的代碼:

.h文件

//  XMGInfiniteScrollView.h
//  無限滾動-UIScrollView
//  Created by 1 on 14/12/30.
//  Copyright ? 2014年 xiaoming. All rights reserved.

#import <UIKit/UIKit.h>
@interface XMGInfiniteScrollView : UIView
#pragma mark - 給外界提供接口
@property (nonatomic, strong) NSArray *imageNames;  /** 本地圖片名 */
@property (nonatomic, strong) NSArray *imageNames;   /** 遠(yuǎn)程圖片URL */
//@property (nonatomic, strong) NSArray *images;  /** 圖片 */
@end

.m文件

//  XMGInfiniteScrollView.m
//  無限滾動-UIScrollView
//  Created by 1 on 14/12/30.
//  Copyright ? 2014年 xiaoming. All rights reserved.

#import "XMGInfiniteScrollView.h"
@interface XMGInfiniteScrollView()

@property (nonatomic, weak) UIScrollView *scrollView;
@property (nonatomic, weak) UIPageControl *pageControl;
@property (nonatomic, weak) NSTimer *timer;
@end
@implementation XMGInfiniteScrollView
static NSUInteger const XMGImageViewCount = 3;
#pragma mark - 初始化方法
- (instancetype)initWithFrame:(CGRect)frame
{
  if (self = [super initWithFrame:frame]) { 
  // 創(chuàng)建并添加scrollView對象
  UIScrollView *scrollView = [[UIScrollView alloc] init];
  scrollView.backgroundColor = [UIColor redColor];
  scrollView.showsVerticalScrollIndicator = NO; // 隱藏垂直滾動條
  scrollView.showsHorizontalScrollIndicator = NO;  // 隱藏水平滾動條
  scrollView.pagingEnabled = YES;  // 分頁
  scrollView.delegate = self; // 代理
  [self addSubview:scrollView];
  self.scrollView = scrollView;
  self.scrollView.bounces = NO;
  // 創(chuàng)建3個(gè)UIImageView對象
  for (NSUInteger i = 0; i < XMGImageViewCount; i++) {
    UIImageView *imageView = [[UIImageView alloc] init];
    [scrollView addSubview:imageView];
  }
    // 創(chuàng)建pageControl對象
    UIPageControl *pageControl = [[UIPageControl alloc] init];
    pageControl.backgroundColor = [UIColor blueColor];
    [self addSubview:pageControl];
    self.pageControl = pageControl;
    // 開啟定時(shí)器
    [self startTimer];
  }
   return self;
}
- (void)layoutSubviews
{
  [super layoutSubviews];
  // scrollView
  self.scrollView.frame = self.bounds;
  // 設(shè)置pageControl的frame
  CGFloat pageControlW = 100;
  CGFloat pageControlH = 30;
  CGFloat pageControlX = self.bounds.size.width - pageControlW;
  CGFloat pageControlY = self.bounds.size.height - pageControlH;
  self.pageControl.frame = CGRectMake(pageControlX,  pageControlY, pageControlW, pageControlH);
 // 設(shè)置3個(gè)UIImageView的frame 
 CGFloat imageW = self.scrollView.frame.size.width;
 CGFloat imageH = self.scrollView.frame.size.height;
 for (NSUInteger i = 0; i < XMGImageViewCount; i++) {
   UIImageView *imageView = self.scrollView.subviews[i];
   CGFloat imageX = i * imageW; 
   CGFloat imageY = 0;
   imageView.frame = CGRectMake(imageX, imageY, imageW, imageH);
   imageView.backgroundColor = [UIColor   colorWithRed:arc4random_uniform(255)/255.0 green:arc4random_uniform(255)/255.0   blue:arc4random_uniform(255)/255.0 alpha:1.0];
}
self.scrollView.contentSize = CGSizeMake(XMGImageViewCount * imageW, 0);
// 更新UIImageView內(nèi)容
   [self updateContent];
}
#pragma mark - 屬性setter
- (void)setImageNames:(NSArray *)imageNames
{
  _imageNames = imageNames;
  // 設(shè)置總頁碼
  self.pageControl.numberOfPages = imageNames.count;
}```

#核心代碼開頭

pragma mark - 其他方法

  • (void)updateContent
    {
    // 1.從左到右重新設(shè)置每一個(gè)UIImageView的圖片
    for (NSUInteger i = 0; i < XMGImageViewCount; i++) {
    UIImageView *imageView = self.scrollView.subviews[i];
    // 求出i位置imageView對應(yīng)的圖片索引
    NSInteger imageIndex = 0; // 這里的imageIndex不能用NSUInteger
    if (i == 0) { // 當(dāng)前頁碼 - 1
    imageIndex = self.pageControl.currentPage - 1;
    } else if (i == 2) { // 當(dāng)前頁碼 + 1
    imageIndex = self.pageControl.currentPage + 1;
    } else { // // 當(dāng)前頁碼
    imageIndex = self.pageControl.currentPage;
    }
    // 判斷越界
    if (imageIndex == -1) { // 最后一張圖片
    imageIndex = self.imageNames.count - 1;
    } else if (imageIndex == self.imageNames.count) { // 最前面那張
    imageIndex = 0;
    }
    imageView.image = [UIImage imageNamed:self.imageNames[imageIndex]];
    // 綁定圖片索引到UIImageView的tag
    imageView.tag = imageIndex;
    }
    // 2.重置UIScrollView的contentOffset.width == 1倍寬度
    self.scrollView.contentOffset = CGPointMake(self.scrollView.frame.size.width, 0);
    }
    核心代碼結(jié)束

pragma mark -<UIScrollViewDelegate>

/****
*** 只要scrollView滾動,就會調(diào)用這個(gè)方法

*/

  • (void)scrollViewDidScroll:(UIScrollView *)scrollView
    {
    // imageView的x 和 scrollView偏移量x 的最小差值
    CGFloat minDelta = MAXFLOAT;
    // 找出顯示在最中間的圖片索引
    NSInteger centerImageIndex = 0;
    for (NSUInteger i = 0; i < XMGImageViewCount; i++) {
    UIImageView *imageView = self.scrollView.subviews[i];
    // ABS : 取得絕對值
    CGFloat delta = ABS(imageView.frame.origin.x - self.scrollView.contentOffset.x);
    if (delta < minDelta) {
    minDelta = delta;
    centerImageIndex = imageView.tag;
    }
    }
    // 設(shè)置頁碼
    self.pageControl.currentPage = centerImageIndex;
    }

/**

  • 滾動完畢后調(diào)用(前提:手松開后繼續(xù)滾動)
    */
  • (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
    {
    [self updateContent];
    }

/**

  • 當(dāng)用戶即將開始拖拽的時(shí)候調(diào)用
    */
  • (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
    {
    [self stopTimer];
    }

/**

  • 當(dāng)用戶即將結(jié)束拖拽的時(shí)候調(diào)用
    */
  • (void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset
    {
    [self startTimer];
    }

pragma mark - 定時(shí)器處理

// 開啟定時(shí)器

  • (void)startTimer
    {
    self.timer = [NSTimer scheduledTimerWithTimeInterval:2.0 target:self selector:@selector(nextPage) userInfo:nil repeats:YES];
    [[NSRunLoop mainRunLoop] addTimer:self.timer forMode:NSRunLoopCommonModes];
    }
    // 停止定時(shí)器
  • (void)stopTimer
    {
    [self.timer invalidate];
    self.timer = nil;
    }
    // 顯示下一頁
  • (void)nextPage
    {
    [UIView animateWithDuration:0.25 animations:^{
    self.scrollView.contentOffset = CGPointMake(2 * self.scrollView.frame.size.width, 0);
    } completion:^(BOOL finished) {
    [self updateContent];
    }];
    }
    @end```

運(yùn)行效果圖:

UIScrollView的無限循環(huán)滾動(封裝)UIScrollView的無限循環(huán)滾動(封裝)來MG明明就是你新浪博客

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容