emotionView.m 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. //
  2. // emotionView.m
  3. // BuguLive
  4. //
  5. // Created by fanwe2014 on 16/7/19.
  6. // Copyright © 2016年 xfg. All rights reserved.
  7. //
  8. #import "emotionView.h"
  9. @implementation emotionView
  10. - (instancetype)initWithFrame:(CGRect)frame withName:(NSString *)name
  11. {
  12. self = [super initWithFrame:frame];
  13. if (self)
  14. {
  15. self.arrayCount = 0;
  16. @autoreleasepool
  17. {
  18. self = [super initWithFrame:frame];
  19. self.titleArray = @[ASLocalizedString(@"保密"),ASLocalizedString(@"单身"),ASLocalizedString(@"恋爱中"),ASLocalizedString(@"已婚"),ASLocalizedString(@"同性")];
  20. for (int j= 0 ; j < self.titleArray.count; j ++)
  21. {
  22. if ([self.titleArray[j] isEqualToString:name])
  23. {
  24. self.arrayCount = j;
  25. }
  26. }
  27. if (self)
  28. {
  29. self.backgroundColor = [UIColor whiteColor];
  30. for (int i = 0; i < 5; i ++)
  31. {
  32. UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
  33. button.frame = CGRectMake(kScreenW*0.05 +kScreenW*0.233*(i%4) , 30 +60*(i/4), kScreenW*0.2, 40);
  34. button.titleLabel.font = [UIFont systemFontOfSize:16];
  35. button.tag = 100+i;
  36. button.layer.cornerRadius = 5;
  37. if (self.arrayCount == i)
  38. {
  39. button.backgroundColor = kAppSecondaryColor;
  40. [button setTitleColor:kWhiteColor forState:0];
  41. }else
  42. {
  43. button.backgroundColor = kGrayTransparentColor1;
  44. [button setTitleColor:kAppGrayColor1 forState:0];
  45. }
  46. [button setTitle:self.titleArray[i] forState:0];
  47. [button addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
  48. [self addSubview:button];
  49. }
  50. }
  51. }
  52. }
  53. return self;
  54. }
  55. - (void)buttonClick:(UIButton *)button
  56. {
  57. for ( UIButton *btn in self.subviews)
  58. {
  59. //因为label和button都是继承制UIView,所以可以这样枚举
  60. //如果是label则删除
  61. if ([button isKindOfClass:[UIButton class]])
  62. {
  63. if (button.tag == btn.tag)
  64. {
  65. [btn setTitleColor:kWhiteColor forState:0];
  66. btn.backgroundColor = kAppSecondaryColor;
  67. }
  68. else
  69. {
  70. [btn setTitleColor:kAppGrayColor1 forState:0];
  71. btn.backgroundColor = kGrayTransparentColor1;
  72. }
  73. }
  74. }
  75. // dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5*NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  76. if (self.delegate)
  77. {
  78. if ([self.delegate respondsToSelector:@selector(changeEmotionStatuWithString:)])
  79. {
  80. [self.delegate changeEmotionStatuWithString:self.titleArray[button.tag-100]];
  81. }
  82. }
  83. // });
  84. }
  85. @end