11.1 Masonry-scrollview复杂约束

Masonry-scrollview复杂约束

#import "ViewController.h"

#import "Masonry.h"

@interface ViewController ()

@property (nonatomic,strong) UIScrollView *scrllV;

@property (nonatomic,strong) NSMutableArray*dataArr;

@property (nonatomic,strong) NSMutableArray *selectArr;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.selectArr = [[NSMutableArray alloc]init];
    self.scrllV = [[UIScrollView alloc]init];
    self.scrllV.pagingEnabled = NO;
    self.scrllV.backgroundColor = [UIColor grayColor];
    [self.view addSubview:self.scrllV];
    
    UILabel *lastLabel = nil;
    for (int i = 0; i < 10; i++) {
        UILabel *label = [[UILabel alloc]init];
        label.tag = 10+i;
        label.numberOfLines = 0;
        label.backgroundColor = [UIColor colorWithRed:arc4random()%255/255.0 green:arc4random()%255/255.0 blue:arc4random()%255/255.0 alpha:1];
        label.text = [self randStr];
        [self.scrllV addSubview:label];
        
        [label mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(10);
            make.right.mas_equalTo(self.view).offset(-10);
            make.height.mas_equalTo(40);
            
            if (lastLabel) {
                make.top.mas_equalTo(lastLabel.mas_bottom).offset(10);
            }else{
                make.top.mas_equalTo(10);
            }
        }];
        
        lastLabel = label;
        //打开用户交互 默认是关闭的
        label.userInteractionEnabled = YES;
        //在label上添加手势
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(clickLabel:)];
        [label addGestureRecognizer:tap];
        
//        NSArray *arr = @[label,@NO];//不可变数组
        //@NO NSNumber类型的BOOL值
//        NSMutableArray *marr = [[NSMutableArray alloc]init];
//        [marr addObject:label];
//        [marr addObject:@NO];
        [self.selectArr addObject:[@[label,@NO] mutableCopy]];
    }
    
    [self.scrllV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(0);
        make.bottom.mas_equalTo(lastLabel.mas_bottom).offset(10);
    }];
    
}
- (void)clickLabel:(UITapGestureRecognizer *)tap{
    
    UILabel *nowLabel = (id)tap.view;
    
    NSNumber *num = self.selectArr[nowLabel.tag-10][1];
    //0 ---label 1 -- @NO
    
    if (num.boolValue) {
        // 布尔值为YES的时候收起
        [nowLabel mas_updateConstraints:^(MASConstraintMaker *make) {
            make.height.mas_equalTo(40);
        }];
        
    }else{ //布尔值为NO的时候展开
        //重置约束
        [nowLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
            
            make.left.mas_equalTo(10);
            make.right.mas_equalTo(self.view).offset(-10);
            //判断两种情况
            if (nowLabel.tag == 10) {
                //点击的是第一个label
                make.top.mas_equalTo(self.scrllV).offset(10);
            }else{
                //点击的是最后一个label
                
                NSArray *arr = self.selectArr[nowLabel.tag-1-10];
                //label @NO
                UILabel *label = arr[0];
                //top的约束参考上一个label的bottom
                make.top.mas_equalTo(label.mas_bottom).offset(10);
            }
        }];
    }

    BOOL isNormal = num.boolValue;
    isNormal = !isNormal;

    [self.selectArr[nowLabel.tag-10] replaceObjectAtIndex:1 withObject:@(isNormal)];
    
    [self.view setNeedsUpdateConstraints];
    [self.view updateConstraintsIfNeeded];
    
    [UIView animateWithDuration:0.5 animations:^{
        
        [self.view layoutIfNeeded];
    } completion:^(BOOL finished) {}];
    
}
- (NSString *)randStr{
    
    int num = arc4random()%20+20;
    NSMutableString *str = [NSMutableString string];
    for (int i = 0; i < num; i++) {
        [str appendString:@"随机数据..."];
    }
    return str;
}
展开前.png

展开后.png

你可能感兴趣的:(11.1 Masonry-scrollview复杂约束)