MASExampleScrollView.m 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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); // let the UIScrollView knonw where the edge is the contentView
  36. // the size of UIScrollView's contentView must be able to caculated by the constraints. here set the width;
  37. // the height will be set bellow.
  38. make.width.equalTo(self.width);
  39. }];
  40. UIView *lastView;
  41. CGFloat height = 25;
  42. for (int i = 0; i < 10; i++) {
  43. UIView *view = UIView.new;
  44. view.backgroundColor = [self randomColor];
  45. [contentView addSubview:view];
  46. // Tap
  47. UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(singleTap:)];
  48. [view addGestureRecognizer:singleTap];
  49. [view mas_makeConstraints:^(MASConstraintMaker *make) {
  50. make.top.equalTo(lastView ? lastView.bottom : @0);
  51. make.left.equalTo(@0);
  52. make.width.equalTo(contentView.width);
  53. make.height.equalTo(@(height));
  54. }];
  55. height += 25;
  56. lastView = view;
  57. }
  58. [contentView makeConstraints:^(MASConstraintMaker *make) {
  59. make.bottom.equalTo(lastView.bottom);
  60. }];
  61. return self;
  62. }
  63. - (UIColor *)randomColor {
  64. CGFloat hue = ( arc4random() % 256 / 256.0 ); // 0.0 to 1.0
  65. CGFloat saturation = ( arc4random() % 128 / 256.0 ) + 0.5; // 0.5 to 1.0, away from white
  66. CGFloat brightness = ( arc4random() % 128 / 256.0 ) + 0.5; // 0.5 to 1.0, away from black
  67. return [UIColor colorWithHue:hue saturation:saturation brightness:brightness alpha:1];
  68. }
  69. - (void)singleTap:(UITapGestureRecognizer*)sender {
  70. [sender.view setAlpha:sender.view.alpha / 1.20]; // To see something happen on screen when you tap :O
  71. [self.scrollView scrollRectToVisible:sender.view.frame animated:YES];
  72. };
  73. @end