UIScrollView 分頁 無限滾動

ZYXInfiniteScrollView.h

#import <UIKit/UIKit.h>

@interface ZYXInfiniteScrollView : UIView  // Infinite 無限
@property (strong, nonatomic) NSArray *images;
@property (weak, nonatomic, readonly) UIPageControl *pageControl;
@property (assign, nonatomic, getter=isScrollDirectionPortrait) BOOL scrollDirectionPortrait;
@end

ZYXInfiniteScrollView.m








#import "ZYXInfiniteScrollView.h"

static int const ImageViewCount = 3;

@interface ZYXInfiniteScrollView() <UIScrollViewDelegate>
@property (weak, nonatomic) UIScrollView *scrollView;
@property (weak, nonatomic) NSTimer *timer;
@end

@implementation ZYXInfiniteScrollView

- (instancetype)initWithFrame:(CGRect)frame
{
    NSLog(@"%@",NSStringFromCGRect(frame));
    if (self = [super initWithFrame:frame]) {
        // 滾動視圖
        UIScrollView *scrollView = [[UIScrollView alloc] init];
        scrollView.showsHorizontalScrollIndicator = NO;
        scrollView.showsVerticalScrollIndicator = NO;
        scrollView.pagingEnabled = YES;
        scrollView.bounces = NO;
        scrollView.delegate = self;
        [self addSubview:scrollView];
        self.scrollView = scrollView;
        
        // 圖片控件
        for (int i = 0; i<ImageViewCount; i++) {
            UIImageView *imageView = [[UIImageView alloc] init];
            [scrollView addSubview:imageView];
        }
        
        // 頁碼視圖
        UIPageControl *pageControl = [[UIPageControl alloc] init];
        [self addSubview:pageControl];
        _pageControl = pageControl;
    }
    return self;
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    
    self.scrollView.frame = self.bounds;
    if (self.isScrollDirectionPortrait) {
        self.scrollView.contentSize = CGSizeMake(0, ImageViewCount * self.bounds.size.height);
    } else {
        self.scrollView.contentSize = CGSizeMake(ImageViewCount * self.bounds.size.width, 0);
    }
    
    for (int i = 0; i<ImageViewCount; i++) {
        UIImageView *imageView = self.scrollView.subviews[i];
        
        if (self.isScrollDirectionPortrait) {
            imageView.frame = CGRectMake(0, i * self.scrollView.frame.size.height, self.scrollView.frame.size.width, self.scrollView.frame.size.height);
        } else {
            imageView.frame = CGRectMake(i * self.scrollView.frame.size.width, 0, self.scrollView.frame.size.width, self.scrollView.frame.size.height);
        }
    }
    
    CGFloat pageW = 80;
    CGFloat pageH = 20;
    CGFloat pageX = self.scrollView.frame.size.width - pageW;
    CGFloat pageY = self.scrollView.frame.size.height - pageH;
    self.pageControl.frame = CGRectMake(pageX, pageY, pageW, pageH);
}

- (void)setImages:(NSArray *)images
{
    _images = images;
    
    // 設(shè)置頁碼
    self.pageControl.numberOfPages = images.count;
    self.pageControl.currentPage = 0;
    
    // 設(shè)置內(nèi)容
    [self updateContent];
    
    // 開始定時器
    [self startTimer];
}

#pragma mark - <UIScrollViewDelegate>
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    // 找出最中間的那個圖片控件
    NSInteger page = 0;
    CGFloat minDistance = MAXFLOAT;
    for (int i = 0; i<self.scrollView.subviews.count; i++) {
        UIImageView *imageView = self.scrollView.subviews[i];
        CGFloat distance = 0;
        if (self.isScrollDirectionPortrait) {
            distance = ABS(imageView.frame.origin.y - scrollView.contentOffset.y);
        } else {
            distance = ABS(imageView.frame.origin.x - scrollView.contentOffset.x);
        }
        if (distance < minDistance) {
            minDistance = distance;
            page = imageView.tag;
        }
    }
    self.pageControl.currentPage = page;
}

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
    [self stopTimer];
}

- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
    [self startTimer];
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    [self updateContent];
}

- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView
{
    [self updateContent];
}

#pragma mark - 內(nèi)容更新
- (void)updateContent
{
    // 設(shè)置圖片
    for (int i = 0; i<self.scrollView.subviews.count; i++) {
        UIImageView *imageView = self.scrollView.subviews[i];
        NSInteger index = self.pageControl.currentPage;
        if (i == 0) {
            index--;
        } else if (i == 2) {
            index++;
        }
        if (index < 0) {
            index = self.pageControl.numberOfPages - 1;
        } else if (index >= self.pageControl.numberOfPages) {
            index = 0;
        }
        imageView.tag = index;
        imageView.image = self.images[index];
    }
    
    // 設(shè)置偏移量在中間
    if (self.isScrollDirectionPortrait) {
        self.scrollView.contentOffset = CGPointMake(0, self.scrollView.frame.size.height);
    } else {
        self.scrollView.contentOffset = CGPointMake(self.scrollView.frame.size.width, 0);
    }
}

#pragma mark - 定時器處理
- (void)startTimer
{
    NSTimer *timer = [NSTimer timerWithTimeInterval:2 target:self selector:@selector(next) userInfo:nil repeats:YES];
    [[NSRunLoop mainRunLoop] addTimer:timer forMode:NSRunLoopCommonModes];
    self.timer = timer;
}

- (void)stopTimer
{
    [self.timer invalidate];
    self.timer = nil;
}

- (void)next
{
    if (self.isScrollDirectionPortrait) {
        [self.scrollView setContentOffset:CGPointMake(0, 2 * self.scrollView.frame.size.height) animated:YES];
    } else {
        [self.scrollView setContentOffset:CGPointMake(2 * self.scrollView.frame.size.width, 0) animated:YES];
    }
}
@end

ZYXInfiniteScrollView 使用

ZYXInfiniteScrollView *scrollView = [[ZYXInfiniteScrollView alloc] init];
  scrollView.scrollDirectionPortrait = NO; // 圖片的滾動方向為水平方向 
  scrollView.frame = CGRectMake(0, 20, self.view.frame.size.width, 170);
  scrollView.images = @[
                        [UIImage imageNamed:@"img_00"],
                        [UIImage imageNamed:@"img_01"],
                        [UIImage imageNamed:@"img_02"],
                        [UIImage imageNamed:@"img_03"],
                        [UIImage imageNamed:@"img_04"]
                        ];
  scrollView.pageControl.currentPageIndicatorTintColor = [UIColor orangeColor];
  scrollView.pageControl.pageIndicatorTintColor = [UIColor grayColor];
  [self.view addSubview:scrollView];
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末韭山,一起剝皮案震驚了整個濱河市颁独,隨后出現(xiàn)的幾起案子噪叙,更是在濱河造成了極大的恐慌,老刑警劉巖繁疤,帶你破解...
    沈念sama閱讀 218,036評論 6 506
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異秕狰,居然都是意外死亡稠腊,警方通過查閱死者的電腦和手機,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,046評論 3 395
  • 文/潘曉璐 我一進店門鸣哀,熙熙樓的掌柜王于貴愁眉苦臉地迎上來架忌,“玉大人,你說我怎么就攤上這事我衬√痉牛” “怎么了饰恕?”我有些...
    開封第一講書人閱讀 164,411評論 0 354
  • 文/不壞的土叔 我叫張陵,是天一觀的道長井仰。 經(jīng)常有香客問我埋嵌,道長,這世上最難降的妖魔是什么俱恶? 我笑而不...
    開封第一講書人閱讀 58,622評論 1 293
  • 正文 為了忘掉前任雹嗦,我火速辦了婚禮,結(jié)果婚禮上合是,老公的妹妹穿的比我還像新娘了罪。我一直安慰自己,他們只是感情好聪全,可當(dāng)我...
    茶點故事閱讀 67,661評論 6 392
  • 文/花漫 我一把揭開白布泊藕。 她就那樣靜靜地躺著,像睡著了一般荔烧。 火紅的嫁衣襯著肌膚如雪吱七。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,521評論 1 304
  • 那天鹤竭,我揣著相機與錄音踊餐,去河邊找鬼。 笑死臀稚,一個胖子當(dāng)著我的面吹牛吝岭,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播吧寺,決...
    沈念sama閱讀 40,288評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼窜管,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了稚机?” 一聲冷哼從身側(cè)響起幕帆,我...
    開封第一講書人閱讀 39,200評論 0 276
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎赖条,沒想到半個月后失乾,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,644評論 1 314
  • 正文 獨居荒郊野嶺守林人離奇死亡纬乍,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,837評論 3 336
  • 正文 我和宋清朗相戀三年碱茁,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片仿贬。...
    茶點故事閱讀 39,953評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡纽竣,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情蜓氨,我是刑警寧澤聋袋,帶...
    沈念sama閱讀 35,673評論 5 346
  • 正文 年R本政府宣布,位于F島的核電站语盈,受9級特大地震影響舱馅,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜刀荒,卻給世界環(huán)境...
    茶點故事閱讀 41,281評論 3 329
  • 文/蒙蒙 一代嗤、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧缠借,春花似錦干毅、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,889評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至绅喉,卻和暖如春渠鸽,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背柴罐。 一陣腳步聲響...
    開封第一講書人閱讀 33,011評論 1 269
  • 我被黑心中介騙來泰國打工徽缚, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人革屠。 一個月前我還...
    沈念sama閱讀 48,119評論 3 370
  • 正文 我出身青樓凿试,卻偏偏與公主長得像,于是被迫代替她去往敵國和親似芝。 傳聞我的和親對象是個殘疾皇子那婉,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 44,901評論 2 355

推薦閱讀更多精彩內(nèi)容