美文网首页
10.8 Masonry-scrollview复杂约束

10.8 Masonry-scrollview复杂约束

作者: 草根小强 | 来源:发表于2019-04-28 14:24 被阅读0次

    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;
    }
    
    /*
     
     UILabel *nowLabel = (id)tap.view;
     
     for (int i = 0; i < self.selectArr.count; i++) {
     //大数组里是小数组
     NSMutableArray *subArr = self.selectArr[i];
     //小数组里第一个元素是label
     UILabel *label = [subArr firstObject];
     //小数组里最后一个元素是number
     NSNumber *numer = [subArr lastObject];
     
     BOOL isNormal = numer.boolValue;
     
     if (!isNormal) {
     
     }else{
     [nowLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
     make.left.mas_equalTo(10);
     make.right.mas_equalTo(self.view).offset(-10);
     
     if (nowLabel == self.selectArr[0][0]) {
     make.top.mas_equalTo(self.scrllV).offset(10);
     }else{
     //上一页label
     make.top.mas_equalTo(self.selectArr[(nowLabel.tag-1-10)][0]);
     //假设当前label的tag值13 -- 上一个label的tag值12 -- 对应数组里的索引2 -- 小数组的索引0
     }
     
     }];
     }
     }
    
     */
    @end
    
    展开后.png
    展开前.png

    相关文章

      网友评论

          本文标题:10.8 Masonry-scrollview复杂约束

          本文链接:https://www.haomeiwen.com/subject/tzpqnqtx.html