| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188 |
- //
- // HobbyViewController.m
- // model
- //
- // Created by Drew on 2018/11/6.
- // Copyright © 2018 Mine. All rights reserved.
- //
- #import "HobbyViewController.h"
- #import "TagCollectionViewCell.h"
- #import <TTGTagCollectionView/TTGTagCollectionView.h>
- @interface HobbyViewController () <TTGTagCollectionViewDelegate, TTGTagCollectionViewDataSource>
- @property(nonatomic, strong) UILabel *infoLabel;
- @property(nonatomic, strong) TTGTagCollectionView *listTagView;
- @property(nonatomic, strong) NSMutableArray *dataSource;
- @property(strong, nonatomic) NSMutableArray <UIView *> *tagViews;
- @property(nonatomic, strong) NSMutableArray *selectTag;
- @end
- @implementation HobbyViewController
- - (void)viewDidLoad {
- [super viewDidLoad];
- [self configUI];
- }
- - (void)configUI {
- self.view.backgroundColor = RGBValueColor(0xf7f7f7, 1.0);
- self.title = @"兴趣爱好";
- self.navigationController.navigationBar.tintColor = RGBValueColor(0x333333, 1);
- self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"保存" style:UIBarButtonItemStylePlain target:self action:@selector(handleSave)];
- self.infoLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, 20, ScreenWidth - 40, 20)];
- self.infoLabel.font = [UIFont systemFontOfSize:14];
- self.infoLabel.text = @"选择你的兴趣爱好(至多选6个)";
- self.infoLabel.textColor = RGBValueColor(0x666666, 1);
- [self.view addSubview:self.infoLabel];
- self.listTagView = [[TTGTagCollectionView alloc] initWithFrame:CGRectMake(20, 60, ScreenWidth - 40, ScreenHeight - 60)];
- self.listTagView.delegate = self;
- self.listTagView.dataSource = self;
- self.listTagView.verticalSpacing = 10;
- self.listTagView.horizontalSpacing = 15;
- [self.view addSubview:self.listTagView];
- _tagViews = [NSMutableArray new];
- NSArray *hobbyArray = @[@"网球", @"高尔夫球", @"桌球", @"滑板", @"跑步", @"瑜伽", @"游泳", @"健身", @"篮球", @"足球",
- @"机车", @"看电影", @"话剧", @"逛街", @"葡萄酒", @"美食", @"蹦迪", @"唱歌", @"跳舞", @"宅女"];
- [self.dataSource addObjectsFromArray:hobbyArray];
- for (NSString *hobby in hobbyArray) {
- [_tagViews addObject:[self newButtonWithTitle:hobby]];
- }
- [self.listTagView reload];
- }
- #pragma mark - TTGTagCollectionViewDelegate
- - (CGSize)tagCollectionView:(TTGTagCollectionView *)tagCollectionView sizeForTagAtIndex:(NSUInteger)index {
- return _tagViews[index].frame.size;
- }
- - (void)tagCollectionView:(TTGTagCollectionView *)tagCollectionView didSelectTag:(UIView *)tagView atIndex:(NSUInteger)index {
- }
- - (BOOL)tagCollectionView:(TTGTagCollectionView *)tagCollectionView shouldSelectTag:(UIView *)tagView atIndex:(NSUInteger)index {
- return YES;
- }
- #pragma mark - TTGTagCollectionViewDataSource
- - (NSUInteger)numberOfTagsInTagCollectionView:(TTGTagCollectionView *)tagCollectionView {
- return _tagViews.count;
- }
- - (UIView *)tagCollectionView:(TTGTagCollectionView *)tagCollectionView tagViewForIndex:(NSUInteger)index {
- return _tagViews[index];
- }
- #pragma mark - Private methods
- - (UIButton *)newButtonWithTitle:(NSString *)title {
- UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
- UIColor *selectColor = [self randomColors][arc4random() % 12];
- button.titleLabel.font = [UIFont systemFontOfSize:14];
- [button setTitle:title forState:UIControlStateNormal];
- [button sizeToFit];
- button.layer.masksToBounds = YES;
- button.layer.cornerRadius = 15.0f;
- [button setTitleColor:selectColor forState:UIControlStateNormal];
- [button setTitleColor:[UIColor whiteColor] forState:UIControlStateSelected];
- button.layer.borderWidth = 1.f;
- button.layer.borderColor = selectColor.CGColor;
- [self expandSizeForView:button extraWidth:15 extraHeight:4];
- [button addTarget:self action:@selector(onTap:) forControlEvents:UIControlEventTouchUpInside];
- return button;
- }
- - (void)expandSizeForView:(UIView *)view extraWidth:(CGFloat)extraWidth extraHeight:(CGFloat)extraHeight {
- CGRect frame = view.frame;
- frame.size.width += extraWidth;
- frame.size.height += extraHeight;
- view.frame = frame;
- }
- - (void)onTap:(UIButton *)sender {
- if (!sender.selected && self.selectTag.count == 6) {
- [MBProgressHUD showOnlyText:@"最多选择6个"];
- } else {
- sender.selected = !sender.selected;
- NSString *hobby = self.dataSource[[self.tagViews indexOfObject:sender]];
- if (sender.selected) {
- sender.backgroundColor = sender.titleLabel.textColor;
- [self.selectTag addObject:hobby];
- } else {
- sender.backgroundColor = [UIColor whiteColor];
- [self.selectTag removeObject:hobby];
- }
- }
- }
- - (void)handleSave {
- if (self.selectTag.count == 0) {
- [MBProgressHUD showOnlyText:@"最少选择一个"];
- return;
- }
- [YanCNetWorkManager requestPostWithURLStr:Url_updateModelCard(PublicUrl)
- parameters:@{@"modelpk": [ModelUser user].modelpk, @"hobby": [self.selectTag componentsJoinedByString:@" "]}
- finish:^(id dataDic) {
- if ([dataDic[@"msg"] isEqualToString:@"success"]) {
- [self.navigationController popViewControllerAnimated:YES];
- }
- }
- enError:^(NSError *error) {
- }];
- }
- - (NSMutableArray *)dataSource {
- if (!_dataSource) {
- _dataSource = [NSMutableArray array];
- }
- return _dataSource;
- }
- - (NSMutableArray *)selectTag {
- if (!_selectTag) {
- _selectTag = [NSMutableArray array];
- }
- return _selectTag;
- }
- - (void)didReceiveMemoryWarning {
- [super didReceiveMemoryWarning];
- // Dispose of any resources that can be recreated.
- }
- /*
- Text自适应
- */
- - (CGFloat)calculateSizeWithFont:(NSInteger)Font Text:(NSString *)Text {
- NSDictionary *attr = @{NSFontAttributeName: [UIFont systemFontOfSize:Font]};
- CGRect rect = [Text boundingRectWithSize:CGSizeMake(MAXFLOAT, 20)
- options:NSStringDrawingUsesLineFragmentOrigin
- attributes:attr
- context:nil];
- return rect.size.width;
- }
- - (NSArray *)randomColors {
- 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)];
- }
- /*
- #pragma mark - Navigation
- // In a storyboard-based application, you will often want to do a little preparation before navigation
- - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
- // Get the new view controller using [segue destinationViewController].
- // Pass the selected object to the new view controller.
- }
- */
- @end
|