HobbyViewController.m 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. //
  2. // HobbyViewController.m
  3. // model
  4. //
  5. // Created by Drew on 2018/11/6.
  6. // Copyright © 2018 Mine. All rights reserved.
  7. //
  8. #import "HobbyViewController.h"
  9. #import "TagCollectionViewCell.h"
  10. #import <TTGTagCollectionView/TTGTagCollectionView.h>
  11. @interface HobbyViewController () <TTGTagCollectionViewDelegate, TTGTagCollectionViewDataSource>
  12. @property(nonatomic, strong) UILabel *infoLabel;
  13. @property(nonatomic, strong) TTGTagCollectionView *listTagView;
  14. @property(nonatomic, strong) NSMutableArray *dataSource;
  15. @property(strong, nonatomic) NSMutableArray <UIView *> *tagViews;
  16. @property(nonatomic, strong) NSMutableArray *selectTag;
  17. @end
  18. @implementation HobbyViewController
  19. - (void)viewDidLoad {
  20. [super viewDidLoad];
  21. [self configUI];
  22. }
  23. - (void)configUI {
  24. self.view.backgroundColor = RGBValueColor(0xf7f7f7, 1.0);
  25. self.title = @"兴趣爱好";
  26. self.navigationController.navigationBar.tintColor = RGBValueColor(0x333333, 1);
  27. self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"保存" style:UIBarButtonItemStylePlain target:self action:@selector(handleSave)];
  28. self.infoLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, 20, ScreenWidth - 40, 20)];
  29. self.infoLabel.font = [UIFont systemFontOfSize:14];
  30. self.infoLabel.text = @"选择你的兴趣爱好(至多选6个)";
  31. self.infoLabel.textColor = RGBValueColor(0x666666, 1);
  32. [self.view addSubview:self.infoLabel];
  33. self.listTagView = [[TTGTagCollectionView alloc] initWithFrame:CGRectMake(20, 60, ScreenWidth - 40, ScreenHeight - 60)];
  34. self.listTagView.delegate = self;
  35. self.listTagView.dataSource = self;
  36. self.listTagView.verticalSpacing = 10;
  37. self.listTagView.horizontalSpacing = 15;
  38. [self.view addSubview:self.listTagView];
  39. _tagViews = [NSMutableArray new];
  40. NSArray *hobbyArray = @[@"网球", @"高尔夫球", @"桌球", @"滑板", @"跑步", @"瑜伽", @"游泳", @"健身", @"篮球", @"足球",
  41. @"机车", @"看电影", @"话剧", @"逛街", @"葡萄酒", @"美食", @"蹦迪", @"唱歌", @"跳舞", @"宅女"];
  42. [self.dataSource addObjectsFromArray:hobbyArray];
  43. for (NSString *hobby in hobbyArray) {
  44. [_tagViews addObject:[self newButtonWithTitle:hobby]];
  45. }
  46. [self.listTagView reload];
  47. }
  48. #pragma mark - TTGTagCollectionViewDelegate
  49. - (CGSize)tagCollectionView:(TTGTagCollectionView *)tagCollectionView sizeForTagAtIndex:(NSUInteger)index {
  50. return _tagViews[index].frame.size;
  51. }
  52. - (void)tagCollectionView:(TTGTagCollectionView *)tagCollectionView didSelectTag:(UIView *)tagView atIndex:(NSUInteger)index {
  53. }
  54. - (BOOL)tagCollectionView:(TTGTagCollectionView *)tagCollectionView shouldSelectTag:(UIView *)tagView atIndex:(NSUInteger)index {
  55. return YES;
  56. }
  57. #pragma mark - TTGTagCollectionViewDataSource
  58. - (NSUInteger)numberOfTagsInTagCollectionView:(TTGTagCollectionView *)tagCollectionView {
  59. return _tagViews.count;
  60. }
  61. - (UIView *)tagCollectionView:(TTGTagCollectionView *)tagCollectionView tagViewForIndex:(NSUInteger)index {
  62. return _tagViews[index];
  63. }
  64. #pragma mark - Private methods
  65. - (UIButton *)newButtonWithTitle:(NSString *)title {
  66. UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
  67. UIColor *selectColor = [self randomColors][arc4random() % 12];
  68. button.titleLabel.font = [UIFont systemFontOfSize:14];
  69. [button setTitle:title forState:UIControlStateNormal];
  70. [button sizeToFit];
  71. button.layer.masksToBounds = YES;
  72. button.layer.cornerRadius = 15.0f;
  73. [button setTitleColor:selectColor forState:UIControlStateNormal];
  74. [button setTitleColor:[UIColor whiteColor] forState:UIControlStateSelected];
  75. button.layer.borderWidth = 1.f;
  76. button.layer.borderColor = selectColor.CGColor;
  77. [self expandSizeForView:button extraWidth:15 extraHeight:4];
  78. [button addTarget:self action:@selector(onTap:) forControlEvents:UIControlEventTouchUpInside];
  79. return button;
  80. }
  81. - (void)expandSizeForView:(UIView *)view extraWidth:(CGFloat)extraWidth extraHeight:(CGFloat)extraHeight {
  82. CGRect frame = view.frame;
  83. frame.size.width += extraWidth;
  84. frame.size.height += extraHeight;
  85. view.frame = frame;
  86. }
  87. - (void)onTap:(UIButton *)sender {
  88. if (!sender.selected && self.selectTag.count == 6) {
  89. [MBProgressHUD showOnlyText:@"最多选择6个"];
  90. } else {
  91. sender.selected = !sender.selected;
  92. NSString *hobby = self.dataSource[[self.tagViews indexOfObject:sender]];
  93. if (sender.selected) {
  94. sender.backgroundColor = sender.titleLabel.textColor;
  95. [self.selectTag addObject:hobby];
  96. } else {
  97. sender.backgroundColor = [UIColor whiteColor];
  98. [self.selectTag removeObject:hobby];
  99. }
  100. }
  101. }
  102. - (void)handleSave {
  103. if (self.selectTag.count == 0) {
  104. [MBProgressHUD showOnlyText:@"最少选择一个"];
  105. return;
  106. }
  107. [YanCNetWorkManager requestPostWithURLStr:Url_updateModelCard(PublicUrl)
  108. parameters:@{@"modelpk": [ModelUser user].modelpk, @"hobby": [self.selectTag componentsJoinedByString:@" "]}
  109. finish:^(id dataDic) {
  110. if ([dataDic[@"msg"] isEqualToString:@"success"]) {
  111. [self.navigationController popViewControllerAnimated:YES];
  112. }
  113. }
  114. enError:^(NSError *error) {
  115. }];
  116. }
  117. - (NSMutableArray *)dataSource {
  118. if (!_dataSource) {
  119. _dataSource = [NSMutableArray array];
  120. }
  121. return _dataSource;
  122. }
  123. - (NSMutableArray *)selectTag {
  124. if (!_selectTag) {
  125. _selectTag = [NSMutableArray array];
  126. }
  127. return _selectTag;
  128. }
  129. - (void)didReceiveMemoryWarning {
  130. [super didReceiveMemoryWarning];
  131. // Dispose of any resources that can be recreated.
  132. }
  133. /*
  134. Text自适应
  135. */
  136. - (CGFloat)calculateSizeWithFont:(NSInteger)Font Text:(NSString *)Text {
  137. NSDictionary *attr = @{NSFontAttributeName: [UIFont systemFontOfSize:Font]};
  138. CGRect rect = [Text boundingRectWithSize:CGSizeMake(MAXFLOAT, 20)
  139. options:NSStringDrawingUsesLineFragmentOrigin
  140. attributes:attr
  141. context:nil];
  142. return rect.size.width;
  143. }
  144. - (NSArray *)randomColors {
  145. return @[RGBValueColor(0xfc7d77, 1), RGBValueColor(0x25e6b9, 1), RGBValueColor(0xd2adfb, 1), RGBValueColor(0xff9dbc, 1), RGBValueColor(0xff6b9e, 1), RGBValueColor(0x5cdee0, 1), RGBValueColor(0xf29a76, 1), RGBValueColor(0x49c9f0, 1), RGBValueColor(0xef87b4, 1), RGBValueColor(0xb3d465, 1), RGBValueColor(0xf8b551, 1), RGBValueColor(0xf80c269, 1)];
  146. }
  147. /*
  148. #pragma mark - Navigation
  149. // In a storyboard-based application, you will often want to do a little preparation before navigation
  150. - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
  151. // Get the new view controller using [segue destinationViewController].
  152. // Pass the selected object to the new view controller.
  153. }
  154. */
  155. @end