PostTopicVC.m 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445
  1. #import "PostTopicVC.h"
  2. #import "UzysAssetsPickerController.h"
  3. #import "LDPostVCbtnView.h"
  4. #define defTitle @"选择分组"
  5. @interface PostTopicVC ()<UzysAssetsPickerControllerDelegate>
  6. {
  7. /**输入框
  8. */
  9. UITextView* textVi;
  10. /**标签按钮
  11. */
  12. UIButton* tagBtn;
  13. UIButton* currentBtn;
  14. /**多个title的父视图
  15. */
  16. UIView* contentV;
  17. /**从照片库挑选的照片
  18. */
  19. UIImage* pickedImg;
  20. /**从照片库挑选的所有照片
  21. */
  22. NSMutableArray *imageArray;
  23. /**存放之前的imageView
  24. */
  25. NSMutableArray *imageViews;
  26. /**添加图片按钮。
  27. */
  28. UIButton* addImgBtn;
  29. }
  30. @property(nonatomic,strong)NSMutableArray* titles;
  31. @end
  32. @implementation PostTopicVC
  33. - (void)viewDidLoad {
  34. [super viewDidLoad];
  35. [self myInit];
  36. }
  37. - (void)didReceiveMemoryWarning {
  38. [super didReceiveMemoryWarning];
  39. }
  40. -(void)viewDidAppear:(BOOL)animated
  41. {
  42. [super viewDidAppear:animated];
  43. }
  44. #pragma mark -
  45. -(void)myInit
  46. {
  47. [self configNavigationBar];
  48. [self setTitle:@"编辑"];
  49. [self.view setBackgroundColor:[UIColor whiteColor]];
  50. self.navigationController.navigationBar.translucent = NO;
  51. imageArray = [NSMutableArray array];
  52. imageViews = [NSMutableArray array];
  53. //最好是。它们的tag值不要变。
  54. NSArray* arr;
  55. arr = @[@"吐槽",@"晒图",@"找驾校",@"科目一",@"科目二",@"科目三",@"科目四",@"下证",@"许愿"];
  56. if (_groupId.intValue == -1)
  57. {
  58. if (_jxjc.length == 0 && defUser.userSchoolName)
  59. {
  60. _jxjc = defUser.userSchoolName;
  61. arr = @[_jxjc,@"吐槽",@"晒图",@"找驾校",@"科目一",@"科目二",@"科目三",@"科目四",@"下证",@"许愿"];
  62. }
  63. }
  64. _titles = [NSMutableArray arrayWithArray:arr];
  65. CGFloat x,y,w,h,bd;
  66. NSInteger tag = 0;
  67. UIButton* btn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 60, 30)];
  68. [btn setBackgroundColor:defGreen];
  69. [btn setTitle:@"发布" forState:UIControlStateNormal];
  70. UIBarButtonItem* bbi = [[UIBarButtonItem alloc] initWithCustomView:btn];
  71. self.navigationItem.rightBarButtonItem = bbi;
  72. [btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
  73. [btn setTag:tag++];
  74. bd = 10;
  75. x = 0;
  76. y = 0;
  77. w = kSize.width - bd;
  78. h = 100;
  79. UITextView* tv = [[UITextView alloc] initWithFrame:CGRectMake(x, y, w, h)];
  80. [self.view addSubview:tv];
  81. textVi = tv;
  82. [tv setFont:[UIFont scaleSize:NormalFont]];
  83. arr = @[@"",@"心情像朝日的阳光,连学生都 变成了开心的事",@"振作心情! 驾照就在不远处",
  84. @"普普通通的一天,普普通通的心情",@"道理我都懂,可是为什么考驾 照这么烦。",
  85. @"不懂啊,完全不懂啊",@"啊!为什么我要学车!!",@"",@"",@""];
  86. if (3 == _type.intValue) {
  87. [tv setText:arr[_mood]];
  88. }
  89. x = bd;
  90. y += h+bd;
  91. w = h = (kSize.width - 5*bd)/4;
  92. btn = [[UIButton alloc] initWithFrame:CGRectMake(x, y, w, h)];
  93. [btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
  94. [btn borderColor:KlineColor width:1 cornorRadios:3];
  95. [self.view addSubview:btn];
  96. [btn setTag:tag++];
  97. addImgBtn = btn;
  98. NSArray *images;
  99. images = @[@"",@"mood0.png",@"mood1.png",@"mood2.png",@"mood3.png",@"mood4.png",@"mood5.png"];
  100. if (3 == _type.intValue) {
  101. pickedImg = [UIImage imageNamed:images[_mood]];
  102. [btn setImage:pickedImg forState:UIControlStateNormal];
  103. }else{
  104. [btn setTitle:@"十" textColor:KlineColor font:35 fotState:UIControlStateNormal];
  105. }
  106. y+= h + 25;
  107. h = 30;
  108. w = [defTitle sizeForFont:13].width + 20;
  109. btn = [[UIButton alloc] initWithFrame:CGRectMake(x, y, w, h)];
  110. [btn setTitle:defTitle textColor:contentTextColor font:13 fotState:UIControlStateNormal];
  111. [btn borderColor:KlineColor width:1 cornorRadios:h/2.0];
  112. [btn setTitleColor:defGreen forState:UIControlStateSelected];
  113. [self.view addSubview:btn];
  114. [btn setTag:tag++];
  115. tagBtn = btn;
  116. arr = @[@"holder",defTitle,@"学车记录",@"学车直播",@"学车问答"];
  117. //如果groupId为0就会target。否则。固定。
  118. if ( 1 == _type.intValue)
  119. {
  120. if(!_groupId || [_groupId isEqualToString:@"0"])
  121. {
  122. [btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
  123. }
  124. else
  125. {
  126. //如果是驾校 就传个-1过去
  127. if ([_groupId isEqualToString:@"-1"]) {
  128. [btn setTitleNormal:_titles[0]];
  129. }else{
  130. [btn setTitleNormal:_titles[_groupId.intValue-1]];
  131. }
  132. btn.width = [arr[_type.intValue] sizeForFont:13].width + 20;
  133. [btn setSelected:YES];
  134. [btn borderColor:defGreen width:1 cornorRadios:h/2.0];
  135. }
  136. }
  137. else
  138. {
  139. [btn setTitleNormal:arr[_type.intValue]];
  140. btn.width = [arr[_type.intValue] sizeForFont:13].width + 20;
  141. [btn setSelected:YES];
  142. [btn borderColor:defGreen width:1 cornorRadios:h/2.0];
  143. }
  144. //tag 3+
  145. x = 0;
  146. y+= h+ 30;
  147. w = kSize.width;
  148. h = kSize.height - kNavOffSet - y;
  149. UIView* vi = [[UIView alloc] initWithFrame:CGRectMake(x, y, w, h)];
  150. [vi setBackgroundColor:backGroundColor];
  151. [self.view addSubview:vi];
  152. contentV = vi;
  153. contentV.hidden = YES;
  154. h = 30;
  155. y = x = bd;
  156. for (int i= 0; i<_titles.count ; i++) {
  157. NSString* str = _titles[i];
  158. w = [str sizeForFont:13].width + 20;
  159. if (x + w + bd > kSize.width) {
  160. x = bd;
  161. y += bd + h;
  162. }
  163. btn = [[UIButton alloc] initWithFrame:CGRectMake(x, y, w, h)];
  164. [btn setTitle:str textColor:contentTextColor font:13 fotState:UIControlStateNormal];
  165. [btn setTitle:str textColor:defGreen font:13 fotState:UIControlStateSelected];
  166. [btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
  167. [btn borderColor:KlineColor width:1 cornorRadios:h/2.0];
  168. [contentV addSubview:btn];
  169. [btn setTag:tag++];
  170. x += w+bd;
  171. }
  172. }
  173. -(void)btnClick:(UIButton*)sender
  174. {
  175. [self.view endEditing:YES];
  176. switch (sender.tag) {
  177. case 0:
  178. //发布话题
  179. [self uploadTopicInfo];
  180. break;
  181. case 1:
  182. //选择图片
  183. [self upDatePhotos];
  184. break;
  185. case 2:
  186. //选择话题分组
  187. [contentV setHidden:NO];
  188. break;
  189. default:
  190. {
  191. CGFloat h = 30;
  192. NSString* str ;
  193. CGFloat w ;
  194. if (sender != currentBtn && currentBtn) {
  195. currentBtn.selected = NO;
  196. [currentBtn borderColor:KlineColor width:1 cornorRadios: h/2.0];
  197. }
  198. sender.selected = !sender.selected;
  199. tagBtn.selected = sender.selected;
  200. currentBtn = sender;
  201. if (sender.selected) {
  202. [sender borderColor:defGreen width:1 cornorRadios: h/2.0];
  203. [tagBtn borderColor:defGreen width:1 cornorRadios: h/2.0];
  204. [tagBtn setTitle: [sender titleForState:UIControlStateNormal] forState:UIControlStateNormal];
  205. str = [sender titleForState:UIControlStateNormal];
  206. w = [str sizeForFont:13].width + 20;
  207. }else{
  208. [sender borderColor:KlineColor width:1 cornorRadios: h/2.0];
  209. [tagBtn borderColor:KlineColor width:1 cornorRadios: h/2.0];
  210. str = defTitle;
  211. w = [str sizeForFont:13].width + 20;
  212. [tagBtn setTitle:defTitle forState:UIControlStateNormal];
  213. }
  214. tagBtn.width = w;
  215. }
  216. break;
  217. }
  218. }
  219. #pragma mark 调用系统相册
  220. -(void)upDatePhotos
  221. {
  222. [imageArray removeAllObjects];
  223. UzysAssetsPickerController *picker = [[UzysAssetsPickerController alloc] init];
  224. picker.delegate = self;
  225. picker.maximumNumberOfSelectionVideo = 0;
  226. picker.maximumNumberOfSelectionPhoto = 9;
  227. picker.modalPresentationStyle = UIModalPresentationFullScreen;
  228. [self presentViewController:picker animated:YES completion:nil];
  229. }
  230. - (void)UzysAssetsPickerController:(UzysAssetsPickerController *)picker didFinishPickingAssets:(NSArray *)assets
  231. {
  232. for (ALAsset *representation in assets)
  233. {
  234. UIImage *img = [UIImage imageWithCGImage:representation.defaultRepresentation.fullResolutionImage
  235. scale:representation.defaultRepresentation.scale
  236. orientation:(UIImageOrientation)representation.defaultRepresentation.orientation];
  237. if (img == nil) {
  238. continue;
  239. }
  240. [imageArray addObject:img];
  241. }
  242. [self setUI];
  243. }
  244. -(void)setUI
  245. {
  246. CGFloat marginX = 10;
  247. CGFloat width = addImgBtn.width;
  248. if (imageViews.count != 0)
  249. {
  250. addImgBtn.frame = CGRectMake(marginX, textVi.y + textVi.height + marginX, width, width);
  251. for (UIImageView *image in imageViews)
  252. {
  253. [image removeFromSuperview];
  254. }
  255. }
  256. CGFloat marginY = addImgBtn.y;
  257. for (int i = 0; i < imageArray.count + 1; i ++)
  258. {
  259. int row = i/4;
  260. int column = i%4;
  261. LDPostVCbtnView *image = [[LDPostVCbtnView alloc] initWithFrame:CGRectMake(marginX + column*(marginX + width), marginY + row*(marginX + width), width, width)];
  262. if (i == imageArray.count)
  263. {
  264. addImgBtn.frame = image.frame;
  265. break;
  266. }
  267. image.image = imageArray[i];
  268. image.userInteractionEnabled = YES;
  269. [self.view addSubview:image];
  270. [imageViews addObject:image];
  271. //btn位置及图片这么做之后 用户体验绝对爽的一比
  272. UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(width - 20, -20, 40, 40)];
  273. [btn setImage:[UIImage imageNamed:@"community6"] forState:UIControlStateNormal];
  274. [btn setImageEdgeInsets:UIEdgeInsetsMake(10, 10, 10, 10)];
  275. btn.tag = 50 + i;
  276. [btn addTarget:self action:@selector(deleteImage:) forControlEvents:UIControlEventTouchUpInside];
  277. [image addSubview:btn];
  278. }
  279. //改变UI
  280. CGRect rect;
  281. CGFloat newY = addImgBtn.y + addImgBtn.height + 25;
  282. rect = tagBtn.frame;
  283. rect.origin.y = newY;
  284. tagBtn.frame = rect;
  285. rect = contentV.frame;
  286. rect.origin.y = newY + tagBtn.height + 30;
  287. contentV.frame = rect;
  288. }
  289. -(void)deleteImage:(UIButton *)btn
  290. {
  291. [imageArray removeObjectAtIndex:(btn.tag - 50)];
  292. [self setUI];
  293. }
  294. -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
  295. {
  296. if (textVi.isFirstResponder) {
  297. [textVi resignFirstResponder];
  298. }
  299. [contentV setHidden:YES];
  300. }
  301. -(void)complete:(void (^)(void))comp
  302. {
  303. fresh = comp;
  304. }
  305. #pragma mark 发布话题
  306. /**没有驾校就是9个。有是10个。
  307. */
  308. -(void)uploadTopicInfo
  309. {
  310. if (textVi.text.length < 6) {
  311. ShowMsg(@"最少输入6个字");
  312. return;
  313. }
  314. //过滤字 500以内
  315. if (![Util connectedToNetWork]) {
  316. showMsgUnconnect();
  317. return;
  318. }
  319. if (imageArray.count == 0) {
  320. //无图片上传
  321. [self uploadTopicInfoWithImageURL:@""];
  322. }else{
  323. //有图片上传
  324. NSMutableArray *imgDataArray = [NSMutableArray arrayWithCapacity:imageArray.count];
  325. NSData *imgData;
  326. for (UIImage *updataImg in imageArray) {
  327. //这个比例似乎有问题 但只能先这样了 完善之图片清晰度--danson
  328. imgData = UIImageJPEGRepresentation(updataImg,0.5);
  329. //imgData = UIImageJPEGRepresentation([updataImg scaledToSize:CGSizeMake(kFrame.size.width, kFrame.size.height)],0.5);
  330. //NSData *data = UIImagePNGRepresentation([updataImg scaledToSize:CGSizeMake(230, 230)]);
  331. //NSString *string = [data base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength];
  332. [imgDataArray addObject:imgData];
  333. }
  334. NSString *method = @"topicImg";
  335. [MBProgressHUD showLoadToView:self.view];
  336. [jiaPeiManager requestAnythingWithURL:method array:imgDataArray data:imgData completion:^(NSDictionary *root) {
  337. [MBProgressHUD hideHUDForView:self.view];
  338. if (!root) {
  339. ShowMsg(@"上传失败");
  340. return;
  341. }
  342. if (![root[@"code"] isEqualToString:@"0"]) {
  343. ShowMsg(root[@"body"]);
  344. return;
  345. }
  346. [self uploadTopicInfoWithImageURL:root[@"body"]];
  347. }];
  348. }
  349. }
  350. -(void)uploadTopicInfoWithImageURL:(NSString *)imgUrl
  351. {
  352. if (!_type) {
  353. _type = @"1";
  354. }
  355. if(!_groupId){
  356. if (currentBtn && currentBtn.selected ) {
  357. _groupId = [NSString stringWithFormat:@"%d",(int)currentBtn.tag-2];
  358. }else{
  359. _groupId = @"1";
  360. }
  361. }
  362. NSMutableArray *arr=[NSMutableArray array];
  363. [arr addPro:@"user" Value:defUser.userDict[@"id"]];
  364. [arr addPro:@"content" Value:textVi.text];
  365. [arr addPro:@"pictures" Value:imgUrl];
  366. [arr addPro:@"groupId" Value:_groupId];
  367. [arr addPro:@"score" Value:@"80"];
  368. [arr addPro:@"type" Value:_type];
  369. [arr addPro:@"moodId" Value:[NSString stringWithFormat:@"%d",(int)_mood]];
  370. [arr addPro:@"childGroupId" Value:_childID];
  371. NSString* method = @"uploadTopicInfo";
  372. [MBProgressHUD showLoadToView:self.view];
  373. [jiaPeiManager requestAnythingWithURL:method array:arr data:nil completion:^(NSDictionary * dict) {
  374. [MBProgressHUD hideHUDForView:self.view];
  375. if (!dict) {
  376. ShowMsgFailed();
  377. return;
  378. }
  379. if ( [dict[@"code"] isEqualToString:@"1"]) {
  380. ShowMsg(dict[@"body"]);
  381. return;
  382. }
  383. ShowMsgSuc();
  384. if (fresh) {
  385. fresh();
  386. }
  387. [self.navigationController popViewControllerAnimated:YES];
  388. }];
  389. }
  390. @end