-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathComplexScrollViewController.m
169 lines (131 loc) · 5.42 KB
/
ComplexScrollViewController.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
//
// ComplexScrollViewController.m
// MasoryDemo
//
// Created by fanweilian on 2017/7/10.
// Copyright © 2017年 fanweilian. All rights reserved.
//
#import "ComplexScrollViewController.h"
@interface ComplexScrollViewController ()
@property (nonatomic,strong) NSMutableArray *labelArray;
@end
@implementation ComplexScrollViewController
- (void)viewDidLoad {
[super viewDidLoad];
_labelArray = [NSMutableArray array];
UIScrollView *scroll = [[UIScrollView alloc] init];
scroll.backgroundColor = [UIColor lightGrayColor];
[self.view addSubview:scroll];
UILabel *lastLabel = nil;
CGFloat screenWidth = [UIScreen mainScreen].bounds.size.width;
for ( int i=0; i< 20; i++) {
UILabel *textLabel = [[UILabel alloc] init];
textLabel.layer.borderColor = [UIColor redColor].CGColor;
textLabel.layer.borderWidth = 2;
textLabel.text = [self randomText];
textLabel.numberOfLines = 0;
textLabel.preferredMaxLayoutWidth = screenWidth - 20;
textLabel.textAlignment = NSTextAlignmentLeft;
textLabel.textColor = [self randomColor];
[scroll addSubview: textLabel];
textLabel.userInteractionEnabled = YES;
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(onTap:)];
[textLabel addGestureRecognizer:tap];
[textLabel mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.mas_equalTo(10);
make.right.mas_equalTo(self.view).offset(-10);
if (lastLabel) {
make.top.equalTo(lastLabel.mas_bottom).offset(20);
}else{
make.top.equalTo(scroll).offset(20);
}
make.height.mas_equalTo(40);
}];
lastLabel = textLabel;
[_labelArray addObject:[@[textLabel,@(NO)] mutableCopy]];
}
[scroll mas_makeConstraints:^(MASConstraintMaker *make) {
make.edges.mas_equalTo(self.view);
make.bottom.equalTo(lastLabel.mas_bottom).offset(20);
}];
// Do any additional setup after loading the view.
}
- (UIColor *)randomColor {
CGFloat hue = ( arc4random() % 256 / 256.0 ); // 0.0 to 1.0
CGFloat saturation = ( arc4random() % 128 / 256.0 ) + 0.5; // 0.5 to 1.0, away from white
CGFloat brightness = ( arc4random() % 128 / 256.0 ) + 0.5; // 0.5 to 1.0, away from black
return [UIColor colorWithHue:hue saturation:saturation brightness:brightness alpha:1];
}
- (NSString *)randomText {
CGFloat length = arc4random() % 50 + 5;
NSMutableString *str = [[NSMutableString alloc] init];
for (NSUInteger i = 0; i < length; ++i) {
[str appendString:@"测试数据很长,"];
}
return str;
}
- (NSMutableArray *)labelArray{
if (_labelArray == nil) {
_labelArray = [[NSMutableArray alloc] init];
}
return _labelArray;
}
- (void)onTap:(UITapGestureRecognizer *)sender {
UIView *tapView = sender.view;
NSUInteger index = 0;
for (NSMutableArray *array in _labelArray) {
UILabel *currentView = [array firstObject];
if (tapView == currentView) {
NSNumber *state = [array lastObject];
if ([state boolValue] == YES) { //当前是展开的,则收缩
[tapView mas_updateConstraints:^(MASConstraintMaker *make) {
make.height.mas_equalTo(40);
}];
}else{
UIView *preView = nil;
UIView *nextView =nil;
if (index-1 < _labelArray.count && index > 0) {
preView = [_labelArray[index -1] firstObject];
}
if (index+1 < _labelArray.count) {
nextView = [_labelArray[index +1] firstObject];
}
[currentView mas_remakeConstraints:^(MASConstraintMaker *make) {
if (preView) {
make.top.equalTo(preView.mas_bottom).offset(20);
}else{
make.top.mas_equalTo(20);
}
make.left.mas_equalTo(10);
make.right.mas_equalTo(-10);
}];
if (nextView) {
[nextView mas_updateConstraints:^(MASConstraintMaker *make) {
make.top.mas_equalTo(currentView.mas_bottom).offset(20);
}];
}
}
[array replaceObjectAtIndex:1 withObject:@(!state.boolValue)];
[self.view setNeedsUpdateConstraints];
[self.view updateConstraintsIfNeeded];
[UIView animateWithDuration:0.2 animations:^{
[self.view layoutIfNeeded];
}];
break;
}
index ++;
}
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
/*
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/
@end