封裝廣告輪播圖

效果圖:
輪播圖.jpg

由于錄制的視頻是mov格式冲甘,真不知道該怎么轉(zhuǎn)換成GIF的碗硬,所以吝秕,就隨便貼一張靜態(tài)圖來吧~

開始做之前百度了一下洲劣,有好幾種方法來做這個(gè)备蚓,但是我這個(gè)要求是要用三個(gè)ImageView套在ScrollView里面實(shí)現(xiàn)復(fù)用,所以就用這種辦法來做的

由于要有封裝性闪檬,所以新建一個(gè)Carousel的類星著,繼承與UIView购笆。
Carousel.h

//
//  Carousel.h
//  廣告輪播
//
//  Created by  劉雅蘭 on 2017/11/28.
//  Copyright ? 2017年  劉雅蘭. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface Carousel : UIView
@property (strong,nonatomic) NSArray *images;//圖片
@property (assign,nonatomic) NSTimeInterval timeInterval;//時(shí)間

@end

Carousel.m

//
//  Carousel.m
//  廣告輪播
//
//  Created by  劉雅蘭 on 2017/11/28.
//  Copyright ? 2017年  劉雅蘭. All rights reserved.
//

#import "Carousel.h"

#define SCREEN_WIDTH    ([[UIScreen mainScreen] bounds].size.width)



@interface Carousel()<UIScrollViewDelegate>

@property (strong,nonatomic) UIScrollView *scrollView;
@property (strong,nonatomic) UIPageControl *pageControl;

@property (assign,nonatomic) NSTimer *timer;//定時(shí)器
@property (assign,nonatomic) NSInteger currentPage;

@property (strong,nonatomic) UIImageView *leftImageView;
@property (strong,nonatomic) UIImageView *middleImageView;
@property (strong,nonatomic) UIImageView *rightImageView;

@property (strong ,nonatomic) NSMutableArray *arrayView;





@end


@implementation Carousel

-(instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        _currentPage = 0;
    
        [self createUI];
    }
    return self;
}
-(void)createUI{
    _scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 220)];
    _scrollView.showsHorizontalScrollIndicator = NO;
    _scrollView.showsVerticalScrollIndicator = NO;
    _scrollView.pagingEnabled = YES;
    _scrollView.scrollEnabled = YES;
    _scrollView.bounces = 0;
    [self addSubview:_scrollView];
    
//    _leftImageView = [[UIImageView alloc]initWithFrame:CGRectMake(SCREEN_WIDTH * 0, 0, SCREEN_WIDTH, 220)];
//    _leftImageView.userInteractionEnabled = YES;
//    [_scrollView addSubview:_leftImageView];
//
//    _middleImageView = [[UIImageView alloc]initWithFrame:CGRectMake(SCREEN_WIDTH * 1, 0, SCREEN_WIDTH, 220)];
//    _middleImageView.userInteractionEnabled = YES;
//    [_scrollView addSubview:_middleImageView];
//
//    _rightImageView = [[UIImageView alloc]initWithFrame:CGRectMake(SCREEN_WIDTH * 2, 0, SCREEN_WIDTH, 220)];
//    _rightImageView.userInteractionEnabled = YES;
//    [_scrollView addSubview:_rightImageView];
    
    NSInteger imgViewCounts = 3;
    _scrollView.contentSize = CGSizeMake(imgViewCounts *SCREEN_WIDTH, 220);
    for (NSInteger i = 0; i < imgViewCounts; i++) {
        UIImageView *imgView = [UIImageView new];
        imgView.frame = CGRectMake(SCREEN_WIDTH *i, 0, SCREEN_WIDTH, 220);
        [_scrollView addSubview:imgView];
        if (i == 0){
            _leftImageView = imgView;
        }
        if (i == 1) {
            _middleImageView = imgView;
        }
        if (i == 2) {
            _rightImageView = imgView;
        }
    }
    _scrollView.contentOffset = CGPointMake(SCREEN_WIDTH, 0);

    
    _pageControl = [[UIPageControl alloc]initWithFrame:CGRectMake(0, 200, SCREEN_WIDTH, 20)];

    _pageControl.currentPageIndicatorTintColor = [UIColor blackColor];
    _pageControl.pageIndicatorTintColor = [UIColor whiteColor];

    [self addSubview:_pageControl];
    
    // 顯示中間的視圖
//    _scrollView.contentOffset = CGPointMake(SCREEN_WIDTH, 0);
    _scrollView.delegate = self;
    
    // 開始是第一張圖片
    _currentPage = 0;
//    _leftImageView.backgroundColor = [UIColor redColor];
//    _middleImageView.backgroundColor = [UIColor yellowColor];
//    _rightImageView.backgroundColor = [UIColor blueColor];


    
//    _leftImageView.image = [UIImage imageNamed:_images[_images.count - 1]];
//    _middleImageView.image = [UIImage imageNamed:_images[_currentPage]];
//    _rightImageView.image = [UIImage imageNamed:_images[_currentPage + 1]];
    
    
    _arrayView = [NSMutableArray arrayWithCapacity:3];
    [_arrayView addObject:_leftImageView];
    [_arrayView addObject:_middleImageView];
    [_arrayView addObject:_rightImageView];
    
}

//滾動(dòng)
-(void)scrollViewDidScroll:(UIScrollView *)scrollView {
//    [self stopTimer];
    [self checkOffset];
//    [self stopTimer];

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

-(void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{
//    [self autoScroll];
    
}
//  每次向左滑動(dòng)到rightImageView粗悯,且滑動(dòng)結(jié)束后把middleImageView的圖片更新為rightImageView一樣的圖片,rightImageView更新為nextIMG,且scrollView.contentOffset強(qiáng)制調(diào)整為middleImageView.frame.x,最后更新leftImageView

//  每次向右滑動(dòng)到leftImageView同欠,且滑動(dòng)結(jié)束后把middleImageView的圖片更新為leftImageView一樣的圖片,leftImageView更新為lastIMG样傍,且scrollView.contentOffset強(qiáng)制調(diào)整為middleImageView.frame.x,最后更新rightImageView

/*
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    CGFloat currentX = scrollView.contentOffset.x;
    NSLog(@"scrollViewDidEndDecelerating%.2f",currentX);
    if (currentX == 0) {
        id obj = _arrayView.lastObject;
        [_arrayView removeLastObject];
        [_arrayView insertObject:obj atIndex:0];
        int i = 0;
        for (UIImageView *imgView in _arrayView) {

            imgView.frame = CGRectMake(i * SCREEN_WIDTH, 0, SCREEN_WIDTH, 220);
            i++;
        }
        [_scrollView setContentOffset:CGPointMake(SCREEN_WIDTH, 0)];
        
        
        
    } else if (currentX == 2 * SCREEN_WIDTH) {
        id obj = _arrayView.firstObject;
        [_arrayView removeObjectAtIndex:0];
        [_arrayView insertObject:obj atIndex:_arrayView.count];

        int i = 0;
        for (UIImageView *imgView in _arrayView) {
            imgView.frame = CGRectMake(i * SCREEN_WIDTH, 0, SCREEN_WIDTH, 220);
            i++;
        }
        [_scrollView setContentOffset:CGPointMake(SCREEN_WIDTH, 0)];

    }



}
*/
-(void)checkOffset {
    
    // 左
    if (_scrollView.contentOffset.x <= 0) {
        _middleImageView.image = _leftImageView.image;
        _scrollView.contentOffset = CGPointMake(SCREEN_WIDTH, 0);
        _currentPage--;
        [self refreshIMG];
    }
    // 右
    else if (_scrollView.contentOffset.x >= SCREEN_WIDTH *2) {
        _middleImageView.image = _rightImageView.image;
        _scrollView.contentOffset = CGPointMake(SCREEN_WIDTH, 0);
        _currentPage++;
        [self refreshIMG];
    }
}
- (void)refreshIMG {
  
    if (_currentPage < 0) {
        _currentPage = _images.count-1;
    }
    else if (_currentPage >= _images.count) {
        _currentPage = 0;
    }
    
    
    //  middleImageView的img為01.jpg的時(shí)候,leftImageView應(yīng)顯示06.img
    NSInteger leftIndex;
    if (_currentPage == 0) {
        
        leftIndex=_images.count-1;
    
    }else{
        
        leftIndex=_currentPage-1;
    }
    
    NSInteger rightIndex;
    if (_currentPage+1 == _images.count) {
        rightIndex = 0;
        
    }else{
        rightIndex = _currentPage +1;
    }
    
    // 左
    _leftImageView.image = [UIImage imageNamed:_images[leftIndex]];
    // 中
    _middleImageView.image = [UIImage imageNamed:_images[_currentPage]];
    // 右
    _rightImageView.image = [UIImage imageNamed:_images[rightIndex]];
    // 小點(diǎn)
    _pageControl.currentPage = _currentPage;
}




#pragma mark setter
- (void)setImages:(NSArray *)images {
    
        _images = images;
        _pageControl.numberOfPages = images.count;
        [self refreshIMG];
//        [self beginTimer];
//        [self autoScroll];

}
- (void)setTimeInterval:(NSTimeInterval)timeInterval {
    
    _timeInterval = timeInterval;
    //    return;
    [self autoScroll];
}

- (void)autoScroll {
    if (!_timer) {
       _timer= [NSTimer scheduledTimerWithTimeInterval:_timeInterval target:self selector:@selector(sad) userInfo:nil repeats:YES];
        [[NSRunLoop mainRunLoop]addTimer:_timer forMode:NSDefaultRunLoopMode];
    }
}
- (void)sad {
    NSLog(@"haha");
    CGPoint offset = CGPointMake(SCREEN_WIDTH *2, 0);
    [_scrollView setContentOffset:offset animated:YES];
}


- (void)stopTimer {
    
    
    [_timer invalidate];
    _timer = nil;
    
    
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end

可以看到我注釋了一大堆铺遂,糾結(jié)好這個(gè)不容易吧栏纭!=笕瘛撤逢!還求助了各種大神!

接下來,在ViewController里面使用了

ViewController.m

//
//  ViewController.m
//  廣告輪播
//
//  Created by  劉雅蘭 on 2017/11/28.
//  Copyright ? 2017年  劉雅蘭. All rights reserved.
//

#import "ViewController.h"
#import "Carousel.h"

@interface ViewController ()<UIScrollViewDelegate>



@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    UIView *viewUp = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 64)];
    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(146, 44, 100, 20)];
    label.text = @"BLACKPINK";
    [viewUp addSubview:label];
    [self.view addSubview:viewUp];
    
  
    NSArray *images = @[@"01.jpg",@"02.jpg",@"03.jpg",@"04.jpg",@"05.jpg",@"06.jpg"];
    
    Carousel *carousel = [[Carousel alloc] initWithFrame:CGRectMake(0, 100, self.view.frame.size.width, 220)];
    carousel.images = images;
    carousel.timeInterval = 2.0;
    [self.view addSubview:carousel];

    // Do any additional setup after loading the view from its nib.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end

ok吶蚊荣,就醬紫初狰,折磨了我三天!
demo鏈接:https://gitee.com/LanXiaoMei/FengZhuangGuangGaoLunBoTu/tree/master

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末互例,一起剝皮案震驚了整個(gè)濱河市奢入,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌媳叨,老刑警劉巖腥光,帶你破解...
    沈念sama閱讀 218,858評(píng)論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場(chǎng)離奇詭異糊秆,居然都是意外死亡武福,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,372評(píng)論 3 395
  • 文/潘曉璐 我一進(jìn)店門痘番,熙熙樓的掌柜王于貴愁眉苦臉地迎上來艘儒,“玉大人,你說我怎么就攤上這事夫偶〗缯觯” “怎么了?”我有些...
    開封第一講書人閱讀 165,282評(píng)論 0 356
  • 文/不壞的土叔 我叫張陵兵拢,是天一觀的道長翻斟。 經(jīng)常有香客問我,道長说铃,這世上最難降的妖魔是什么访惜? 我笑而不...
    開封第一講書人閱讀 58,842評(píng)論 1 295
  • 正文 為了忘掉前任,我火速辦了婚禮腻扇,結(jié)果婚禮上债热,老公的妹妹穿的比我還像新娘。我一直安慰自己幼苛,他們只是感情好窒篱,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,857評(píng)論 6 392
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著舶沿,像睡著了一般墙杯。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上括荡,一...
    開封第一講書人閱讀 51,679評(píng)論 1 305
  • 那天高镐,我揣著相機(jī)與錄音,去河邊找鬼畸冲。 笑死嫉髓,一個(gè)胖子當(dāng)著我的面吹牛观腊,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播算行,決...
    沈念sama閱讀 40,406評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼恕沫,長吁一口氣:“原來是場(chǎng)噩夢(mèng)啊……” “哼!你這毒婦竟也來了纱意?” 一聲冷哼從身側(cè)響起婶溯,我...
    開封第一講書人閱讀 39,311評(píng)論 0 276
  • 序言:老撾萬榮一對(duì)情侶失蹤,失蹤者是張志新(化名)和其女友劉穎偷霉,沒想到半個(gè)月后迄委,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,767評(píng)論 1 315
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡类少,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,945評(píng)論 3 336
  • 正文 我和宋清朗相戀三年叙身,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片硫狞。...
    茶點(diǎn)故事閱讀 40,090評(píng)論 1 350
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡信轿,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出残吩,到底是詐尸還是另有隱情财忽,我是刑警寧澤,帶...
    沈念sama閱讀 35,785評(píng)論 5 346
  • 正文 年R本政府宣布泣侮,位于F島的核電站即彪,受9級(jí)特大地震影響,放射性物質(zhì)發(fā)生泄漏活尊。R本人自食惡果不足惜隶校,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,420評(píng)論 3 331
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望蛹锰。 院中可真熱鬧深胳,春花似錦、人聲如沸铜犬。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,988評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽翎苫。三九已至权埠,卻和暖如春榨了,著一層夾襖步出監(jiān)牢的瞬間煎谍,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,101評(píng)論 1 271
  • 我被黑心中介騙來泰國打工龙屉, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留呐粘,地道東北人满俗。 一個(gè)月前我還...
    沈念sama閱讀 48,298評(píng)論 3 372
  • 正文 我出身青樓,卻偏偏與公主長得像作岖,于是被迫代替她去往敵國和親唆垃。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,033評(píng)論 2 355

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

  • Gif 圖效果如圖所示: 我所用的 Gif 圖工具是 "licecap"大家可以上百度查詢,有下載地址 知道你們懶...
    小苗曉雪閱讀 424評(píng)論 0 2
  • 發(fā)現(xiàn) 關(guān)注 消息 iOS 第三方庫痘儡、插件辕万、知名博客總結(jié) 作者大灰狼的小綿羊哥哥關(guān)注 2017.06.26 09:4...
    肇東周閱讀 12,105評(píng)論 4 62
  • Documentation Supported OS & SDK Versions 支持的OS & SDK版本 S...
    c5550ea746f8閱讀 4,350評(píng)論 0 2
  • 【從本文章中我學(xué)到的最重要的概念】我們要懂 得充分挖掘并發(fā)揮自己的潛能 【我在本文章中學(xué)到的怦然心動(dòng)的單詞】 re...
    145武鵬梅閱讀 211評(píng)論 2 0
  • 雖然這本書曾經(jīng)被"快樂大本營"里宣傳過,但是今日沉删,我還是忍不住要向大家介紹起這本書來渐尿,因?yàn)樗刮揖镁貌荒芡鼞选?本...
    喂歪閱讀 590評(píng)論 0 2