三張圖片輪播器<ios>

來源:互聯網
上載者:User

標籤:

三張圖片預設擺放;

實際索引值為0,1,2;

但是為了理解容易,所以設定為對稱的模型;我們把圖片放在一個數組中

圖片預設顯示第一張圖片,放在索引為1的位置;

當圖片往左滾動時,程式會調用cell的方法,在程式內部我們實現索引值為0的位置顯示最後一張圖片,然後再將圖片移動到索引為1的位置,

以此類推,當向右滾動時,同向左一樣,不同之處是在於,下一張圖片不是顯示圖片數組上一張圖片而是顯示圖片數組中下一張

具體代碼實現如下:(用Main.storyboard製作)

HmImgCell.h#import <UIKit/UIKit.h>@interface HmImgCell : UICollectionViewCell@property(nonatomic,strong)NSIndexPath*indexPath;@property(nonatomic,strong)UIImage*img;@end//.m#import "HmImgCell.h"@interface HmImgCell()@property (weak, nonatomic) IBOutlet UIImageView *iconView;@property (weak, nonatomic) IBOutlet UILabel *textView;@end@implementation HmImgCell-(void)setImg:(UIImage *)img{    _img=img;    self.iconView.image=img;}-(void)setIndexPath:(NSIndexPath *)indexPath{    _indexPath=indexPath;    self.textView.text=[NSString stringWithFormat:@"%ld---%ld",indexPath.section,indexPath.item];}@end///ViewController//.m#import "ViewController.h"#import "HmImgCell.h"#define cnt 9@interface ViewController ()@property (weak, nonatomic) IBOutlet UICollectionViewFlowLayout *layout;@property(nonatomic,assign)NSInteger currentIndex;@property(nonatomic,strong)NSArray* imgs;@end@implementation ViewController//實現懶載入-(NSArray*)imgs{    if (_imgs==nil) {        NSMutableArray*arr=[NSMutableArray arrayWithCapacity:cnt];        for (int i=0; i<cnt; i++) {//            把資料封裝            NSString*imgName=[NSString stringWithFormat:@"%d",i];            UIImage*img=[UIImage imageNamed:imgName];            [arr addObject:img];        }        _imgs=arr;    }    return _imgs;}- (void)viewDidLoad {    [super viewDidLoad];//設定layout的屬性    self.layout.itemSize=self.collectionView.frame.size;    self.layout.minimumLineSpacing=0;    self.layout.scrollDirection=UICollectionViewScrollDirectionHorizontal;    self.collectionView.pagingEnabled=YES;    self.collectionView.bounces=NO;//設定動畫    NSTimer*time=[NSTimer timerWithTimeInterval:2 target:self selector:@selector(nextImg) userInfo:nil repeats:YES];    NSRunLoop*run=[NSRunLoop currentRunLoop];    [run addTimer:time forMode:NSRunLoopCommonModes];//    先讓scolleView滾動到索引為一的位置    NSIndexPath*indexPath=[NSIndexPath indexPathForItem:1 inSection:0];    [self.collectionView scrollToItemAtIndexPath:indexPath atScrollPosition:UICollectionViewScrollPositionNone animated:NO];}//設定下一張圖片方法-(void)nextImg{//讓圖片滾動到原來索引為1的位置    [self.collectionView setContentOffset:CGPointMake(self.collectionView.frame.size.width*2, 0) animated:YES];}//確定多少item-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{    return self.imgs.count;}//確定每行展示的cell-(UICollectionViewCell*)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{    HmImgCell*cell=[collectionView dequeueReusableCellWithReuseIdentifier:@"img" forIndexPath:indexPath];//   擷取當前需要展示圖片的索引        NSInteger index=(indexPath.item-1+cnt+self.currentIndex)%cnt;//賦值    cell.img=self.imgs[index];    cell.indexPath=indexPath;    return cell;}//當拖拽停止時調用的方法-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{//讓圖片回到原來的位置    //    計算當前位移量的索引        NSInteger offsetIndex=(self.collectionView.contentOffset.x)/self.collectionView.frame.size.width-1;//    當前位置的位移量    self.currentIndex=(offsetIndex+cnt+self.currentIndex)%cnt;    NSIndexPath*index=[NSIndexPath indexPathForItem:1 inSection:0];    [self.collectionView scrollToItemAtIndexPath:index atScrollPosition:UICollectionViewScrollPositionNone animated:NO];//關閉動畫    [UIView setAnimationsEnabled:NO];//    重新整理當前圖片    [self.collectionView reloadItemsAtIndexPaths:@[index]];//開啟動畫    [UIView setAnimationsEnabled:YES];}//當開啟動畫停止滾動時用上面的方法-(void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView{    [self scrollViewDidEndDecelerating:scrollView];}@end

  

三張圖片輪播器<ios>

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.