IOS_OC_滾動(dòng)視圖帶點(diǎn)點(diǎn)(使用方便&封裝完善)


效果圖:


效果圖

代碼:
創(chuàng)建一個(gè)UIView類.h中

//圖片數(shù)組&frame
-(instancetype)initWithFrame:(CGRect)frame imageNames:(NSArray *)imageNames;
//時(shí)間間隔&循環(huán)滾動(dòng)
- (void)scrollWithTimeInterval:(NSTimeInterval)timeInterval;


.m

#import "ZDH_ScrollView.h"
#define SCROLL_WIDTH CGRectGetWidth(_scrollView.frame)
#define SCROLL_HEIGHT CGRectGetHeight(_scrollView.frame)

#pragma mark - interface
@interface ZDH_ScrollView()<UIScrollViewDelegate>
{
    UIScrollView *_scrollView;
    UIPageControl *_pageControl;  //-->分頁
    NSInteger imageNum;           //-->圖片數(shù)量
    NSTimer *_timer;              //-->定時(shí)器
    NSInteger _currentPage;       //-->當(dāng)前顯示的頁數(shù)
    NSTimeInterval _timeInterval;//-->時(shí)間間隔
}

@end

@implementation ZDH_ScrollView

#pragma mark - 初始化自定義方法
-(instancetype)initWithFrame:(CGRect)frame imageNames:(NSArray *)imageNames{
    self = [super initWithFrame:frame];
    if (self) {
        [self commonInitWithImageNames:imageNames];
        [self commonInitPageControl];
        
        // 剪裁超出范圍的視圖
        self.clipsToBounds = YES;
        
    }
    return self;
    
}
#pragma mark - 定時(shí)器
//根據(jù)指定的時(shí)間間隔觸發(fā)定時(shí)器實(shí)現(xiàn)自動(dòng)循環(huán)滾動(dòng)
-(void)scrollWithTimeInterval:(NSTimeInterval)timeInterval{
    _timeInterval = timeInterval;
    _timer = [NSTimer scheduledTimerWithTimeInterval:_timeInterval target:self selector:@selector(autoScroll) userInfo:nil repeats:YES];
}

//重新設(shè)定定時(shí)器的激活時(shí)間
-(void)resetTimerFireDate{
    
    NSDate *nDate= [NSDate dateWithTimeIntervalSinceNow:_timeInterval];
    _timer.fireDate = nDate;
    
}

#pragma mark - 系列初始化方法都在這里
//初始化 ImageView 并添加到主視圖上
-(void)commonInitWithImageNames:(NSArray *)imageNames{
    
    //獲取圖片的個(gè)數(shù)
    imageNum = imageNames.count;
    
    //初始化 滾動(dòng)視圖
    [self commonInitScrollView];
    
    //添加 UIimageView
    for (int i=0; i<imageNum +2 ; i++) {
        
        //最左側(cè)的視圖顯示最后一張圖片
        UIImageView *imageV = [[UIImageView alloc] initWithFrame:CGRectMake(i*CGRectGetWidth(self.frame), 0, CGRectGetWidth(self.frame), CGRectGetHeight(self.frame))];
        
        //圖片添加規(guī)律為,最左側(cè)視圖上顯示最后一張圖,最右側(cè)視圖顯示第一張圖
        imageV.image = [UIImage imageNamed:imageNames[i==0?(imageNum - 1):(i == (imageNum + 1)?0:(i-1))]];
        
        [_scrollView addSubview:imageV];
        
    }
    
    //默認(rèn)顯示第二個(gè)視圖
    [self showScrollViewWithPage:1];
    
    
}

// 初始化 UIScrollView 對(duì)象
- (void)commonInitScrollView
{
    _scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, CGRectGetWidth(self.frame), CGRectGetHeight(self.frame))];
    
    [self addSubview:_scrollView];
    
    _scrollView.delegate = self;
    
    // 設(shè)置UIScrollView 翻頁效果
    _scrollView.pagingEnabled = YES;
    
    // 設(shè)置內(nèi)容寬度 contentSize
    // 為讓scrollView循環(huán)滾動(dòng)巡揍,在最左側(cè)添加一個(gè)視圖矩形赏廓,在最右側(cè)添加一個(gè)視圖矩形碑幅,所以imageNum+2
    _scrollView.contentSize = CGSizeMake((imageNum+2)*CGRectGetWidth(self.frame), CGRectGetHeight(self.frame));
    
    // 設(shè)置水平、垂直滑動(dòng)滾動(dòng)條不顯示
    _scrollView.showsHorizontalScrollIndicator = NO;
    _scrollView.showsVerticalScrollIndicator = NO;
}


// 初始化 UIPageControl
- (void)commonInitPageControl
{
    _pageControl = [[UIPageControl alloc]initWithFrame:CGRectMake(0, CGRectGetHeight(self.frame)-50, CGRectGetWidth(self.frame), 20)];
    
    // 設(shè)置 UIPageControl 指示點(diǎn)的顏色
    _pageControl.pageIndicatorTintColor = [UIColor whiteColor];
    // 設(shè)置 UIPageControl 指示點(diǎn)的高亮顏色(當(dāng)前頁的顏色)
    _pageControl.currentPageIndicatorTintColor = [UIColor redColor];
    
    // 設(shè)置總頁數(shù)
    _pageControl.numberOfPages = imageNum;
    
    // 設(shè)置當(dāng)前默認(rèn)顯示的頁數(shù)
    _pageControl.currentPage = 0;
    
    // 添加目標(biāo)方法
    [_pageControl addTarget:self action:@selector(pageControlValueChanged:) forControlEvents:UIControlEventValueChanged];
    
    [self addSubview:_pageControl];
    
}

#pragma mark - 有關(guān)滾動(dòng)的一些設(shè)置在這里

// 根據(jù) UIPageControl 指定頁 顯示 UIScrollView 的視圖
- (void)pageControlValueChanged:(UIPageControl*)pageC
{
    [self showScrollViewWithPage:pageC.currentPage+1];
}

// 自定方法庸论,用于根據(jù)當(dāng)前頁數(shù)顯示指定的 UIScrollView 頁面
- (void)showScrollViewWithPage:(NSUInteger)page
{
    _currentPage = page;
    
    // 預(yù)防超出總頁數(shù)
    if (page >= imageNum+1) {
        return;
    }
    
    // 根據(jù)當(dāng)前給定的 page 獲取相對(duì)應(yīng)的矩形區(qū)域(將要顯示的區(qū)域)
    CGRect visibleRect = CGRectMake(page*SCROLL_WIDTH, 0, SCROLL_WIDTH, SCROLL_HEIGHT);
    
    // 讓 scrollView 滑動(dòng)到指定要顯示的區(qū)域
    [_scrollView scrollRectToVisible:visibleRect animated:YES];
    
}
// 自動(dòng)滾動(dòng)
- (void)autoScroll
{
    
    // 通過設(shè)定 UIView 的動(dòng)畫效果實(shí)現(xiàn) offset移動(dòng)效果
    [UIView animateWithDuration:.5 animations:^{
        [_scrollView setContentOffset:CGPointMake(++_currentPage*SCROLL_WIDTH, _scrollView.contentOffset.y)];
    }];
    
    [self scrollViewDidEndDecelerating:_scrollView];
    
    // 判斷當(dāng)前頁計(jì)算量大于理論總頁數(shù)時(shí),重新賦值
    if (_currentPage > imageNum) {
        _currentPage=1;
    }
    
}

#pragma mark - UIScrollViewDelegate
// 當(dāng) UIScrollView 滾動(dòng)后激發(fā)該方法
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    // 手動(dòng)滾動(dòng)視圖后需要重新為定時(shí)器設(shè)定激活時(shí)間
    [self resetTimerFireDate];
    
    // 根據(jù)內(nèi)容 offset 的 x 值計(jì)算當(dāng)前視圖的頁數(shù)
    NSUInteger page = scrollView.contentOffset.x/CGRectGetWidth(self.frame);
    
    // 如果是最左側(cè)視圖(顯示最后一張圖片),scrollView 滾動(dòng)到倒數(shù)第二個(gè)視圖上(理論中的最后一張圖)
    if (page==0) {
        CGRect newRect = CGRectMake(imageNum*CGRectGetWidth(_scrollView.frame), 0, CGRectGetWidth(self.frame), CGRectGetHeight(self.frame));
        
        // 讓scrollView 滾動(dòng)到指定的區(qū)域
        [_scrollView scrollRectToVisible:newRect animated:NO];
        
        // 設(shè)置 UIPageControl 當(dāng)前頁
        _pageControl.currentPage = imageNum - 1;
        _currentPage = _pageControl.currentPage+1;
        return;
    }
    
    // 如果是最右側(cè)視圖(顯示第一張圖片),scrollView 滾動(dòng)到第二個(gè)視圖上(理論中的第一張圖)
    else if(page==imageNum+1){
        CGRect newRect = CGRectMake(CGRectGetWidth(_scrollView.frame), 0, CGRectGetWidth(self.frame), CGRectGetHeight(self.frame));
        [_scrollView scrollRectToVisible:newRect animated:NO];
        // 設(shè)置 UIPageControl 當(dāng)前頁
        _pageControl.currentPage = 0;
        _currentPage = _pageControl.currentPage+1;
        return;
    }
    
    // 設(shè)置 UIPageControl 當(dāng)前頁
    _pageControl.currentPage = page - 1;
    _currentPage = _pageControl.currentPage+1;
    
}



到這里就封裝好了,只需要在控制器里面調(diào)用就可以了

低能
"
導(dǎo)入頭文件
初始化賦值
加到視圖上
"
還不明白看Demo

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末机蔗,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子甘萧,更是在濱河造成了極大的恐慌,老刑警劉巖梆掸,帶你破解...
    沈念sama閱讀 218,755評(píng)論 6 507
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件扬卷,死亡現(xiàn)場離奇詭異,居然都是意外死亡酸钦,警方通過查閱死者的電腦和手機(jī)怪得,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,305評(píng)論 3 395
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來卑硫,“玉大人徒恋,你說我怎么就攤上這事』斗” “怎么了入挣?”我有些...
    開封第一講書人閱讀 165,138評(píng)論 0 355
  • 文/不壞的土叔 我叫張陵,是天一觀的道長硝拧。 經(jīng)常有香客問我径筏,道長葛假,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,791評(píng)論 1 295
  • 正文 為了忘掉前任滋恬,我火速辦了婚禮聊训,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘恢氯。我一直安慰自己带斑,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,794評(píng)論 6 392
  • 文/花漫 我一把揭開白布勋拟。 她就那樣靜靜地躺著勋磕,像睡著了一般。 火紅的嫁衣襯著肌膚如雪指黎。 梳的紋絲不亂的頭發(fā)上朋凉,一...
    開封第一講書人閱讀 51,631評(píng)論 1 305
  • 那天,我揣著相機(jī)與錄音醋安,去河邊找鬼杂彭。 笑死,一個(gè)胖子當(dāng)著我的面吹牛吓揪,可吹牛的內(nèi)容都是我干的亲怠。 我是一名探鬼主播,決...
    沈念sama閱讀 40,362評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼柠辞,長吁一口氣:“原來是場噩夢啊……” “哼团秽!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起叭首,我...
    開封第一講書人閱讀 39,264評(píng)論 0 276
  • 序言:老撾萬榮一對(duì)情侶失蹤习勤,失蹤者是張志新(化名)和其女友劉穎,沒想到半個(gè)月后焙格,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體图毕,經(jīng)...
    沈念sama閱讀 45,724評(píng)論 1 315
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,900評(píng)論 3 336
  • 正文 我和宋清朗相戀三年眷唉,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了予颤。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 40,040評(píng)論 1 350
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡冬阳,死狀恐怖蛤虐,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情肝陪,我是刑警寧澤驳庭,帶...
    沈念sama閱讀 35,742評(píng)論 5 346
  • 正文 年R本政府宣布,位于F島的核電站氯窍,受9級(jí)特大地震影響嚷掠,放射性物質(zhì)發(fā)生泄漏捏检。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,364評(píng)論 3 330
  • 文/蒙蒙 一不皆、第九天 我趴在偏房一處隱蔽的房頂上張望贯城。 院中可真熱鬧,春花似錦霹娄、人聲如沸能犯。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,944評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽踩晶。三九已至,卻和暖如春枕磁,著一層夾襖步出監(jiān)牢的瞬間渡蜻,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,060評(píng)論 1 270
  • 我被黑心中介騙來泰國打工计济, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留茸苇,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 48,247評(píng)論 3 371
  • 正文 我出身青樓沦寂,卻偏偏與公主長得像学密,于是被迫代替她去往敵國和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子传藏,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,979評(píng)論 2 355

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