拿走即用之傻瓜式圖片輪播器

用法 (作者離職,求一份工作)

  • 只需要一個urlString數(shù)組,即可從后端服務(wù)器獲取圖片,并實現(xiàn)圖片輪播

先看看效果

優(yōu)化版圖片瀏覽器.gif

使用條件

  • 先導(dǎo)入SD_WebImage框架
  • 將WHCircleImageView、WHCirCleImageViewCell類導(dǎo)入項目(具體代碼見下面)

代碼

  • WHCircleImageView.h
#import <UIKit/UIKit.h>

@interface WHCircleImageView : UICollectionView


//提供一個接口
-(instancetype)initWithFrame:(CGRect)frame AndImageUrlArray:(NSArray *)ImageUrlArray view:(UIView *)view;
@end
  • WHCircleImageView.m

#import "WHCircleImageView.h"
#import "WHCirCleImageViewCell.h"
#import "UIImageView+WebCache.h"

//多少秒翻動一次圖片
#define changeImageTime 1.5
//pageControl的中心點位置
#define pageControlPosition CGPointMake(CGRectGetMaxX(frame)/2, CGRectGetMaxY(frame)-20)

@interface WHCircleImageView ()<UICollectionViewDelegate,UICollectionViewDataSource>
//接受傳來的ImageUrlArray
@property(nonatomic,strong)NSArray *ImageUrlArray;
//存儲下載的圖片
@property(nonatomic,strong)NSArray *imageViewArray;
// 當(dāng)前顯示圖片的索引
@property (nonatomic, assign) NSInteger currentIndex;
//提供一個timer,讓其自動循環(huán)播放
@property(nonatomic,strong)NSTimer *timer;
//全局屬性圖片滾動到的位置
@property (nonatomic,assign)NSInteger index;

@property (nonatomic,strong)UIPageControl * page;
@end

@implementation WHCircleImageView


-(instancetype)initWithFrame:(CGRect)frame AndImageUrlArray:(NSArray *)ImageUrlArray view:(UIView *)view{
    
    //創(chuàng)建流水布局
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.itemSize = CGSizeMake(frame.size.width, frame.size.height);
    layout.minimumInteritemSpacing = 0;
    layout.minimumLineSpacing = 0;
    layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    
    //創(chuàng)建collectionView
    self = [[WHCircleImageView alloc] initWithFrame:frame collectionViewLayout:layout];
    //注冊cell
    [self registerClass:[WHCirCleImageViewCell class] forCellWithReuseIdentifier:@"circle"];
    
    self.pagingEnabled = YES;
    self.showsHorizontalScrollIndicator = NO;

    self.delegate =self;
    self.dataSource = self;

    [self addTimer];
    
    self.index = self.ImageUrlArray.count*30;
    
    NSIndexPath * indexPath = [NSIndexPath indexPathForItem:self.index inSection:0];
    
    //默認(rèn)一開始滾動到第1000張圖片的位置
    [self scrollToItemAtIndexPath:indexPath atScrollPosition:UICollectionViewScrollPositionNone animated:YES];

    self.ImageUrlArray = ImageUrlArray;
    
    //添加pageController
    UIPageControl * page = [self createPage];
    
    //根據(jù)圖片張數(shù)獲取page的寬高
    CGSize  pageSize = [page sizeForNumberOfPages:self.ImageUrlArray.count];
    page.frame = CGRectMake(0, 0, pageSize.width, pageSize.height);
    page.center = pageControlPosition;
    self.page = page;
   
    
    [view addSubview:self];
    [view addSubview:page];
    return self;
}

//計時器的方法 自動滾動圖片
-(void)changeImage
{
    //調(diào)用計時器方法是 讓圖片索引++ 滾動圖片
    self.index++;
    
    NSIndexPath *indexpath = [NSIndexPath indexPathForItem:self.index inSection:0];
    
    [self scrollToItemAtIndexPath:indexpath atScrollPosition:UICollectionViewScrollPositionNone animated:YES];
    
    self.page.currentPage = self.index%self.imageViewArray.count;
}

// MARK:數(shù)據(jù)源方法

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{

    return 30000;
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    WHCirCleImageViewCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"circle" forIndexPath:indexPath];
    
    //取出圖片賦值給cell
    cell.UrlimageView = self.imageViewArray[indexPath.item%self.imageViewArray.count];
    
    return cell;
}
//當(dāng)手指拖動圖片時
-(void)scrollViewWillBeginDecelerating:(UIScrollView *)scrollView
{
    [self deleteTimer];
}

//滾動結(jié)束之后發(fā)送通知 讓控制器接收通知 設(shè)置page的當(dāng)前頁數(shù)
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    NSInteger index = scrollView.contentOffset.x/scrollView.bounds.size.width;
    
    self.index = index;
    
    self.page.currentPage = self.index%self.imageViewArray.count;
    
    [self addTimer];
}
-(void)addTimer
{
    NSTimer * timer = [NSTimer scheduledTimerWithTimeInterval:changeImageTime target:self selector:@selector(changeImage) userInfo:nil repeats:YES];
    
    //添加到運行循環(huán)
    [[NSRunLoop currentRunLoop] addTimer:timer forMode:NSDefaultRunLoopMode];
    
    self.timer = timer;
}

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

//創(chuàng)建pageControl
-(UIPageControl *)createPage
{
    //初始化方法
    UIPageControl * page = [[UIPageControl alloc]init];
    
    page.currentPage = 0;
    
    page.numberOfPages = self.ImageUrlArray.count;
    
    page.currentPageIndicatorTintColor = [UIColor orangeColor];
    
    page.pageIndicatorTintColor = [UIColor cyanColor];
    
    return page;
    
}

//當(dāng)手指拖動圖片時
//懶加載
-(NSArray *)imageViewArray{
    
    NSMutableArray *tempArray = [NSMutableArray arrayWithCapacity:self.ImageUrlArray.count];
    for (int i = 0; i < [_ImageUrlArray count]; i++) {
        NSString *imageUrl = self.ImageUrlArray[i];
        UIImageView *imageView = [[UIImageView alloc] initWithFrame:self.frame];
        UIImage *image = [UIImage imageNamed:@"new_feature_finish_button_highlighted"];
        [imageView sd_setImageWithURL:[NSURL URLWithString:imageUrl] placeholderImage:image];
        [tempArray addObject:imageView];
    }
    
    return tempArray;
}
@end
  • WHCirCleImageViewCell.h
#import <UIKit/UIKit.h>

@interface WHCirCleImageViewCell : UICollectionViewCell
//接受外部的圖片
@property(nonatomic,strong)UIImageView *UrlimageView;
@end
  • WHCirCleImageViewCell.m
#import "WHCirCleImageViewCell.h"

@interface WHCirCleImageViewCell ()

@property(nonatomic,strong)UIImageView *imageView;

@end

@implementation WHCirCleImageViewCell

-(void)setUrlimageView:(UIImageView *)UrlimageView{

    _UrlimageView = UrlimageView;
    self.imageView = UrlimageView;
    [self.contentView addSubview:UrlimageView];

}
@end
  • Controller中掉用
#import "ViewController.h"
#import "WHCircleImageView.h"

#define Application_Frame       [[UIScreen mainScreen] bounds]

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    NSArray *testArr = @[@"http://img4.duitang.com/uploads/item/201303/17/20130317090104_jF8iH.jpeg",@"http://imgsrc.baidu.com/forum/pic/item/0e2442a7d933c895d8064c31d11373f08202007b.jpg",@"http://d.3987.com/Qhyrz_130520/004.jpg"];

    WHCircleImageView *circleImageView = [[WHCircleImageView alloc] initWithFrame:CGRectMake(0, 0, Application_Frame.size.width, 250) AndImageUrlArray:testArr view:self.view];
#pragma 不要再掉用下面方法了,在上面的方法內(nèi)部已經(jīng)將circleImageView添加到self.view上過了
//    [self.view addSubview:circleImageView];
}
@end
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

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

  • Android 自定義View的各種姿勢1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 173,269評論 25 708
  • 發(fā)現(xiàn) 關(guān)注 消息 iOS 第三方庫、插件、知名博客總結(jié) 作者大灰狼的小綿羊哥哥關(guān)注 2017.06.26 09:4...
    肇東周閱讀 12,229評論 4 61
  • 在Ubuntu自帶的軟件中心里下載安裝的軟件,直接點擊“移除”,老是卸不掉! 試了很多次都不行,后來在網(wǎng)上看到的實...
    靜海潛蛟閱讀 29,061評論 0 5
  • 我是此間狂放人,酒樽盈舉灑昆侖。管他侯爵與王孫。 縱使明朝多落魄,玉珠金箔散如塵。不教俗事染吾身。
    曾慕青衫閱讀 305評論 3 9
  • 漢諾塔問題是一個經(jīng)典的問題。漢諾塔(Hanoi Tower),又稱河內(nèi)塔,源于印度一個古老傳說。大梵天創(chuàng)造世界的時...
    suniney閱讀 319評論 0 0