MASExampleScrollView.m 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. //
  2. // MASExampleScrollView.m
  3. // Masonry iOS Examples
  4. //
  5. // Created by Jonas Budelmann on 20/11/13.
  6. // Copyright (c) 2013 Jonas Budelmann. All rights reserved.
  7. //
  8. #import "MASExampleScrollView.h"
  9. /**
  10. * UIScrollView and Auto Layout don't play very nicely together see
  11. * https://developer.apple.com/library/ios/technotes/tn2154/_index.html
  12. *
  13. * This is an example of one workaround
  14. *
  15. * for another approach see https://github.com/bizz84/MVScrollViewAutoLayout
  16. */
  17. @interface MASExampleScrollView ()
  18. @property (strong, nonatomic) UIScrollView* scrollView;
  19. @end
  20. @implementation MASExampleScrollView
  21. - (id)init {
  22. self = [super init];
  23. if (!self) return nil;
  24. UIScrollView *scrollView = UIScrollView.new;
  25. self.scrollView = scrollView;
  26. scrollView.backgroundColor = [UIColor grayColor];
  27. [self addSubview:scrollView];
  28. [self.scrollView makeConstraints:^(MASConstraintMaker *make) {
  29. make.edges.equalTo(self);
  30. }];
  31. // We create a dummy contentView that will hold everything (necessary to use scrollRectToVisible later)
  32. UIView* contentView = UIView.new;
  33. [self.scrollView addSubview:contentView];
  34. [contentView makeConstraints:^(MASConstraintMaker *make) {
  35. make.edges.equalTo(self.scrollView);
  36. make.width.equalTo(self.scrollView.width);
  37. }];
  38. UIView *lastView;
  39. CGFloat height = 25;
  40. for (int i = 0; i < 10; i++) {
  41. UIView *view = UIView.new;
  42. view.backgroundColor = [self randomColor];
  43. [contentView addSubview:view];
  44. // Tap
  45. UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(singleTap:)];
  46. [view addGestureRecognizer:singleTap];
  47. [view mas_makeConstraints:^(MASConstraintMaker *make) {
  48. make.top.equalTo(lastView ? lastView.bottom : @0);
  49. make.left.equalTo(@0);
  50. make.width.equalTo(contentView.width);
  51. make.height.equalTo(@(height));
  52. }];
  53. height += 25;
  54. lastView = view;
  55. }
  56. // dummy view, which determines the size of the contentView size and therefore the scrollView contentSize
  57. UIView *sizingView = UIView.new;
  58. [scrollView addSubview:sizingView];
  59. [sizingView makeConstraints:^(MASConstraintMaker *make) {
  60. make.top.equalTo(lastView.bottom);
  61. make.bottom.equalTo(contentView.bottom);
  62. }];
  63. return self;
  64. }
  65. - (UIColor *)randomColor {
  66. CGFloat hue = ( arc4random() % 256 / 256.0 ); // 0.0 to 1.0
  67. CGFloat saturation = ( arc4random() % 128 / 256.0 ) + 0.5; // 0.5 to 1.0, away from white
  68. CGFloat brightness = ( arc4random() % 128 / 256.0 ) + 0.5; // 0.5 to 1.0, away from black
  69. return [UIColor colorWithHue:hue saturation:saturation brightness:brightness alpha:1];
  70. }
  71. - (void)singleTap:(UITapGestureRecognizer*)sender {
  72. [sender.view setAlpha:sender.view.alpha / 1.20]; // To see something happen on screen when you tap :O
  73. [self.scrollView scrollRectToVisible:sender.view.frame animated:YES];
  74. };
  75. @end