MASExampleArrayView.m 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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. self = [super init];
  17. if (!self) return nil;
  18. UIButton *raiseButton = [UIButton buttonWithType:UIButtonTypeSystem];
  19. [raiseButton setTitle:@"Raise" forState:UIControlStateNormal];
  20. [raiseButton addTarget:self action:@selector(raiseAction) forControlEvents:UIControlEventTouchUpInside];
  21. [self addSubview:raiseButton];
  22. UIButton *centerButton = [UIButton buttonWithType:UIButtonTypeSystem];
  23. [centerButton setTitle:@"Center" forState:UIControlStateNormal];
  24. [centerButton addTarget:self action:@selector(centerAction) forControlEvents:UIControlEventTouchUpInside];
  25. [self addSubview:centerButton];
  26. UIButton *lowerButton = [UIButton buttonWithType:UIButtonTypeSystem];
  27. [lowerButton setTitle:@"Lower" forState:UIControlStateNormal];
  28. [lowerButton addTarget:self action:@selector(lowerAction) forControlEvents:UIControlEventTouchUpInside];
  29. [self addSubview:lowerButton];
  30. [lowerButton mas_makeConstraints:^(MASConstraintMaker *make) {
  31. make.left.equalTo(self).with.offset(10.0);
  32. }];
  33. [centerButton mas_makeConstraints:^(MASConstraintMaker *make) {
  34. make.centerX.equalTo(self);
  35. }];
  36. [raiseButton mas_makeConstraints:^(MASConstraintMaker *make) {
  37. make.right.equalTo(self).with.offset(-10);
  38. }];
  39. self.buttonViews = @[ raiseButton, lowerButton, centerButton ];
  40. return self;
  41. }
  42. - (void)centerAction {
  43. self.offset = 0.0;
  44. }
  45. - (void)raiseAction {
  46. self.offset -= kArrayExampleIncrement;
  47. }
  48. - (void)lowerAction {
  49. self.offset += kArrayExampleIncrement;
  50. }
  51. - (void)setOffset:(CGFloat)offset {
  52. _offset = offset;
  53. [self setNeedsUpdateConstraints];
  54. }
  55. - (void)updateConstraints {
  56. [self.buttonViews updateConstraints:^(MASConstraintMaker *make) {
  57. make.baseline.equalTo(self.mas_centerY).with.offset(self.offset);
  58. }];
  59. //according to apple super should be called at end of method
  60. [super updateConstraints];
  61. }
  62. @end