CloudRouterShowView.m 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. //
  2. // CloudRouterShowView.m
  3. // zhuxun
  4. //
  5. // Created by winsoft on 17/6/28.
  6. //
  7. //
  8. #import "CloudRouterShowView.h"
  9. #define MarginLR 10
  10. #define MarginBetweenBtn 5
  11. #define MarginTB 4
  12. @interface CloudRouterShowView()
  13. @end
  14. @implementation CloudRouterShowView
  15. + (CloudRouterShowView *)cloudRouterShowViewWithItems:(NSArray *)items bkColors:(NSArray *)bkColors
  16. {
  17. CloudRouterShowView *cloudRouterShowView = [[CloudRouterShowView alloc]initCloudRouterShowViewWithItems:items bkColors:bkColors];
  18. return cloudRouterShowView;
  19. }
  20. - (instancetype)initCloudRouterShowViewWithItems:(NSArray *)items bkColors:(NSArray *)bkColors
  21. {
  22. if (self = [super init]) {
  23. for (int i = 0; i < items.count; i++) {
  24. UIButton *actionBtn = [[UIButton alloc]init];
  25. actionBtn.tag = i;
  26. [actionBtn setTitle:items[i] forState:UIControlStateNormal];
  27. [actionBtn setBackgroundColor:bkColors[i]];
  28. [actionBtn addTarget:self action:@selector(actionForBtn:) forControlEvents:UIControlEventTouchUpInside];
  29. [self addSubview:actionBtn];
  30. }
  31. }
  32. return self;
  33. }
  34. - (void)actionForBtn:(UIButton *)btn
  35. {
  36. if (self.actionBlock) {
  37. self.actionBlock(btn.tag);
  38. }
  39. }
  40. - (void)setIndexActionBtnEnableWithIndex:(NSInteger)index enable:(BOOL)enable
  41. {
  42. UIButton *targetBtn = self.subviews[index];
  43. targetBtn.enabled = enable;
  44. targetBtn.backgroundColor = [UIColor lightGrayColor];
  45. }
  46. - (void)layoutSubviews
  47. {
  48. [super layoutSubviews];
  49. CGFloat btnW = (self.frame.size.width - 2 * MarginLR - (self.subviews.count - 1) * MarginBetweenBtn)/self.subviews.count;
  50. CGFloat btnH = self.frame.size.height - 2 * MarginTB;
  51. for (UIButton *subBtn in self.subviews) {
  52. CGFloat btnX = subBtn.tag * (btnW + MarginBetweenBtn) + MarginLR;
  53. CGFloat btnY = (self.frame.size.height - btnH)/2.0f;
  54. subBtn.layer.masksToBounds = YES;
  55. subBtn.layer.cornerRadius = 2;
  56. subBtn.frame = CGRectMake(btnX, btnY, btnW, btnH);
  57. }
  58. }
  59. @end