MMSheetView.m 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. //
  2. // MMSheetView.m
  3. // MMPopupView
  4. //
  5. // Created by Ralph Li on 9/6/15.
  6. // Copyright © 2015 LJC. All rights reserved.
  7. //
  8. #import "MMSheetView.h"
  9. #import "MMPopupItem.h"
  10. #import "MMPopupCategory.h"
  11. #import "MMPopupDefine.h"
  12. #import <Masonry/Masonry.h>
  13. @interface MMSheetView()
  14. @property (nonatomic, strong) UIView *titleView;
  15. @property (nonatomic, strong) UILabel *titleLabel;
  16. @property (nonatomic, strong) UIView *buttonView;
  17. @property (nonatomic, strong) UIButton *cancelButton;
  18. @property (nonatomic, strong) NSArray *actionItems;
  19. @end
  20. @implementation MMSheetView
  21. - (instancetype)initWithTitle:(NSString *)title items:(NSArray *)items
  22. {
  23. self = [super init];
  24. if ( self )
  25. {
  26. NSAssert(items.count>0, @"Could not find any items.");
  27. MMSheetViewConfig *config = [MMSheetViewConfig globalConfig];
  28. self.type = MMPopupTypeSheet;
  29. self.actionItems = items;
  30. self.backgroundColor = config.splitColor;
  31. [self mas_makeConstraints:^(MASConstraintMaker *make) {
  32. make.width.mas_equalTo([UIScreen mainScreen].bounds.size.width);
  33. }];
  34. [self setContentCompressionResistancePriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisHorizontal];
  35. [self setContentHuggingPriority:UILayoutPriorityFittingSizeLevel forAxis:UILayoutConstraintAxisVertical];
  36. MASViewAttribute *lastAttribute = self.mas_top;
  37. if ( title.length > 0 )
  38. {
  39. self.titleView = [UIView new];
  40. [self addSubview:self.titleView];
  41. [self.titleView mas_makeConstraints:^(MASConstraintMaker *make) {
  42. make.left.right.top.equalTo(self);
  43. }];
  44. self.titleView.backgroundColor = config.backgroundColor;
  45. self.titleLabel = [UILabel new];
  46. [self.titleView addSubview:self.titleLabel];
  47. [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
  48. make.edges.equalTo(self.titleView).insets(UIEdgeInsetsMake(config.innerMargin, config.innerMargin, config.innerMargin, config.innerMargin));
  49. }];
  50. self.titleLabel.textColor = config.titleColor;
  51. self.titleLabel.font = [UIFont systemFontOfSize:config.titleFontSize];
  52. self.titleLabel.textAlignment = NSTextAlignmentCenter;
  53. self.titleLabel.numberOfLines = 0;
  54. self.titleLabel.text = title;
  55. lastAttribute = self.titleView.mas_bottom;
  56. }
  57. self.buttonView = [UIView new];
  58. [self addSubview:self.buttonView];
  59. [self.buttonView mas_makeConstraints:^(MASConstraintMaker *make) {
  60. make.left.right.equalTo(self);
  61. make.top.equalTo(lastAttribute);
  62. }];
  63. __block UIButton *firstButton = nil;
  64. __block UIButton *lastButton = nil;
  65. for ( NSInteger i = 0 ; i < items.count; ++i )
  66. {
  67. MMPopupItem *item = items[i];
  68. UIButton *btn = [UIButton mm_buttonWithTarget:self action:@selector(actionButton:)];
  69. [self.buttonView addSubview:btn];
  70. btn.tag = i;
  71. [btn mas_makeConstraints:^(MASConstraintMaker *make) {
  72. make.left.right.equalTo(self.buttonView).insets(UIEdgeInsetsMake(0, -MM_SPLIT_WIDTH, 0, -MM_SPLIT_WIDTH));
  73. make.height.mas_equalTo(config.buttonHeight);
  74. if ( !firstButton )
  75. {
  76. firstButton = btn;
  77. make.top.equalTo(self.buttonView.mas_top).offset(-MM_SPLIT_WIDTH);
  78. }
  79. else
  80. {
  81. make.top.equalTo(lastButton.mas_bottom).offset(-MM_SPLIT_WIDTH);
  82. make.height.equalTo(firstButton);
  83. }
  84. lastButton = btn;
  85. }];
  86. [btn setBackgroundImage:[UIImage mm_imageWithColor:config.backgroundColor] forState:UIControlStateNormal];
  87. [btn setBackgroundImage:[UIImage mm_imageWithColor:config.backgroundColor] forState:UIControlStateDisabled];
  88. [btn setBackgroundImage:[UIImage mm_imageWithColor:config.itemPressedColor] forState:UIControlStateHighlighted];
  89. [btn setTitle:item.title forState:UIControlStateNormal];
  90. [btn setTitleColor:item.highlight?config.itemHighlightColor:item.disabled?config.itemDisableColor:config.itemNormalColor forState:UIControlStateNormal];
  91. btn.titleLabel.font = [UIFont systemFontOfSize:config.buttonFontSize];
  92. btn.layer.borderWidth = MM_SPLIT_WIDTH;
  93. btn.layer.borderColor = config.splitColor.CGColor;
  94. btn.enabled = !item.disabled;
  95. }
  96. [lastButton mas_updateConstraints:^(MASConstraintMaker *make) {
  97. make.bottom.equalTo(self.buttonView.mas_bottom).offset(MM_SPLIT_WIDTH);
  98. }];
  99. self.cancelButton = [UIButton mm_buttonWithTarget:self action:@selector(actionCancel)];
  100. [self addSubview:self.cancelButton];
  101. [self.cancelButton mas_makeConstraints:^(MASConstraintMaker *make) {
  102. make.left.right.equalTo(self.buttonView);
  103. make.height.mas_equalTo(config.buttonHeight);
  104. make.top.equalTo(self.buttonView.mas_bottom).offset(8);
  105. }];
  106. self.cancelButton.titleLabel.font = [UIFont systemFontOfSize:config.buttonFontSize];
  107. [self.cancelButton setBackgroundImage:[UIImage mm_imageWithColor:config.backgroundColor] forState:UIControlStateNormal];
  108. [self.cancelButton setBackgroundImage:[UIImage mm_imageWithColor:config.itemPressedColor] forState:UIControlStateHighlighted];
  109. [self.cancelButton setTitle:config.defaultTextCancel forState:UIControlStateNormal];
  110. [self.cancelButton setTitleColor:config.itemNormalColor forState:UIControlStateNormal];
  111. [self mas_updateConstraints:^(MASConstraintMaker *make) {
  112. make.bottom.equalTo(self.cancelButton.mas_bottom);
  113. }];
  114. }
  115. return self;
  116. }
  117. - (void)actionButton:(UIButton*)btn
  118. {
  119. MMPopupItem *item = self.actionItems[btn.tag];
  120. [self hide];
  121. if ( item.handler )
  122. {
  123. item.handler(btn.tag);
  124. }
  125. }
  126. - (void)actionCancel
  127. {
  128. [self hide];
  129. }
  130. @end
  131. @interface MMSheetViewConfig()
  132. @end
  133. @implementation MMSheetViewConfig
  134. + (MMSheetViewConfig *)globalConfig
  135. {
  136. static MMSheetViewConfig *config;
  137. static dispatch_once_t onceToken;
  138. dispatch_once(&onceToken, ^{
  139. config = [MMSheetViewConfig new];
  140. });
  141. return config;
  142. }
  143. - (instancetype)init
  144. {
  145. self = [super init];
  146. if ( self )
  147. {
  148. self.buttonHeight = 50.0f;
  149. self.innerMargin = 19.0f;
  150. self.titleFontSize = 14.0f;
  151. self.buttonFontSize = 17.0f;
  152. self.backgroundColor = MMHexColor(0xFFFFFFFF);
  153. self.titleColor = MMHexColor(0x666666FF);
  154. self.splitColor = MMHexColor(0xCCCCCCFF);
  155. self.itemNormalColor = MMHexColor(0x333333FF);
  156. self.itemDisableColor = MMHexColor(0xCCCCCCFF);
  157. self.itemHighlightColor = MMHexColor(0xE76153FF);
  158. self.itemPressedColor = MMHexColor(0xEFEDE7FF);
  159. self.defaultTextCancel = ASLocalizedString(@"取消");
  160. }
  161. return self;
  162. }
  163. @end