iOS Collection滚动显示图像

来源:互联网 发布:淘宝职称论文署名单位 编辑:程序博客网 时间:2024/06/04 15:54

//

//  ViewController.m

//  070402连续显示图片

//

//  Created by tianshangrenjian on 15/7/4.

//  Copyright © 2015 tianshangrenjian. All rights reserved.

//

#define SCHCELL @"cell"

#import "ViewController.h"


@interface ViewController ()<UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout>

@property (weak, nonatomic) IBOutletUIScrollView *schScroll;

@property (strong ,nonatomic)NSMutableArray *pics;


@property (weak, nonatomic) IBOutletUICollectionView *schCollection;


@end


@implementation ViewController


- (NSMutableArray *)pics

{

   if (_pics==nil) {

        _pics=[NSMutableArrayarray];

    }

   return _pics;

}


- (NSInteger)collectionView:(nonnullUICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section

{

   NSLog(@"cao");

   return 9;

}

- (UICollectionViewCell*)collectionView:(nonnullUICollectionView *)collectionView cellForItemAtIndexPath:(nonnullNSIndexPath *)indexPath

{


    UICollectionViewCell *cell=[collectionViewdequeueReusableCellWithReuseIdentifier:SCHCELLforIndexPath:indexPath];

  

   NSString *path=[NSStringstringWithFormat:@"%zd.jpg",indexPath.row+1];

   NSLog(@"%@",path);

    UIImageView *view=[[UIImageViewalloc] initWithImage:[UIImageimageNamed:path]];

    view.contentMode=UIViewContentModeScaleToFill;

    view.contentScaleFactor=0;

//    view.frame=self.schCollection.bounds;

    view.frame=cell.bounds;

    [celladdSubview:view];

   return cell;

}


-(void)showPic

{

   CGFloat x=0;

    CGFloat width=self.schScroll.bounds.size.width;

    CGFloat height=self.schScroll.bounds.size.height;

    

   for (int i=1; i<10; i++) {

        UIImage *img=[UIImageimageNamed:[NSStringstringWithFormat:@"%d.jpg",i]];

        

       UIImageView *imgView=[[UIImageViewalloc] initWithImage:img];

        

        imgView.frame=CGRectMake(x,0, width, height);

        x+=width;

        [self.schScrolladdSubview:imgView];

    }

    

   self.schScroll.contentSize=CGSizeMake(9*width, height);

    

    

    //    self.schScroll.frame=CGRectMake(0, 0, imgView.frame.size.width*self.pics.count, imgView.frame.size.height);

    //    [self.schScroll addSubview:imgView];

    

   

}

- (void)viewDidLoad {

    [superviewDidLoad];

    // Do any additional setup after loading the view, typically from a nib.

    self.schCollection.delegate=self;


    self.schCollection.dataSource=self;

    [self.schCollectionregisterClass:[UICollectionViewCellclass] forCellWithReuseIdentifier:SCHCELL];

    

    [selfshowPic];

}


- (void)didReceiveMemoryWarning {

    [superdidReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}


@end


0 0
原创粉丝点击