MASExampleScrollView.m 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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. [self generateContent];
  32. return self;
  33. }
  34. - (void)generateContent {
  35. UIView* contentView = UIView.new;
  36. [self.scrollView addSubview:contentView];
  37. [contentView makeConstraints:^(MASConstraintMaker *make) {
  38. make.edges.equalTo(self.scrollView);
  39. make.width.equalTo(self.scrollView);
  40. }];
  41. UIView *lastView;
  42. CGFloat height = 25;
  43. for (int i = 0; i < 10; i++) {
  44. UIView *view = UIView.new;
  45. view.backgroundColor = [self randomColor];
  46. [contentView addSubview:view];
  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. }
  62. - (UIColor *)randomColor {
  63. CGFloat hue = ( arc4random() % 256 / 256.0 ); // 0.0 to 1.0
  64. CGFloat saturation = ( arc4random() % 128 / 256.0 ) + 0.5; // 0.5 to 1.0, away from white
  65. CGFloat brightness = ( arc4random() % 128 / 256.0 ) + 0.5; // 0.5 to 1.0, away from black
  66. return [UIColor colorWithHue:hue saturation:saturation brightness:brightness alpha:1];
  67. }
  68. - (void)singleTap:(UITapGestureRecognizer*)sender {
  69. [sender.view setAlpha:sender.view.alpha / 1.20]; // To see something happen on screen when you tap :O
  70. [self.scrollView scrollRectToVisible:sender.view.frame animated:YES];
  71. };
  72. @end