MASExampleDistributeView.m 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. //
  2. // MASExampleDistributeView.m
  3. // Masonry iOS Examples
  4. //
  5. // Created by bibibi on 15/8/6.
  6. // Copyright (c) 2015年 Jonas Budelmann. All rights reserved.
  7. //
  8. #import "MASExampleDistributeView.h"
  9. @implementation MASExampleDistributeView
  10. - (id)init {
  11. self = [super init];
  12. if (!self) return nil;
  13. NSMutableArray *arr = @[].mutableCopy;
  14. for (int i = 0; i < 4; i++) {
  15. UIView *view = UIView.new;
  16. view.backgroundColor = [self randomColor];
  17. view.layer.borderColor = UIColor.blackColor.CGColor;
  18. view.layer.borderWidth = 2;
  19. [self addSubview:view];
  20. [arr addObject:view];
  21. }
  22. unsigned int type = arc4random()%4;
  23. switch (type) {
  24. case 0:
  25. [arr mas_distributeViewsAlongAxis:MASAxisTypeHorizon withFixedSpacing:20 leadSpacing:5 tailSpacing:5];
  26. [arr makeConstraints:^(MASConstraintMaker *make) {
  27. make.top.equalTo(@60);
  28. make.height.equalTo(@60);
  29. }];
  30. break;
  31. case 1:
  32. [arr mas_distributeViewsAlongAxis:MASAxisTypeVertical withFixedSpacing:20 leadSpacing:5 tailSpacing:5];
  33. [arr makeConstraints:^(MASConstraintMaker *make) {
  34. make.left.equalTo(@0);
  35. make.width.equalTo(@60);
  36. }];
  37. break;
  38. case 2:
  39. [arr mas_distributeViewsAlongAxis:MASAxisTypeHorizon withFixedItemLength:30 leadSpacing:5 tailSpacing:5];
  40. [arr makeConstraints:^(MASConstraintMaker *make) {
  41. make.top.equalTo(@60);
  42. make.height.equalTo(@60);
  43. }];
  44. break;
  45. case 3:
  46. [arr mas_distributeViewsAlongAxis:MASAxisTypeVertical withFixedItemLength:30 leadSpacing:5 tailSpacing:5];
  47. [arr makeConstraints:^(MASConstraintMaker *make) {
  48. make.left.equalTo(@0);
  49. make.width.equalTo(@60);
  50. }];
  51. break;
  52. default:
  53. break;
  54. }
  55. return self;
  56. }
  57. - (UIColor *)randomColor {
  58. CGFloat hue = ( arc4random() % 256 / 256.0 ); // 0.0 to 1.0
  59. CGFloat saturation = ( arc4random() % 128 / 256.0 ) + 0.5; // 0.5 to 1.0, away from white
  60. CGFloat brightness = ( arc4random() % 128 / 256.0 ) + 0.5; // 0.5 to 1.0, away from black
  61. return [UIColor colorWithHue:hue saturation:saturation brightness:brightness alpha:1];
  62. }
  63. @end