Updated UIScrollView example in order to use scrollRectToVisible
With the previous example we encountered that scrollRectToVisible method was broken. I added a tap gesture to every view to test it :) This is fixed by adding a "content" UIView inside the scrollView where we will add everything we need (in this case randomly colored views).
Showing
1 changed file
with
30 additions
and
10 deletions
@@ -17,6 +17,10 @@ | @@ -17,6 +17,10 @@ | ||
17 | * for another approach see https://github.com/bizz84/MVScrollViewAutoLayout | 17 | * for another approach see https://github.com/bizz84/MVScrollViewAutoLayout |
18 | */ | 18 | */ |
19 | 19 | ||
20 | +@interface MASExampleScrollView () | ||
21 | +@property (strong, nonatomic) UIScrollView* scrollView; | ||
22 | +@end | ||
23 | + | ||
20 | @implementation MASExampleScrollView | 24 | @implementation MASExampleScrollView |
21 | 25 | ||
22 | - (id)init { | 26 | - (id)init { |
@@ -24,39 +28,50 @@ | @@ -24,39 +28,50 @@ | ||
24 | if (!self) return nil; | 28 | if (!self) return nil; |
25 | 29 | ||
26 | UIScrollView *scrollView = UIScrollView.new; | 30 | UIScrollView *scrollView = UIScrollView.new; |
31 | + self.scrollView = scrollView; | ||
27 | scrollView.backgroundColor = [UIColor grayColor]; | 32 | scrollView.backgroundColor = [UIColor grayColor]; |
28 | [self addSubview:scrollView]; | 33 | [self addSubview:scrollView]; |
29 | - | ||
30 | - [scrollView makeConstraints:^(MASConstraintMaker *make) { | 34 | + [self.scrollView makeConstraints:^(MASConstraintMaker *make) { |
31 | make.edges.equalTo(self); | 35 | make.edges.equalTo(self); |
32 | }]; | 36 | }]; |
33 | - | 37 | + |
38 | + // We create a dummy contentView that will hold everything (necessary to use scrollRectToVisible later) | ||
39 | + UIView* contentView = UIView.new; | ||
40 | + [self.scrollView addSubview:contentView]; | ||
41 | + [contentView makeConstraints:^(MASConstraintMaker *make) { | ||
42 | + make.edges.equalTo(self.scrollView); | ||
43 | + make.width.equalTo(self.scrollView.width); | ||
44 | + }]; | ||
45 | + | ||
34 | UIView *lastView; | 46 | UIView *lastView; |
35 | - CGFloat height = 20; | 47 | + CGFloat height = 25; |
36 | 48 | ||
37 | for (int i = 0; i < 10; i++) { | 49 | for (int i = 0; i < 10; i++) { |
38 | UIView *view = UIView.new; | 50 | UIView *view = UIView.new; |
39 | view.backgroundColor = [self randomColor]; | 51 | view.backgroundColor = [self randomColor]; |
40 | - [scrollView addSubview:view]; | 52 | + [contentView addSubview:view]; |
41 | 53 | ||
54 | + // Tap | ||
55 | + UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(singleTap:)]; | ||
56 | + [view addGestureRecognizer:singleTap]; | ||
57 | + | ||
42 | [view mas_makeConstraints:^(MASConstraintMaker *make) { | 58 | [view mas_makeConstraints:^(MASConstraintMaker *make) { |
43 | make.top.equalTo(lastView ? lastView.bottom : @0); | 59 | make.top.equalTo(lastView ? lastView.bottom : @0); |
44 | make.left.equalTo(@0); | 60 | make.left.equalTo(@0); |
45 | - make.width.equalTo(scrollView.width); | 61 | + make.width.equalTo(contentView.width); |
46 | make.height.equalTo(@(height)); | 62 | make.height.equalTo(@(height)); |
47 | }]; | 63 | }]; |
48 | 64 | ||
49 | - height += 20; | 65 | + height += 25; |
50 | lastView = view; | 66 | lastView = view; |
51 | } | 67 | } |
52 | 68 | ||
53 | - // dummy view, which determines the contentSize of scroll view | 69 | + // dummy view, which determines the size of the contentView size and therefore the scrollView contentSize |
54 | UIView *sizingView = UIView.new; | 70 | UIView *sizingView = UIView.new; |
55 | [scrollView addSubview:sizingView]; | 71 | [scrollView addSubview:sizingView]; |
56 | - | ||
57 | [sizingView makeConstraints:^(MASConstraintMaker *make) { | 72 | [sizingView makeConstraints:^(MASConstraintMaker *make) { |
58 | make.top.equalTo(lastView.bottom); | 73 | make.top.equalTo(lastView.bottom); |
59 | - make.bottom.equalTo(scrollView.bottom); | 74 | + make.bottom.equalTo(contentView.bottom); |
60 | }]; | 75 | }]; |
61 | 76 | ||
62 | return self; | 77 | return self; |
@@ -69,4 +84,9 @@ | @@ -69,4 +84,9 @@ | ||
69 | return [UIColor colorWithHue:hue saturation:saturation brightness:brightness alpha:1]; | 84 | return [UIColor colorWithHue:hue saturation:saturation brightness:brightness alpha:1]; |
70 | } | 85 | } |
71 | 86 | ||
87 | +- (void)singleTap:(UITapGestureRecognizer*)sender { | ||
88 | + [sender.view setAlpha:sender.view.alpha / 1.20]; // To see something happen on screen when you tap :O | ||
89 | + [self.scrollView scrollRectToVisible:sender.view.frame animated:YES]; | ||
90 | +}; | ||
91 | + | ||
72 | @end | 92 | @end |
-
Please register or login to post a comment