MASExampleArrayView.m 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. //
  2. // MASExampleArrayView.m
  3. // Masonry iOS Examples
  4. //
  5. // Created by Daniel Hammond on 11/26/13.
  6. // Copyright (c) 2013 Jonas Budelmann. All rights reserved.
  7. //
  8. #import "MASExampleArrayView.h"
  9. static CGFloat const kArrayExampleIncrement = 10.0;
  10. @interface MASExampleArrayView ()
  11. @property (nonatomic, assign) CGFloat offset;
  12. @property (nonatomic, strong) NSArray *buttonViews;
  13. @end
  14. @implementation MASExampleArrayView
  15. - (id)init
  16. {
  17. self = [super init];
  18. if (!self) return nil;
  19. UIButton *raiseButton = [UIButton buttonWithType:UIButtonTypeSystem];
  20. [raiseButton setTitle:@"Raise" forState:UIControlStateNormal];
  21. [raiseButton addTarget:self action:@selector(raiseAction) forControlEvents:UIControlEventTouchUpInside];
  22. [self addSubview:raiseButton];
  23. UIButton *centerButton = [UIButton buttonWithType:UIButtonTypeSystem];
  24. [centerButton setTitle:@"Center" forState:UIControlStateNormal];
  25. [centerButton addTarget:self action:@selector(centerAction) forControlEvents:UIControlEventTouchUpInside];
  26. [self addSubview:centerButton];
  27. UIButton *lowerButton = [UIButton buttonWithType:UIButtonTypeSystem];
  28. [lowerButton setTitle:@"Lower" forState:UIControlStateNormal];
  29. [lowerButton addTarget:self action:@selector(lowerAction) forControlEvents:UIControlEventTouchUpInside];
  30. [self addSubview:lowerButton];
  31. [lowerButton mas_makeConstraints:^(MASConstraintMaker *make) {
  32. make.left.equalTo(self).with.offset(10.0);
  33. }];
  34. [centerButton mas_makeConstraints:^(MASConstraintMaker *make) {
  35. make.centerX.equalTo(self);
  36. }];
  37. [raiseButton mas_makeConstraints:^(MASConstraintMaker *make) {
  38. make.right.equalTo(self).with.offset(-10);
  39. }];
  40. self.buttonViews = @[ raiseButton, lowerButton, centerButton ];
  41. return self;
  42. }
  43. - (void)centerAction
  44. {
  45. self.offset = 0.0;
  46. }
  47. - (void)raiseAction
  48. {
  49. self.offset -= kArrayExampleIncrement;
  50. }
  51. - (void)lowerAction
  52. {
  53. self.offset += kArrayExampleIncrement;
  54. }
  55. - (void)setOffset:(CGFloat)offset
  56. {
  57. _offset = offset;
  58. [self setNeedsUpdateConstraints];
  59. }
  60. - (void)updateConstraints
  61. {
  62. [super updateConstraints];
  63. [self.buttonViews updateConstraints:^(MASConstraintMaker *make) {
  64. make.baseline.equalTo(self.mas_centerY).with.offset(self.offset);
  65. }];
  66. }
  67. @end