// // YOUPAILCSendImageDynamicVC.m // LiveChat // // Created by 张灿 on 2018/9/6. // Copyright © 2018年 caicai. All rights reserved. // #import "YOUPAILCSendImageDynamicVC.h" #import "TZImagePickerController.h" #import "YOUPAILCImageCollectionCell.h" #import "YOUPAILCImageDeleteView.h" #import "UIViewController+TFPresent.h" #import "LZAlertWindow.h" #import "YOUPAILPChatTipView.h" #import "YOUPAILZTextView.h" @interface YOUPAILCSendImageDynamicVC () @property (nonatomic, strong) NSMutableArray *youpaipcellAttributesArray; @property (nonatomic, assign) CGPoint youpaiplastPressPoint; @property(nonatomic,strong)UIScrollView* youpaipscrollView; @property (nonatomic,strong)UICollectionView *youpaipcollectionView; @property (nonatomic,strong)YOUPAILZTextView* youpaipliveView; @property(nonatomic,strong)YOUPAILCImageDeleteView* youpaipdeleteView; @property(nonatomic,strong)UIButton *youpaipsendImageDynamicBtn;//发布动态 @end @implementation YOUPAILCSendImageDynamicVC - (YOUPAILCImageDeleteView*)youpaipdeleteView{ if (!_youpaipdeleteView) { _youpaipdeleteView = [[YOUPAILCImageDeleteView alloc]initWithFrame:CGRectMake(0, KScreenHeight, KScreenWidth, 50)]; [self.view addSubview:_youpaipdeleteView]; } return _youpaipdeleteView; } - (NSMutableArray *)youpaipcellAttributesArray{ if (!_youpaipcellAttributesArray) { self.youpaipcellAttributesArray = [NSMutableArray arrayWithCapacity:0]; } return _youpaipcellAttributesArray; } - (void)viewWillAppear:(BOOL)animated{ [super viewWillAppear:animated]; // [self.navigationController.navigationBar setShadowImage:[UIImage imageWithColor:LCBkgColor size:CGSizeMake(KScreenWidth, 0.0)]]; // [self youpaifaddTip]; } - (void)youpaifaddTip{ NSString *tip = @"请勿涉及色情,广告等违规内容,违者将永久封号"; YOUPAILPChatTipView* chatTip = [[YOUPAILPChatTipView alloc]initWithFrame:CGRectMake(0,NavBarHeight-40, KScreenWidth, 40)]; chatTip.tipLabel.text = tip; [self.view addSubview:chatTip]; [UIView animateWithDuration:1 animations:^{ chatTip.frame = CGRectMake(0,NavBarHeight, KScreenWidth, 40); }completion:^(BOOL finished) { if (finished) { dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ [UIView animateWithDuration:1 animations:^{ chatTip.frame = CGRectMake(0,NavBarHeight-40, KScreenWidth, 40); }completion:^(BOOL finished) { if (finished) { [chatTip removeFromSuperview]; } }]; }); } }]; } - (void)viewWillDisappear:(BOOL)animated{ [super viewWillDisappear:animated]; [self.navigationController.navigationBar setShadowImage:nil]; } - (void)viewDidLoad { [super viewDidLoad]; self.title = @"发布动态"; self.sx_disableInteractivePop = YES; // self.view.backgroundColor = [UIColor whiteColor]; [self youpaifrightClick]; // self.navigationItem.leftBarButtonItem = [UIBarButtonItem itemWithTarget:self action:@selector(youpaifcancelClick) title:@"取消" font:LCFont15 titleColor:LCTextGray highlightedColor:LCTextGray titleEdgeInsets:UIEdgeInsetsZero]; // self.navigationItem.rightBarButtonItem = [UIBarButtonItem itemWithTarget:self action:@selector(youpaifsendClick) title:@"发布" font:LCFont15 titleColor:LCBlueColor highlightedColor:LCBlueColorHighit titleEdgeInsets:UIEdgeInsetsZero]; self.navigationItem.leftBarButtonItem = [UIBarButtonItem itemWithTarget:self action:@selector(youpaifcancelClick) image:[UIImage imageNamed:@"vqu_images_navigation_back_black"]]; UIButton *rightBgV = [UIButton buttonWithType:UIButtonTypeCustom]; rightBgV.frame = CGRectMake(0, 0, 44.0f, 44.0f); [rightBgV addTarget:self action:@selector(youpaifrightClick) forControlEvents:UIControlEventTouchUpInside]; UIImageView *rightImgV = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"vqu_images_D_senddynamic_right"]]; rightImgV.frame = CGRectMake(44.0f - 16.0f, (44.0f - 16.0f) / 2.0f, 16.0f, 16.0f); [rightBgV addSubview:rightImgV]; UIBarButtonItem *rightBarItem = [[UIBarButtonItem alloc] initWithCustomView:rightBgV]; self.navigationItem.rightBarButtonItem = rightBarItem;//[UIBarButtonItem itemWithTarget:self action:@selector(youpaifrightClick) image:[UIImage imageNamed:@"ic_senddynamic_right"]]; UIButton* btn = (UIButton*)self.navigationItem.rightBarButtonItem.customView; btn.uxy_acceptEventInterval = 3.0; [self youpaifsetupView]; self.youpaipliveView.youpaiptextView.text = self.youpaipsendModel.youpaiptit; [self.youpaipcollectionView reloadData]; [self youpaifupdateFrame]; // self.liveView.textView.delegate = self; // UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(youpaifhideTheKeyboard)]; // [self.navigationController.navigationBar addGestureRecognizer:tap]; } - (void)youpaifsetupView{ UIScrollView* scrollV = [[UIScrollView alloc]initWithFrame:CGRectMake(0, NavBarHeight, KScreenWidth, KScreenHeight-NavBarHeight)]; scrollV.backgroundColor = [UIColor whiteColor]; scrollV.delegate = self; scrollV.contentSize = CGSizeMake(KScreenWidth, KScreenHeight-NavBarHeight+1); self.youpaipscrollView = scrollV; [self.view addSubview:scrollV]; YOUPAILZTextView *textView = [[YOUPAILZTextView alloc] initWithMaxCount:140]; textView.frame = CGRectMake(14.0f, 25.0f, KScreenWidth-28, 148.0f); textView.youpaiptextViewPadding = UIEdgeInsetsMake(14.0f, 14.0f, 10.0f, 14.0f); textView.youpaiptextLengthDefaultColor = LZD3D1D7Color; textView.youpaiptextLengthHigtColor = LZ7C69FEColor; textView.youpaiptextLengthFont = LCFont(14.0f); textView.youpaiptextLengthRight = 14.0f; textView.youpaiptextLengthBottom = 14.0f; textView.youpaiptextView.placeholderFont = LCFont(14.0f); textView.youpaiptextView.placeholderTextColor = LZD3D1D7Color; textView.youpaiptextView.placeholderText = @"这一刻你想说点什么…"; textView.youpaiptextView.font = LCFont(14.0f); textView.layer.cornerRadius = 5.0f; textView.clipsToBounds = YES; textView.tintColor = LZ7C69FEColor; [self.youpaipscrollView addSubview:textView]; textView.youpaiptextView.textColor = LZ273145Color; self.youpaipliveView = textView; textView.backgroundColor = LZF7F8FAColor; UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init]; CGFloat margin = 5; CGFloat itemWH = (KScreenWidth-40)/3.0; layout.scrollDirection = UICollectionViewScrollDirectionVertical; layout.itemSize = CGSizeMake(itemWH, itemWH); layout.minimumInteritemSpacing = margin; layout.minimumLineSpacing = margin; _youpaipcollectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(14, CGRectGetMaxY(textView.frame) + 14.0f, KScreenWidth-28, itemWH+5) collectionViewLayout:layout]; _youpaipcollectionView.backgroundColor = [UIColor clearColor]; _youpaipcollectionView.contentInset = UIEdgeInsetsMake(0, 0, 0, 0); _youpaipcollectionView.layer.masksToBounds = NO; _youpaipcollectionView.dataSource = self; _youpaipcollectionView.delegate = self; _youpaipcollectionView.scrollEnabled = NO; [_youpaipcollectionView registerClass:[YOUPAILCImageCollectionCell class] forCellWithReuseIdentifier:@"YOUPAILCImageCollectionCell"]; [scrollV addSubview:_youpaipcollectionView]; self.youpaipsendImageDynamicBtn = [UIButton buttonWithType:UIButtonTypeCustom]; self.youpaipsendImageDynamicBtn.frame = CGRectMake(40,KScreenHeight-NavBarHeight-48-SafeHeight-45,KScreenWidth - 80,48); [self.youpaipsendImageDynamicBtn setTitle:@"发布动态" forState:0]; [self.youpaipsendImageDynamicBtn setTitleColor:HexColorFromRGB(0x000000) forState:0]; self.youpaipsendImageDynamicBtn.titleLabel.font = LCFont(17); self.youpaipsendImageDynamicBtn.layer.cornerRadius = 24; self.youpaipsendImageDynamicBtn.layer.masksToBounds = YES; [self.youpaipsendImageDynamicBtn addTarget:self action:@selector(youpaifsendClick) forControlEvents:UIControlEventTouchUpInside]; [self.youpaipsendImageDynamicBtn setBackgroundImage:[LCTools ColorImage:self.youpaipsendImageDynamicBtn.frame.size FromColors:@[LZBFB6FFColor,LZ7C69FEColor] ByGradientType:(GradientLeftToRight)] forState:(UIControlStateNormal)]; [scrollV addSubview:self.youpaipsendImageDynamicBtn]; } #pragma mark UICollectionView - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section { if (self.youpaipsendModel.youpaipimages.count==9) { return self.youpaipsendModel.youpaipimages.count; }else{ return self.youpaipsendModel.youpaipimages.count+1; } } - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath { YOUPAILCImageCollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"YOUPAILCImageCollectionCell" forIndexPath:indexPath]; cell.backgroundColor = [UIColor clearColor]; cell.layer.cornerRadius = 4.0f; cell.clipsToBounds = YES; cell.youpaipindexPath = indexPath; WeakSelf; [cell setClickRemoveBtnBlock:^(NSIndexPath *indexPath) { if (weakSelf.youpaipsendModel.youpaipimages.count>indexPath.item) { [weakSelf.youpaipsendModel.youpaipimages removeObjectAtIndex:indexPath.item]; } [weakSelf.youpaipcollectionView reloadData]; }]; if (self.youpaipsendModel.youpaipimages.count==9) { UIImage* image = self.youpaipsendModel.youpaipimages[indexPath.item]; cell.youpaipimageView.image = image ; UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(youpaiflongPressGesture:)]; [cell addGestureRecognizer:longPress]; cell.youpaipremoveBtn.hidden = NO; return cell; }else{ if (indexPath.row==self.youpaipsendModel.youpaipimages.count) { cell.youpaipimageView.image = [UIImage imageNamed:@"vqu_images_D_dynamic_add"]; cell.youpaipremoveBtn.hidden = YES; }else{ cell.youpaipremoveBtn.hidden = NO; UIImage* image = self.youpaipsendModel.youpaipimages[indexPath.item]; cell.youpaipimageView.image = image ; UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(youpaiflongPressGesture:)]; [cell addGestureRecognizer:longPress]; return cell; } } return cell; } - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath { if (self.youpaipsendModel.youpaipimages.count==9) { [self youpaifshowBigWithIndex:indexPath.item]; }else{ if (indexPath.row==self.youpaipsendModel.youpaipimages.count) { [self youpaifaddPhotos]; }else{ [self youpaifshowBigWithIndex:indexPath.item]; } } } - (void)youpaifshowBigWithIndex:(NSInteger)index{ NSMutableArray *tempArr = [NSMutableArray array]; for (int i=0; iself.youpaiplastPressPoint.y) { [UIView animateWithDuration:0.25 animations:^{ self.youpaipdeleteView.frame = CGRectMake(0, KScreenHeight-50, KScreenWidth, 50); }]; } for (UICollectionViewLayoutAttributes *attributes in self.youpaipcellAttributesArray) { if (CGRectContainsPoint(attributes.frame, cell.center) && cellIndexPath != attributes.indexPath ) { isChanged = YES; //对数组中存放的元素重新排序 UIImage *tmpImage = self.youpaipsendModel.youpaipimages[cellIndexPath.row]; [self.youpaipsendModel.youpaipimages removeObjectAtIndex:cellIndexPath.row]; [self.youpaipsendModel.youpaipimages insertObject:tmpImage atIndex:attributes.indexPath.row]; [self.self.youpaipcollectionView moveItemAtIndexPath:cellIndexPath toIndexPath:attributes.indexPath]; } } CGPoint viewPoint = [sender locationInView:self.view]; if (viewPoint.y>=KScreenHeight-50) { self.youpaipdeleteView.youpaipstatu = 2; }else{ self.youpaipdeleteView.youpaipstatu = 1; } }else if (sender.state == UIGestureRecognizerStateEnded) { if (!isChanged) { cell.center = [self.youpaipcollectionView layoutAttributesForItemAtIndexPath:cellIndexPath].center; } cell.transform = CGAffineTransformIdentity; if (self.youpaipdeleteView.youpaipstatu==2) { if (self.youpaipsendModel.youpaipimages.count>cell.youpaipitem) { [self.youpaipsendModel.youpaipimages removeObjectAtIndex:cell.youpaipitem]; } [self.youpaipcollectionView reloadData]; } [UIView animateWithDuration:0.25 animations:^{ self.youpaipdeleteView.frame = CGRectMake(0, KScreenHeight, KScreenWidth, 50); } completion:nil]; } } - (void)youpaifaddPhotos{ TZImagePickerController *imagePickerVc = [[TZImagePickerController alloc] initWithMaxImagesCount:9-self.youpaipsendModel.youpaipimages.count columnNumber:4 delegate:nil]; [LCTZImageConfigHelper setDefaultTZImageConfig:imagePickerVc]; imagePickerVc.allowTakePicture = YES; // 在内部显示拍照按钮 imagePickerVc.allowPickingVideo = NO;//是否允许选择视频 imagePickerVc.allowPickingImage = YES;//是否允许选择照片 imagePickerVc.allowPickingOriginalPhoto = YES;//是否选择原图 imagePickerVc.showSelectedIndex = YES; //显示图片序号 // 4. 照片排列按修改时间升序 imagePickerVc.sortAscendingByModificationDate = YES;//按时间倒序排列图片 // 你可以通过block或者代理,来得到用户选择的照片. [imagePickerVc setDidFinishPickingPhotosHandle:^(NSArray *photos, NSArray *assets, BOOL isSelectOriginalPhoto) { for (int i =0 ; iKScreenHeight-NavBarHeight) { self.youpaipscrollView.contentSize = CGSizeMake(KScreenWidth, CGRectGetMaxY(self.youpaipcollectionView.frame)); }else{ self.youpaipscrollView.contentSize = CGSizeMake(KScreenWidth, KScreenHeight-NavBarHeight+1); } } - (void)youpaifcancelClick{ self.youpaipsendModel.youpaiptit = self.youpaipliveView.youpaiptextView.text; UIAlertController *alertController = [UIAlertController alertControllerWithTitle:nil message:@"要保存草稿吗?" preferredStyle:UIAlertControllerStyleAlert]; UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"放弃" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action){ [LCSaveModel saveSendDynamicModel:nil]; [self popEffectDismiss]; }]; UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"保存" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action){ [LCSaveModel saveSendDynamicModel:self.youpaipsendModel]; [self popEffectDismiss]; }]; [alertController addAction:cancelAction]; [alertController addAction:okAction]; [self presentViewController:alertController animated:YES completion:nil]; } //提示 -(void)youpaifrightClick { LZAlertAction *action = [LZAlertAction actionWithTitle:@"我知道了" handler:^(LZAlertAction *action) { }]; action.bgColor = [UIColor colorWithPatternImage:[LCTools ColorImage:CGSizeMake(KScreenWidth - 128.0f, 48.0f) FromColors:@[LZBFB6FFColor,LZ7C69FEColor] ByGradientType:GradientLeftToRight]]; LZAlertWindow *alert = [LZAlertWindow alertWithTitle:@"发布须知" content:@"1、禁止发布色情,性暗示等低俗内容;\n\n2、禁止发布国家政治,暴恐暴乱等内容;\n\n3、不可涉及第三方平台信息;\n\n4、不可使用他人的图片或视频发布。" action:@[action]]; alert.contentFont = LCFont16; alert.contentHorizontalSpace = 32.0f; [self TFPresentVC:alert completion:^{}]; } - (void)youpaifsendClick{ self.youpaipsendModel.youpaiptit = self.youpaipliveView.youpaiptextView.text; if (self.youpaipsendModel.youpaiptit.length>0 && self.youpaipsendModel.youpaipimages.count>0) { [LCHttpHelper requestWithURLString:DynamicPub parameters:@{@"content":self.youpaipsendModel.youpaiptit} needToken:YES type:(HttpRequestTypePost) success:^(id responseObject) { NSDictionary* dict = (NSDictionary*)responseObject; NSInteger code = [[dict objectForKey:@"code"] integerValue]; if (code==0) { NSString* dynamicId = [[dict objectForKey:@"data"]objectForKey:@"dynamic_id"]; [ZCHUDHelper show]; [LCCommonHttp uploadWithImages:self.youpaipsendModel.youpaipimages Type:@"dynamic" successBlock:^(NSArray *ossImagePaths) { NSMutableString *string = [NSMutableString string]; for (NSString *itemPath in ossImagePaths) { [string appendString:itemPath]; [string appendString:@","]; } NSRange range = NSMakeRange(string.length - 1, 1); [string deleteCharactersInRange:range]; [LCHttpHelper requestWithURLString:DynamicUpdateImage parameters:@{@"type":@"0", @"images":string ,@"dynamic_id":dynamicId} needToken:YES type:(HttpRequestTypePost) success:^(id responseObject) { NSDictionary* dict = (NSDictionary*)responseObject; NSInteger code = [[dict objectForKey:@"code"] integerValue]; if (code==0) { [LCSaveModel saveSendDynamicModel:nil]; } [ZCHUDHelper dismiss]; [self popEffectDismiss]; [ZCHUDHelper showTitle:@"发布视频成功,请等待客服审核" showtime:5]; } failure:^(NSError *error) { [ZCHUDHelper dismiss]; [self popEffectDismiss]; }]; }]; } } failure:^(NSError *error) { }]; }else{ [ZCHUDHelper showTitle:@"照片和文字不能为空"]; } } -(void)scrollViewDidScroll:(UIScrollView *)scrollView{ [self youpaifhideTheKeyboard]; } //隐藏键盘 -(void)youpaifhideTheKeyboard { dispatch_async(dispatch_get_main_queue(), ^{ [self.youpaipliveView.youpaiptextView endEditing:YES]; }); } @end