#import "ViewController.h" #import <Masonry/Masonry.h> @interface ViewController () @property (nonatomic, strong) UIScrollView *scrollView; @property (nonatomic, strong) NSMutableArray *dataArrayM; @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; [self.view addSubview:self.scrollView]; [self createLabels]; } - (void)createLabels { UILabel *lastLabel = nil; for (NSInteger i = 0; i < 10; i++) { UILabel *label = [[UILabel alloc] init]; label.numberOfLines = 0; label.layer.borderColor = [UIColor greenColor].CGColor; Label. Layer. BorderWidth = 2.0; label.text = [self randomText]; label.userInteractionEnabled = YES; UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(onTap:)]; [label addGestureRecognizer:tap]; label.textAlignment = NSTextAlignmentLeft; label.textColor = [self randomColor]; [self.scrollView addSubview:label]; [label mas_makeConstraints:^(MASConstraintMaker *make) { make.left.mas_equalTo(15); make.right.mas_equalTo(self.view.mas_right).mas_offset(-15); make.height.mas_equalTo(40); if (lastLabel) { make.top.mas_equalTo(lastLabel.mas_bottom).mas_offset(20); } else { make.top.mas_equalTo(self.scrollView.mas_top).mas_offset(20); } }]; lastLabel = label; [self.dataArrayM addObject:[@[label, @(NO)] mutableCopy]]; } [self.scrollView mas_makeConstraints:^(MASConstraintMaker *make) { make.edges.mas_equalTo(self.view); Make.bottom.mas_equalto (lastlabel.mas_bottom).mas_offset(20); } - (UIScrollView *)scrollView { if (! _scrollView) { _scrollView = [[UIScrollView alloc] init]; _scrollView.pagingEnabled = NO; _scrollView.backgroundColor = [UIColor lightGrayColor]; } return _scrollView; } - (void)onTap:(UITapGestureRecognizer *)sender { UIView *tapView = sender.view; // Get the click Label NSUInteger index = 0; for (NSMutableArray *array in self.dataArrayM) { UILabel *view = [array firstObject]; if (view == tapView) { NSNumber *state = [array lastObject]; If ([state boolValue] == YES) { [mas_updateConstraints:^(MASConstraintMaker *make) {make.height.mas_equalto (40); } else { UIView *preView = nil; UIView *nextView = nil; if (index - 1 < self.dataArrayM.count && index >= 1) { preView = [[self.dataArrayM objectAtIndex:index - 1] firstObject]; } if (index + 1 < self.dataArrayM.count) { nextView = [[self.dataArrayM objectAtIndex:index + 1] firstObject]; } [view mas_remakeConstraints:^(MASConstraintMaker *make) { make.left.mas_equalTo(15); make.right.mas_equalTo(self.view.mas_right).mas_offset(-15); if (preView) { make.top.mas_equalTo(preView.mas_bottom).mas_offset(20); } else { make.top.mas_equalTo(20); } }]; if (nextView) { [nextView mas_updateConstraints:^(MASConstraintMaker *make) { make.top.mas_equalTo(view.mas_bottom).mas_offset(20); }]; } } [array replaceObjectAtIndex:1 withObject:@(!state.boolValue)]; Animations :^{[self.view layoutIfNeeded];} completion:^(BOOL finished) {}]; [UIView animateWithDuration:0.35 animations:^{[self.view layoutIfNeeded];} completion:^(BOOL finished) {}]; break; } index++; } } - (NSMutableArray *)dataArrayM { if (! _dataArrayM) { _dataArrayM = [NSMutableArray array]; } return _dataArrayM; } - (UIColor *)randomColor {CGFloat hue = (arc4random() % 256/256.0); CGFloat saturation = (arc4random() % 128/256.0) + 0.5; CGFloat Brightness = (arc4Random () % 128/256.0) + 0.5; return [UIColor colorWithHue:hue saturation:saturation brightness:brightness alpha:1]; } - (NSString *)randomText { CGFloat count = arc4random() % 50 + 5; NSMutableString *str = [[NSMutableString alloc] init]; for (NSInteger i = 0; i < count; [STR appendString:@" Test data is long, "]; } return str; } @endCopy the code