码迷,mamicode.com
首页 > 其他好文 > 详细

masnory 高级进阶

时间:2018-07-07 20:13:29      阅读:281      评论:0      收藏:0      [点我收藏+]

标签:asa   一个   subarray   event   .com   sax   cell   log   ica   

ios 开发-UICollectionView详解+实例

Masnory 适配资料

  • 我实现的目标 ,在一个页面显示一个视图,或者4个视图,或者16个视图,因此我最开始实现思路是这样的:
//想法是好的,现实是错的
#define viewByIndex(x) ((UIView*)[array objectAtIndex:x])
-(void)colloctionTestWithCount:(int)count withRow:(int)row{
    NSMutableArray *array = [NSMutableArray new];
    for (int i=0; i!=count; i++) {
        UIView *view = [UIView new];
        view.backgroundColor = [UIColor colorWithRed:i*20/255.f green:i*15/255.f blue:i*30/255.f alpha:1.f];
        [self.view addSubview:view];
        [array addObject:view];
        view.layer.zPosition = i;
    }
    for (int i=0; i!=count/row; i++) {
        NSArray *arrayRow = [array subarrayWithRange:NSMakeRange(i*row, 4)];
        [arrayRow mas_distributeViewsAlongAxis:MASAxisTypeHorizontal withFixedSpacing:10 leadSpacing:10 tailSpacing:10];
        [arrayRow mas_makeConstraints:^(MASConstraintMaker *make){
           if (i==0) {
                 make.top.mas_equalTo(10);
            }else{
               make.top.mas_equalTo(viewByIndex(i*row).mas_bottom).offset(10);
            }
            make.height.mas_equalTo((self.view.frame.size.height-10*(row+1))/row);
        }];
    }
}

上面的代码是错的

  • 借鉴了两篇文章,我写下了如下代码:
    //初始化
    -(void)initCollectionView{
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    [flowLayout setScrollDirection:UICollectionViewScrollDirectionHorizontal];

    
    UICollectionView *collectionView = [[UICollectionView alloc]initWithFrame:CGRectZero collectionViewLayout:flowLayout];
    [self.view addSubview:collectionView];
    collectionView.alwaysBounceVertical = YES;
    collectionView.backgroundColor = [UIColor grayColor];
    collectionView.tag = 1000;
    

    [collectionView mas_makeConstraints:^(MASConstraintMaker* make){
        make.edges.insets(UIEdgeInsetsMake(20, 20, 100, 20));
    }];
    [collectionView setDelegate:self];
    [collectionView setDataSource:self];
    
    
    [collectionView registerClass:[CollectionViewCell class] forCellWithReuseIdentifier:identifierCollectionCell];
    [collectionView setDirectionalLockEnabled:YES];
}

-(void)initButton{
    UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
    [button setTitle:@"变换 " forState:UIControlStateNormal];
    [self.view addSubview:button];
    [button mas_makeConstraints:^(MASConstraintMaker* make){
        make.centerX.equalTo(self.view);
        make.centerY.equalTo(self.view.mas_bottom).offset(-50);
        make.size.mas_equalTo(CGSizeMake(60, 40));
    }];
    [button addTarget:self action:@selector(changeCollection:) forControlEvents:UIControlEventTouchDown];
}

-(IBAction)changeCollection:(id)sender{
    self.row = 4;
    UICollectionView *colletionView = [self.view viewWithTag:1000];
    [colletionView reloadData];
}
    #pragma collection datasource&delegate
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:identifierCollectionCell forIndexPath:indexPath];;
    cell.backgroundColor = [UIColor colorWithRed:((10 * indexPath.row) / 255.0) green:((20 * indexPath.row)/255.0) blue:((30 * indexPath.row)/255.0) alpha:1.0f];
    return cell;
}

-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    return UIEdgeInsetsMake(10, 10, 10, 10);
}


- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    return CGSizeMake((collectionView.frame.size.width-10*(self.row+1))/self.row,
                      (collectionView.frame.size.height-10*(self.row+1))/self.row
                      );
}

效果图如下:


技术分享图片

点击按钮后


技术分享图片

masnory 高级进阶

标签:asa   一个   subarray   event   .com   sax   cell   log   ica   

原文地址:https://www.cnblogs.com/RoysPhoneBlog/p/9277703.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!