// // LogAddViewController.m // IBOSSmini // // Created by guan hong hou on 2017/5/9. // Copyright © 2017年 elongtian. All rights reserved. // #define logTextFont [UIFont systemFontOfSize:14] #import "LogAddViewController.h" #import "DateFormat.h" #import "LogShareVC.h" #import "LogCommentVC.h" #import "LogNewCollectionViewCell.h" #import "MapViewController.h" #import #import "DKAudioPlayerHelper.h" #import "LeslieAsyncImageDownloader.h" @interface LogAddViewController () @end @implementation LogAddViewController{ UITabBar* bar; UIView * contentView; UIView *vPositionView; UIScrollView *scroll; } @synthesize shareArr=_shareArr; @synthesize commentArr=_commentArr; #pragma mark - 公共函数 /** viewDidLoad函数 */ - (void)viewDidLoad { [super viewDidLoad]; self.navigationItem.title=@"新增日志"; UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom]; [button setBackgroundImage:[UIImage imageNamed:@"icon_back.png"] forState:UIControlStateNormal]; [button addTarget:self action:@selector(goBack) forControlEvents:UIControlEventTouchUpInside]; button.frame = CGRectMake(0, 0, 15, 18); UIBarButtonItem *menuButton = [[UIBarButtonItem alloc] initWithCustomView:button]; self.navigationItem.leftBarButtonItem = menuButton; [self initUI]; _allowsSendVoice = YES; self.delegate = self; self.dataSource = self; _sDate= [DateFormat getCurrentDateTime]; [_btnDate setTitle:[DateFormat dateFormatSplit:_sDate]forState:UIControlStateNormal]; } /** 安全区视图发生变化 */ -(void)viewSafeAreaInsetsDidChange{ scroll.frame = CGRectMake(0, 0, SCREENWIDTH, self.view.safeAreaLayoutGuide.layoutFrame.size.height- 45.0); CGRect inputFrame = CGRectMake(0.0f, self.view.safeAreaLayoutGuide.layoutFrame.size.height - 45.0, self.view.safeAreaLayoutGuide.layoutFrame.size.width, 45.0); _inputView.frame = inputFrame; [super viewSafeAreaInsetsDidChange]; } //获得DKVoiceRecordHUD - (DKVoiceRecordHUD *)voiceRecordHUD { if (!_voiceRecordHUD) { _voiceRecordHUD = [[DKVoiceRecordHUD alloc] initWithFrame:CGRectMake(0, 0, 140, 140)]; } return _voiceRecordHUD; } //获得DKVoiceRecordHelper - (DKVoiceRecordHelper *)voiceRecordHelper { if (!_voiceRecordHelper) { _isMaxTimeStop = NO; WEAKSELF _voiceRecordHelper = [[DKVoiceRecordHelper alloc] init]; _voiceRecordHelper.maxTimeStopRecorderCompletion = ^{ UIButton *holdDown = weakSelf.inputView.holdDownButton; holdDown.selected = NO; holdDown.highlighted = NO; weakSelf.isMaxTimeStop = YES; [weakSelf finishRecorded]; }; _voiceRecordHelper.peakPowerForChannel = ^(float peakPowerForChannel) { weakSelf.voiceRecordHUD.peakPower = peakPowerForChannel; }; _voiceRecordHelper.maxRecordTime = kVoiceRecorderTotalTime; } return _voiceRecordHelper; } //手机屏幕方向方法 #if __IPHONE_OS_VERSION_MAX_ALLOWED < __IPHONE_9_0 - (NSUInteger)supportedInterfaceOrientations #else - (UIInterfaceOrientationMask)supportedInterfaceOrientations #endif { return UIInterfaceOrientationMaskPortrait; } - (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation { return UIInterfaceOrientationPortrait; } //相册数组属性 - (NSMutableArray *)assets{ if (!_assets) { _assets = [NSMutableArray array]; } return _assets; } //图片路径数组 - (NSMutableArray *)imagePaths{ if (!_imagePaths) { _imagePaths = [NSMutableArray array]; } return _imagePaths; } //声音路径数组 - (NSMutableArray *)voicePaths{ if (!_voicePaths) { _voicePaths = [NSMutableArray array]; } return _voicePaths; } //声音上传路径数组 - (NSMutableArray *)voiceUploadPaths{ if (!_voiceUploadPaths) { _voiceUploadPaths = [NSMutableArray array]; } return _voiceUploadPaths; } //声音上传时间数组 -(NSMutableArray *)voiceUploadTimes{ if (!_voiceUploadTimes) { _voiceUploadTimes = [NSMutableArray array]; } return _voiceUploadTimes; } //图片本地路径数组 -(NSMutableArray *)imageLocalPaths{ if (!_imageLocalPaths) { _imageLocalPaths = [NSMutableArray array]; } return _imageLocalPaths; } // 播放语音方法 - (void)multiMediaMessageDidSelectedOnMessage:(id)message atIndexPath:(NSIndexPath *)indexPath onMessageTableViewCell:(DKUITableViewCell *)messageTableViewCell { UIViewController *disPlayViewController; switch (message.messageMediaType) { case DKBubbleMessageMediaTypeVoice: { message.isRead = YES; messageTableViewCell.messageBubbleView.voiceUnreadDotImageView.hidden = YES; [[DKAudioPlayerHelper shareInstance] setDelegate:(id)self]; if (_currentSelectedCell) { [_currentSelectedCell.messageBubbleView.animationVoiceImageView stopAnimating]; } if (_currentSelectedCell == messageTableViewCell) { [messageTableViewCell.messageBubbleView.animationVoiceImageView stopAnimating]; [[DKAudioPlayerHelper shareInstance] stopAudio]; self.currentSelectedCell = nil; } else { self.currentSelectedCell = messageTableViewCell; [messageTableViewCell.messageBubbleView.animationVoiceImageView startAnimating]; [[DKAudioPlayerHelper shareInstance] managerAudioWithFileName:message.voicePath toPlay:YES]; } break; } default: break; } if (disPlayViewController) { [self.navigationController pushViewController:disPlayViewController animated:YES]; } } //停止播放语音方法 - (void)didAudioPlayerStopPlay:(AVAudioPlayer *)audioPlayer { if (!_currentSelectedCell) { return; } [_currentSelectedCell.messageBubbleView.animationVoiceImageView stopAnimating]; self.currentSelectedCell = nil; } /** * 发送语音消息的回调方法 * * @param voicePath 目标语音本地路径 * @param voiceDuration 目标语音时长 * @param sender 发送者的名字 * @param date 发送时间 */ - (void)didSendVoice:(NSString *)voicePath voiceDuration:(NSString *)voiceDuration fromSender:(NSString *)sender onDate:(NSDate *)date { int value=ceilf([voiceDuration floatValue]); NSString* duration=[NSString stringWithFormat:@"%d",value ] ; DKMessage *voiceMessage = [[DKMessage alloc] initWithVoicePath:voicePath voiceUrl:nil voiceDuration:duration sender:sender timestamp:date]; [self.voicePaths addObject:voicePath]; [self.voiceUploadTimes addObject:duration]; [self addMessage:voiceMessage]; CGFloat collectionheight=0; if (self.assets.count==0) { collectionheight=0; }else { collectionheight= [self getPhotoCollectionViewHeightWithPhotos:self.assets]; } _collectionView.frame=CGRectMake(10,CGRectGetMaxY(_txtContent.frame)+8, Screen_Width-20, collectionheight); _positionImg.frame=CGRectMake(15, CGRectGetMaxY(_collectionView.frame)+15,9,12); _lblPosition.frame=CGRectMake(CGRectGetMaxX(_positionImg.frame)+11,CGRectGetMaxY(_collectionView.frame)+10, 350, 20); _btnTakePhoto.frame=CGRectMake(15, CGRectGetMaxY(_positionImg.frame)+15,80,20); _btnPhoto.frame=CGRectMake((Screen_Width/3)+15, CGRectGetMaxY(_positionImg.frame)+15,80,20); _btnPosition.frame=CGRectMake((Screen_Width/3*2)+15, CGRectGetMaxY(_positionImg.frame)+15,80,20); _separatorView.frame=CGRectMake(15, CGRectGetMaxY(_btnTakePhoto.frame)+15,Screen_Width-15, 1); _voiceImg.frame=CGRectMake(17,CGRectGetMaxY(_separatorView.frame)+18, 13, 15); _lblVoice.frame=CGRectMake(CGRectGetMaxX(_voiceImg.frame)+13,CGRectGetMaxY(_separatorView.frame)+15,30, 20); _btnRecord.frame=CGRectMake(Screen_Width-90, CGRectGetMaxY(_separatorView.frame)+18,100,20); _messageTV.frame=CGRectMake(CGRectGetMaxX(_lblVoice.frame)+2, CGRectGetMaxY(_separatorView.frame)+18,190, [self getSoundViewHeight:self.voicePaths]); // _messageTV.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight; if(self.voicePaths.count>0){ contentView.frame=CGRectMake(0, 0, Screen_Width,CGRectGetMaxY(_messageTV.frame)); } else{ contentView.frame=CGRectMake(0, 0, Screen_Width,CGRectGetMaxY(_btnRecord.frame)); } scroll.contentSize=CGSizeMake(self.view.frame.size.width, contentView.frame.size.height+110); } // DKMessageTableViewController Delegate - (BOOL)shouldPreventScrollToBottomWhileUserScrolling { return YES; } //DKMessageTableViewController DataSource - (id )messageForRowAtIndexPath:(NSIndexPath *)indexPath { return self.messages[indexPath.row]; } // DKMessageInputView Delegate - (void)didChangeSendVoiceAction:(BOOL)changed { if (changed) { if (self.textViewInputViewType == DKInputViewTypeText) return; [self layoutOtherMenuViewHiden:YES]; } } // scrollView delegate开始拖拽方法 - (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView { self.isUserScrolling = YES; UIMenuController *menu = [UIMenuController sharedMenuController]; if (menu.isMenuVisible) { [menu setMenuVisible:NO animated:YES]; } if (self.textViewInputViewType != DKInputViewTypeNormal) { // [self layoutOtherMenuViewHiden:YES]; } } /* scrollView delegate结束拖拽方法 */ - (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate { self.isUserScrolling = NO; } //点击位置后回调函数 -(void)relocationData:(NSString *)name{ self.sLocation=name; if (self.sLocation.length>0){ _lblPosition.text=self.sLocation; } } // 保存完成后更新画面 - (void)onLoadFinish:(ASIDownManager *)sender { NSDictionary *dic = [sender.mWebStr JSONValue]; [self cancel]; if (dic && [dic isKindOfClass:[NSDictionary class]]) { int iStatus = [[dic objectForKey:@"Status"] intValue]; NSString *message=[dic objectForKey:@"Message"]; self.navigationItem.rightBarButtonItem.enabled=YES; if (iStatus == 0) { [self clearSenderData]; [self showAlertViewText:@"保存成功!"]; } else if(iStatus==ActionResultStatusAuthError ||iStatus==ActionResultStatusNoLogin ||iStatus==ActionResultStatusLogined ||iStatus == ActionResultStatusLoginedInvalid){ [self showReLoginDialog:message]; return; } else { [self showAlertViewText:message]; } } } // 保存失败 - (void)onLoadFail:(ASIDownManager *)sender { [self cancel]; self.navigationItem.rightBarButtonItem.enabled=YES; [self showAlertViewText:@"操作失败"]; } //开始编辑UITextField的方法 开始编辑UITextField的方法 -(void)textViewDidBeginEditing:(UITextView *)textView { CGRect textFrame = textView.frame; float textY = textFrame.origin.y+textFrame.size.height; float bottomY = self.view.frame.size.height-textY; if(bottomY>=216) //判断当前的高度是否已经有216,如果超过了就不需要再移动主界面的View高度 { _preTag = -1; return; } _preTag = textView.tag; float moveY = 216-bottomY; _preMoveY = moveY; NSTimeInterval animationDuration = 0.30f; CGRect frame = self.view.frame; frame.origin.y -=moveY;//view的Y轴上移 frame.size.height +=moveY; //View的高度增加 self.view.frame = frame; [UIView beginAnimations:@"ResizeView" context:nil]; [UIView setAnimationDuration:animationDuration]; self.view.frame = frame; [UIView commitAnimations];//设置调整界面的动画效果 } /** 结束编辑UITextField的方法,让原来的界面还原高度 */ -(void)textViewDidEndEditing:(UITextView *)textView { if(_preTag == -1) //当编辑的View不是需要移动的View { return; } float moveY ; NSTimeInterval animationDuration = 0.30f; CGRect frame = self.view.frame; if(_preTag == textView.tag) //当结束编辑的View的TAG是上次的就移动 { //还原界面 moveY = _preMoveY; frame.origin.y +=moveY; frame.size. height -=moveY; self.view.frame = frame; } //self.view移回原位置 [UIView beginAnimations:@"ResizeView" context:nil]; [UIView setAnimationDuration:animationDuration]; self.view.frame = frame; [UIView commitAnimations]; [textView resignFirstResponder]; } //Table View 数据源-得到分区数 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return 1; } //Table View 数据源-每个分区的行数 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return self.messages.count; } //Table View 数据源-获得tableviewcell - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { id message = [self.dataSource messageForRowAtIndexPath:indexPath]; // 如果需要定制复杂的业务UI,那么就实现该DataSource方法 if ([self.dataSource respondsToSelector:@selector(tableView:cellForRowAtIndexPath:targetMessage:)]) { UITableViewCell *tableViewCell = [self.dataSource tableView:tableView cellForRowAtIndexPath:indexPath targetMessage:message]; return tableViewCell; } BOOL displayTimestamp = NO; static NSString *cellIdentifier = @"DKUITableViewCell"; DKUITableViewCell *messageTableViewCell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier]; if (!messageTableViewCell) { messageTableViewCell = [[DKUITableViewCell alloc] initWithMessage:message displaysTimestamp:displayTimestamp reuseIdentifier:cellIdentifier]; messageTableViewCell.delegate = self; } messageTableViewCell.indexPath = indexPath; [messageTableViewCell configureCellWithMessage:message displaysTimestamp:displayTimestamp]; [messageTableViewCell setBackgroundColor:tableView.backgroundColor]; return messageTableViewCell; } //获得tableview的高度 - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { id messageModel = [self.dataSource messageForRowAtIndexPath:indexPath]; CGFloat calculateCellHeight ; if ([self.delegate respondsToSelector:@selector(tableView:heightForRowAtIndexPath:targetMessage:)]) { calculateCellHeight = [self.delegate tableView:tableView heightForRowAtIndexPath:indexPath targetMessage:messageModel]; } else { calculateCellHeight = [self calculateCellHeightWithMessage:messageModel atIndexPath:indexPath]; } return calculateCellHeight; } // UICollectionViewDataSource - (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{ return 1; } //DKPhotoPickerBrowserViewControllerDataSource - (NSInteger)numberOfSectionInPhotosInPickerBrowser:(DKPhotoPickerBrowserViewController *)pickerBrowser{ return self.assets.count; } //DKPhotoPickerBrowserViewControllerDataSource - (NSInteger)photoBrowser:(DKPhotoPickerBrowserViewController *)photoBrowser numberOfItemsInSection:(NSUInteger)section{ return [self.assets count]; } //DKPhotoPickerBrowserViewControllerDataSource - (DKPhotoPickerBrowserPhoto *)photoBrowser:(DKPhotoPickerBrowserViewController *)pickerBrowser photoAtIndexPath:(NSIndexPath *)indexPath{ id imageObj = [self.assets objectAtIndex:indexPath.item]; DKPhotoPickerBrowserPhoto *photo = [DKPhotoPickerBrowserPhoto photoAnyImageObjWith:imageObj]; // 包装下imageObj 成 DKPhotoPickerBrowserPhoto 传给数据源 LogNewCollectionViewCell *cell = (LogNewCollectionViewCell *)[self.collectionView cellForItemAtIndexPath:indexPath]; // 缩略图 photo.thumbImage = cell.imageView.image; return photo; } //DKPhotoPickerBrowserViewControllerDelegate - (void)photoBrowser:(DKPhotoPickerBrowserViewController *)photoBrowser removePhotoAtIndexPath:(NSIndexPath *)indexPath{ if (indexPath.row > [self.assets count]) return; [self.assets removeObjectAtIndex:indexPath.row]; [self.collectionView reloadData]; } //collectionview datasource - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{ CGFloat collectionheight=0; if (self.assets.count==0) { collectionheight=0; }else { collectionheight= [self getPhotoCollectionViewHeightWithPhotos:self.assets]; } _collectionView.frame=CGRectMake(10,CGRectGetMaxY(_txtContent.frame)+8, Screen_Width-20, collectionheight); _positionImg.frame=CGRectMake(15, CGRectGetMaxY(_collectionView.frame)+15,9,12); _lblPosition.frame=CGRectMake(CGRectGetMaxX(_positionImg.frame)+11,CGRectGetMaxY(_collectionView.frame)+10, 350, 20); _btnTakePhoto.frame=CGRectMake(15, CGRectGetMaxY(_positionImg.frame)+15,80,20); _btnPhoto.frame=CGRectMake((Screen_Width/3)+15, CGRectGetMaxY(_positionImg.frame)+15,80,20); _btnPosition.frame=CGRectMake((Screen_Width/3*2)+15, CGRectGetMaxY(_positionImg.frame)+15,80,20); _separatorView.frame=CGRectMake(15, CGRectGetMaxY(_btnTakePhoto.frame)+15,Screen_Width-15,1); _voiceImg.frame=CGRectMake(17,CGRectGetMaxY(_separatorView.frame)+18, 13, 15); _lblVoice.frame=CGRectMake(CGRectGetMaxX(_voiceImg.frame)+13,CGRectGetMaxY(_separatorView.frame)+15,30, 20); _btnRecord.frame=CGRectMake(Screen_Width-90, CGRectGetMaxY(_separatorView.frame)+18,100,20); _messageTV.frame=CGRectMake(CGRectGetMaxX(_lblVoice.frame)+2, CGRectGetMaxY(_separatorView.frame)+18,140, [self getSoundViewHeight:self.voicePaths]); if(self.voicePaths.count>0){ contentView.frame=CGRectMake(0, 0, Screen_Width,CGRectGetMaxY( _messageTV.frame)); } else{ contentView.frame=CGRectMake(0, 0, Screen_Width,CGRectGetMaxY(_btnRecord.frame)); } scroll.contentSize=CGSizeMake(self.view.frame.size.width, contentView.frame.size.height+110); return [self.assets count]; } //collectionview datasource - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{ LogNewCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"LogNewCollectionViewCell" forIndexPath:indexPath]; // 判断类型来获取Image DKPhotoAssets *asset = self.assets[indexPath.item]; if ([asset isKindOfClass:[DKPhotoAssets class]]) { cell.imageView.image = asset.originImage; }else if ([asset isKindOfClass:[NSString class]]){ [cell.imageView sd_setImageWithURL:[NSURL URLWithString:(NSString *)asset] placeholderImage:[UIImage imageNamed:@"wallpaper_placeholder"]]; }else if([asset isKindOfClass:[UIImage class]]){ cell.imageView.image = (UIImage *)asset; }else if ([asset isKindOfClass:[DKCamera class]]){ cell.imageView.image = [(DKCamera*)asset thumbImage]; } return cell; } //collectionView Delegate - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{ LogNewCollectionViewCell *cell = (LogNewCollectionViewCell *)[self.collectionView cellForItemAtIndexPath:indexPath]; // 图片游览器 DKPhotoPickerBrowserViewController *pickerBrowser = [[DKPhotoPickerBrowserViewController alloc] init]; // 传入点击图片View的话,会有微信朋友圈照片的风格 pickerBrowser.toView = cell.imageView; // 数据源/delegate pickerBrowser.delegate = self; pickerBrowser.dataSource = self; // 是否可以删除照片 pickerBrowser.editing = YES; // 当前选中的值 pickerBrowser.currentIndexPath = [NSIndexPath indexPathForItem:indexPath.row inSection:0]; // 展示控制器 [pickerBrowser show]; } //分享事件回调函数 - (void)reShareData:(NSArray *)sarray { if(self.shareArr==nil){ self.shareArr=[NSMutableArray new]; } [self.shareArr removeAllObjects]; [self.shareArr addObjectsFromArray:sarray]; NSMutableArray* ids=[NSMutableArray new]; NSMutableArray* names=[NSMutableArray new]; for (int i=0; i0){ self.sShareId=[ ids componentsJoinedByString:@","]; self.sShareUserName=[names componentsJoinedByString:@","]; [_btnShare setTitle:self.sShareUserName forState:UIControlStateNormal]; [_btnComment setTitle:@"请选择点评人" forState:UIControlStateNormal]; self.sCommentId = @""; self.sComment = @""; } } // 评论事件回调函数 - (void)reCommentData:(LogShareAndCommentInfoModel*)model { if(self.commentArr==nil){ self.commentArr=[NSMutableArray new]; } [self.commentArr removeAllObjects]; [self.commentArr addObject:model ]; NSMutableArray* ids=[NSMutableArray new]; NSMutableArray* names=[NSMutableArray new]; for (int i=0; i0){ self.sCommentId=[ ids componentsJoinedByString:@","]; self.sComment=[names componentsJoinedByString:@","]; [_btnComment setTitle:self.sComment forState:UIControlStateNormal]; } } //照片选择完回调方法 - (void)pickerViewControllerDoneAsstes:(NSArray *)assets{ for (int i=(int)self.assets.count-1;i>=0;i-- ) { if ([self.assets[i] isKindOfClass:[DKPhotoAssets class]]) { [self.assets removeObject:self.assets[i]]; } } for (DKPhotoAssets* asset in assets ) { if ([asset isKindOfClass:[DKPhotoAssets class]]) { [self.assets addObject:asset]; } } [self.collectionView reloadData]; } #pragma mark - 私有方法 //获得录音路径 - (NSString *)getRecorderPath { NSString *recorderPath = nil; recorderPath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) objectAtIndex: 0]; NSDate *now = [NSDate date]; NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; [dateFormatter setDateFormat:@"yyyyMMddHHmmssSSS"]; recorderPath = [recorderPath stringByAppendingFormat:@"%@-MySound.aac", [dateFormatter stringFromDate:now]]; return recorderPath; } //tableview底部间距 - (UIEdgeInsets)tableViewInsetsWithBottomValue:(CGFloat)bottom { UIEdgeInsets insets = UIEdgeInsetsZero; if ([self respondsToSelector:@selector(topLayoutGuide)]) { insets.top = 2;//self.topLayoutGuide.length; } insets.bottom = bottom; return insets; } // 计算消息的高度 - (CGFloat)calculateCellHeightWithMessage:(id )message atIndexPath:(NSIndexPath *)indexPath { CGFloat cellHeight = 0; cellHeight = [DKUITableViewCell calculateCellHeightWithMessage:message displaysTimestamp:NO]; return cellHeight; } //发送语音方法 - (void)didSendMessageWithVoice:(NSString *)voicePath voiceDuration:(NSString*)voiceDuration { if ([self.delegate respondsToSelector:@selector(didSendVoice:voiceDuration:fromSender:onDate:)]) { [self.delegate didSendVoice:voicePath voiceDuration:voiceDuration fromSender:self.messageSender onDate:[NSDate date]]; } } // Other Menu View Frame Helper Mehtod - (void)layoutOtherMenuViewHiden:(BOOL)hide { [UIView animateWithDuration:0.2 delay:0 options:UIViewAnimationOptionCurveEaseInOut animations:^{ __block CGRect inputViewFrame = _inputView.frame; __block CGRect otherMenuViewFrame; void (^InputViewAnimation)(BOOL hide) = ^(BOOL hide) { inputViewFrame.origin.y = (hide ? (CGRectGetHeight(self.view.bounds) - CGRectGetHeight(inputViewFrame)) : (CGRectGetMinY(otherMenuViewFrame) - CGRectGetHeight(inputViewFrame))); _inputView.frame = inputViewFrame; }; InputViewAnimation(hide); } completion:^(BOOL finished) { }]; } //录音完成方法 - (void)prepareRecordWithCompletion:(DKPrepareRecorderCompletion)completion { [self.voiceRecordHelper prepareRecordingWithPath:[self getRecorderPath] prepareRecorderCompletion:completion]; } //开始录音方法 - (void)startRecord { [self.voiceRecordHUD startRecordingHUDAtView:self.view]; [self.voiceRecordHelper startRecordingWithStartRecorderCompletion:^{ }]; } //完成录音方法 - (void)finishRecorded { WEAKSELF [self.voiceRecordHUD stopRecordCompled:^(BOOL fnished) { weakSelf.voiceRecordHUD = nil; }]; [self.voiceRecordHelper stopRecordingWithStopRecorderCompletion:^{ [weakSelf didSendMessageWithVoice:weakSelf.voiceRecordHelper.recordPath voiceDuration:weakSelf.voiceRecordHelper.recordDuration]; }]; } /** * 改变数据源需要的子线程 * * @param queue 子线程执行完成的回调block */ - (void)exChangeMessageDataSourceQueue:(void (^)())queue { dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), queue); } /** * 执行块代码在主线程 * * @param queue 主线程执行完成回调block */ - (void)exMainQueue:(void (^)())queue { dispatch_async(dispatch_get_main_queue(), queue); } //停止录音方法 - (void)pauseRecord { [self.voiceRecordHUD pauseRecord]; } //恢复录音方法 - (void)resumeRecord { [self.voiceRecordHUD resaueRecord]; } //取消录音方法 - (void)cancelRecord { WEAKSELF [self.voiceRecordHUD cancelRecordCompled:^(BOOL fnished) { weakSelf.voiceRecordHUD = nil; }]; [self.voiceRecordHelper cancelledDeleteWithCompletion:^{ }]; } //准备录音完成方法 - (void)prepareRecordingVoiceActionWithCompletion:(BOOL (^)(void))completion { [self prepareRecordWithCompletion:completion]; } //开始录音事件方法 - (void)didStartRecordingVoiceAction { [self startRecord]; } //取消录音事件方法 - (void)didCancelRecordingVoiceAction { [self cancelRecord]; } //完成录音事件方法 - (void)didFinishRecordingVoiceAction { if (self.isMaxTimeStop == NO) { [self finishRecorded]; } else { self.isMaxTimeStop = NO; } } //手指移出事件 - (void)didDragOutsideAction { [self resumeRecord]; } //手指移入事件 - (void)didDragInsideAction { [self pauseRecord]; } //获得声音 tableviewcell高度 - (CGFloat)getSoundViewHeight:(NSMutableArray *)soundArray { // 上下间隔已经在frame上做了 NSInteger row = soundArray.count; return row*50; } //添加消息 - (void)addMessage:(DKMessage *)addedMessage { WEAKSELF [self exChangeMessageDataSourceQueue:^{ NSMutableArray *messages = [NSMutableArray arrayWithArray:weakSelf.messages]; [messages addObject:addedMessage]; NSMutableArray *indexPaths = [NSMutableArray arrayWithCapacity:1]; [indexPaths addObject:[NSIndexPath indexPathForRow:messages.count - 1 inSection:0]]; [weakSelf exMainQueue:^{ weakSelf.messages = messages; [_messageTV insertRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationNone]; [weakSelf scrollToBottomAnimated:YES]; }]; }]; } //滚动到底部动画 - (void)scrollToBottomAnimated:(BOOL)animated { NSInteger rows = [_messageTV numberOfRowsInSection:0]; if (rows > 0) { [_messageTV scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:rows - 1 inSection:0] atScrollPosition:UITableViewScrollPositionBottom animated:animated]; } } //滚动到指定行 - (void)scrollToRowAtIndexPath:(NSIndexPath *)indexPath atScrollPosition:(UITableViewScrollPosition)position animated:(BOOL)animated { [_messageTV scrollToRowAtIndexPath:indexPath atScrollPosition:position animated:animated]; } //初始化布局 -(void)initUI{ scroll=[UIScrollView new]; scroll.tag=1001; scroll.delegate=self; [self.view addSubview:scroll]; scroll.frame=self.view.bounds; contentView=[[UIView alloc]init]; contentView.frame=CGRectZero; self.view.backgroundColor=[UIColor whiteColor]; [scroll addSubview:contentView]; UITapGestureRecognizer* gesture=[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hideKey)]; gesture.cancelsTouchesInView=NO; [contentView addGestureRecognizer:gesture]; UILabel* lblUserName=[[UILabel alloc] init]; lblUserName.frame=CGRectMake(15, 15, 100, 25); lblUserName.text=@"姓 名"; lblUserName.font=logTextFont; [contentView addSubview:lblUserName]; UITextField* textUserName=[[UITextField alloc] init]; textUserName.frame=CGRectMake(130, 15, 150, 25); textUserName.textColor=ButtonTitleColor; textUserName.text=kkUserName; textUserName.font=logTextFont; textUserName.enabled=NO; [contentView addSubview:textUserName]; UIView *userNameSeparator=[[UIView alloc]init]; userNameSeparator.frame=CGRectMake(130,CGRectGetMaxY(textUserName.frame)+14,Screen_Width-145,1); userNameSeparator.backgroundColor=LineBackgroundColor; [contentView addSubview: userNameSeparator]; UILabel* lblDate=[[UILabel alloc] init]; lblDate.frame=CGRectMake(15,CGRectGetMaxY(userNameSeparator.frame)+15, 100, 25); lblDate.text=@"日 期"; lblDate.font=logTextFont; [contentView addSubview:lblDate]; _btnDate=[UIButton buttonWithType:UIButtonTypeCustom]; _btnDate.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft; _btnDate.titleEdgeInsets = UIEdgeInsetsMake(0, 0, 0, 0); _btnDate.frame=CGRectMake(130,CGRectGetMaxY(userNameSeparator.frame)+15, 150, 25); [_btnDate setTitle:@"请选择日志日期" forState:UIControlStateNormal]; [_btnDate setTitleColor:ButtonTitleColor forState:UIControlStateNormal]; _btnDate.titleLabel.font=logTextFont; [_btnDate addTarget:self action:@selector(showStartSelect) forControlEvents:UIControlEventTouchUpInside]; [contentView addSubview:_btnDate]; UIView *dateSeparator=[[UIView alloc]init]; dateSeparator.frame=CGRectMake(130,CGRectGetMaxY(_btnDate.frame)+14,Screen_Width-145,1); dateSeparator.backgroundColor=LineBackgroundColor; [contentView addSubview: dateSeparator]; UILabel* lblScope=[[UILabel alloc] init]; lblScope.frame=CGRectMake(15,CGRectGetMaxY(dateSeparator.frame)+15, 100, 25); lblScope.text=@"范围权限"; lblScope.font=logTextFont; [contentView addSubview:lblScope]; _btnShare=[UIButton buttonWithType:UIButtonTypeCustom]; _btnShare.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft; _btnShare.titleEdgeInsets = UIEdgeInsetsMake(0, 0, 0, 0); _btnShare.frame=CGRectMake(130,CGRectGetMaxY(dateSeparator.frame)+15, 300, 25); [_btnShare setTitle:@"请选择范围权限" forState:UIControlStateNormal]; [_btnShare setTitleColor:ButtonTitleColor forState:UIControlStateNormal]; _btnShare.titleLabel.font=logTextFont; [_btnShare addTarget:self action:@selector(showScope) forControlEvents:UIControlEventTouchUpInside]; [contentView addSubview:_btnShare]; UIView *scopeSeparator=[[UIView alloc]init]; scopeSeparator.frame=CGRectMake(130,CGRectGetMaxY(_btnShare.frame)+14,Screen_Width-145, 1); scopeSeparator.backgroundColor=LineBackgroundColor; [contentView addSubview: scopeSeparator]; UILabel* lblChecker=[[UILabel alloc] init]; lblChecker.frame=CGRectMake(15,CGRectGetMaxY(scopeSeparator.frame)+15, 100, 25); lblChecker.text=@"点 评 人"; lblChecker.font=logTextFont; [contentView addSubview:lblChecker]; _btnComment=[UIButton buttonWithType:UIButtonTypeCustom]; _btnComment.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft; _btnComment.titleEdgeInsets = UIEdgeInsetsMake(0, 0, 0, 0); _btnComment.frame=CGRectMake(130,CGRectGetMaxY(scopeSeparator.frame)+15, 150, 25); [_btnComment setTitle:@"请选择点评人" forState:UIControlStateNormal]; [_btnComment setTitleColor:ButtonTitleColor forState:UIControlStateNormal]; _btnComment.titleLabel.font=logTextFont; [_btnComment addTarget:self action:@selector(showComment) forControlEvents:UIControlEventTouchUpInside]; [contentView addSubview:_btnComment]; UIView *selectView=[[UIView alloc]init]; selectView.frame=CGRectMake(0, CGRectGetMaxY(lblChecker.frame)+15, Screen_Width,40); selectView.backgroundColor=LineBackgroundColor; UILabel *selectBackgroundView=[[UILabel alloc]init]; selectBackgroundView.frame=CGRectMake(14,13,100,25); UIColor *color = [UIColor colorWithPatternImage:[UIImage imageNamed:@"selectBackground"]]; [selectBackgroundView setBackgroundColor:color]; selectBackgroundView.text=@" 以下均为选填"; selectBackgroundView.font=logTextFont; [selectView addSubview:selectBackgroundView]; [contentView addSubview:selectView]; _txtContent=[[UITextView alloc] init]; _txtContent.frame=CGRectMake(15,CGRectGetMaxY(selectView.frame)+15,Screen_Width-30,100); _txtContent.textColor=[UIColor blackColor]; _txtContent.font=logTextFont; _txtContent.delegate=self; _txtContent.tag=101; [contentView addSubview:_txtContent]; UILabel *placeHolderLabel = [[UILabel alloc] init]; placeHolderLabel.text = @"请填写日志内容"; placeHolderLabel.numberOfLines = 0; placeHolderLabel.textColor = ButtonTitleColor; placeHolderLabel.font=logTextFont; [placeHolderLabel sizeToFit]; [_txtContent addSubview: placeHolderLabel]; [_txtContent setValue:placeHolderLabel forKey:@"_placeholderLabel"]; UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init]; flowLayout.itemSize = CGSizeMake(80,80); flowLayout.minimumInteritemSpacing = 5; flowLayout.minimumLineSpacing = 10; flowLayout.scrollDirection = UICollectionViewScrollDirectionVertical; _collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(10,CGRectGetMaxY(_txtContent.frame)+8, Screen_Width-20, 1) collectionViewLayout:flowLayout]; _collectionView.backgroundColor = [UIColor clearColor]; _collectionView.dataSource = self; _collectionView.delegate = self; [_collectionView registerNib:[UINib nibWithNibName:@"LogNewCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:@"LogNewCollectionViewCell"]; [contentView addSubview:_collectionView]; _positionImg=[[UIImageView alloc]init]; _positionImg.frame=CGRectMake(15, CGRectGetMaxY(_collectionView.frame),9,12); [_positionImg setImage:[UIImage imageNamed:@"log_position"]]; [contentView addSubview:_positionImg]; _lblPosition=[[UILabel alloc]init]; _lblPosition.frame=CGRectMake(CGRectGetMaxX(_positionImg.frame)+11,CGRectGetMaxY(_collectionView.frame)+10, 350, 20); _lblPosition.font=logTextFont; [contentView addSubview:_lblPosition]; _btnTakePhoto=[UIButton buttonWithType:UIButtonTypeCustom]; _btnTakePhoto.frame=CGRectMake(15, CGRectGetMaxY(_positionImg.frame)+15,80,20); [contentView addSubview:_btnTakePhoto]; UIImageView *takephotoImg=[[UIImageView alloc]init]; takephotoImg.frame=CGRectMake(0,3, 18, 15); [takephotoImg setImage:[UIImage imageNamed:@"icon_takephoto"]]; takephotoImg.userInteractionEnabled=NO; [_btnTakePhoto addSubview:takephotoImg]; UILabel *lblTakePhoto=[[UILabel alloc]init]; lblTakePhoto.text=@"照相"; lblTakePhoto.font=logTextFont; lblTakePhoto.frame=CGRectMake(CGRectGetMaxX(takephotoImg.frame)+11,0,50, 20); [_btnTakePhoto addSubview:lblTakePhoto]; [_btnTakePhoto addTarget:self action:@selector(selectCamera) forControlEvents:UIControlEventTouchUpInside]; _btnPhoto=[UIButton buttonWithType:UIButtonTypeCustom]; _btnPhoto.frame=CGRectMake((Screen_Width/3)+15, CGRectGetMaxY(_positionImg.frame)+15,80,20); [contentView addSubview:_btnPhoto]; UIImageView *photoImg=[[UIImageView alloc]init]; photoImg.userInteractionEnabled=NO; photoImg.frame=CGRectMake(0,3, 18, 15); [photoImg setImage:[UIImage imageNamed:@"icon_photo"]]; [_btnPhoto addSubview:photoImg]; UILabel *lblPhoto=[[UILabel alloc]init]; lblPhoto.text=@"相册"; lblPhoto.font=logTextFont; lblPhoto.frame=CGRectMake(CGRectGetMaxX(photoImg.frame)+11,0,50, 20); [_btnPhoto addSubview:lblPhoto]; [_btnPhoto addTarget:self action:@selector(selectPhotos) forControlEvents:UIControlEventTouchUpInside]; _btnPosition=[UIButton buttonWithType:UIButtonTypeCustom]; _btnPosition.frame=CGRectMake((Screen_Width/3*2)+15, CGRectGetMaxY(_positionImg.frame)+15,80,20); [contentView addSubview:_btnPosition]; [_btnPosition addTarget:self action:@selector(selectPosition) forControlEvents:UIControlEventTouchUpInside]; UIImageView *mapPositionImg=[[UIImageView alloc]init]; mapPositionImg.userInteractionEnabled=NO; mapPositionImg.frame=CGRectMake(0,3, 13, 15); [ mapPositionImg setImage:[UIImage imageNamed:@"icon_position"]]; [_btnPosition addSubview: mapPositionImg]; UILabel *lblPosition=[[UILabel alloc]init]; lblPosition.text=@"所在位置"; lblPosition.font=logTextFont; lblPosition.frame=CGRectMake(CGRectGetMaxX(mapPositionImg.frame)+11,0,80, 20); [_btnPosition addSubview:lblPosition]; _separatorView=[[UIView alloc]init]; _separatorView.frame=CGRectMake(15, CGRectGetMaxY(_btnTakePhoto.frame)+15,Screen_Width-15, 1); _separatorView.backgroundColor=LineBackgroundColor; [contentView addSubview:_separatorView]; _voiceImg=[[UIImageView alloc]init]; _voiceImg.frame=CGRectMake(17,CGRectGetMaxY(_separatorView.frame)+18, 13, 15); [_voiceImg setImage:[UIImage imageNamed:@"icon_voice"]]; [contentView addSubview:_voiceImg]; _lblVoice=[[UILabel alloc]init]; _lblVoice.text=@"语音"; _lblVoice.font=logTextFont; _lblVoice.frame=CGRectMake(CGRectGetMaxX(_voiceImg.frame)+13,CGRectGetMaxY(_separatorView.frame)+15,30, 20); [contentView addSubview:_lblVoice]; _messageTV = [[DKMessageTableView alloc] initWithFrame:CGRectMake(CGRectGetMaxX(_lblVoice.frame)+2, CGRectGetMaxY(_separatorView.frame)+18,190, 1) style:UITableViewStylePlain]; _messageTV.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight; _messageTV.dataSource = self; _messageTV.delegate = self; _messageTV.separatorStyle = UITableViewCellSeparatorStyleNone; [contentView addSubview:_messageTV]; _btnRecord=[UIButton buttonWithType:UIButtonTypeCustom]; _btnRecord.frame=CGRectMake(Screen_Width-90, CGRectGetMaxY(_separatorView.frame)+18,100,20); [contentView addSubview:_btnRecord]; UIImageView *recordImg=[[UIImageView alloc]init]; recordImg.frame=CGRectMake(0,3, 14, 15); recordImg.userInteractionEnabled=NO; [recordImg setImage:[UIImage imageNamed:@"longpressed"]]; [_btnRecord addSubview:recordImg]; [_btnRecord addTarget:self action:@selector(switchKeyboard) forControlEvents:UIControlEventTouchUpInside]; UILabel *lblRecord=[[UILabel alloc]init]; lblRecord.text=@"长按录音"; lblRecord.textColor=LabelGrayTextColor; lblRecord.font=logTextFont; lblRecord.frame=CGRectMake(CGRectGetMaxX(recordImg.frame)+3,0,80, 20); [_btnRecord addSubview:lblRecord]; contentView.frame=CGRectMake(0, 0, Screen_Width,CGRectGetMaxY(_btnRecord.frame)); // 输入工具条的frame CGRect inputFrame = CGRectMake(0.0f, self.view.frame.size.height - 45.0, self.view.frame.size.width, 45.0); // 初始化输入工具条 _inputView = [[DKMessageInputView alloc] initWithFrame:inputFrame]; _inputView.allowsSendVoice = self.allowsSendVoice; _inputView.delegate = self; [self.view addSubview:_inputView]; [self.view bringSubviewToFront:_inputView]; } // 保存前的校验 -(BOOL)check { if(_sDate ==nil || [NSString stringWithFormat:@"%@",_sDate ].length==0) { [self showAlertViewText:@"请选择日志日期"]; return NO; } if(_txtContent.text ==nil || [NSString stringWithFormat:@"%@",_txtContent.text ].length==0) { [self showAlertViewText:@"请输入日志内容"]; return NO; } if(_sShareId ==nil ||[NSString stringWithFormat:@"%@",_sShareId ].length==0) { [self showAlertViewText:@"请选择分享范围"]; return NO; } if(_sCommentId ==nil || [NSString stringWithFormat:@"%@",_sCommentId ].length==0) { [self showAlertViewText:@"请选择点评人"]; return NO; } return YES; } // 上传图片和语音 -(BOOL)saveImageAndVioceManager{ _mDownManager = [[ASIDownManager alloc] init]; _mDownManager.delegate = self; //////////////上传语音 NSString *urlStr = ServerURL; NSMutableDictionary *dict = [NSMutableDictionary dictionary]; [dict setObject:@"SaveSound" forKey:@"Action"]; [dict setObject:[NSString stringWithFormat:@"%@",kkAccountCode]forKey:@"AccountCode"]; [dict setObject:kkUserCode forKey:@"UserCode"]; [dict setObject:kkUserPwd forKey:@"UserPassword"]; [dict setObject:kkSessionKey forKey:@"SessionKey"]; [self.voiceUploadPaths removeAllObjects]; for (int i=0; i0){ [dict setObject:self.sLocation forKey:@"RecordLocation"]; } /////////图片上传返回的地址 if (self.imagePaths && self.imagePaths.count>0) { [dict setObject:[self.imagePaths componentsJoinedByString:@"," ] forKey:@"ImagePath"]; } if (self.voiceUploadPaths && self.voiceUploadPaths.count>0) { [dict setObject:[self.voiceUploadPaths componentsJoinedByString:@"," ] forKey:@"SoundPath"]; } if (self.voiceUploadTimes && self.voiceUploadTimes.count>0) { [dict setObject:[self.voiceUploadTimes componentsJoinedByString:@"," ] forKey:@"SoundTime"]; } [_mDownManager postHttpRequest:urlStr dic:dict path:nil fileName:nil]; } //切换底部布局 -(void)switchKeyboard{ if(_inputView!=nil){ if(_inputView.saveButton.hidden==YES){ _inputView.saveButton.hidden=NO; _inputView.switchButton.hidden=YES; _inputView.voiceButton.hidden=YES; } else{ _inputView.saveButton.hidden=YES; _inputView.switchButton.hidden=NO; _inputView.voiceButton.hidden=NO; } } } //保存图片至沙盒 - (DKCamera *) saveImage:(UIImage *)currentImage withName:(NSString *)imageName { if (!currentImage) { return nil; } NSDateFormatter *formater = [[NSDateFormatter alloc] init]; formater.dateFormat = @"yyyyMMddHHmmss"; NSString *currentTimeStr = [[formater stringFromDate:[NSDate date]] stringByAppendingFormat:@"_%d_.jpg" ,arc4random_uniform(10000)]; NSData *imageData= [LeslieAsyncImageDownloader resetSizeOfImageData:currentImage maxSize:500]; // 获取沙盒目录 NSString *fullPath ; if([imageName isEqualToString:@""] || imageName==nil){ fullPath = [[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"] stringByAppendingPathComponent:currentTimeStr]; } else{ fullPath = [[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"] stringByAppendingPathComponent:imageName]; } // 将图片写入文件 [imageData writeToFile:fullPath atomically:NO]; UIImage *savedImage = [[UIImage alloc] initWithContentsOfFile:fullPath]; //NSData *data = UIImageJPEGRepresentation(savedImage, 0.3); DKCamera *camera = [[DKCamera alloc] init]; camera.imagePath = fullPath; camera.thumbImage =savedImage;// [UIImage imageWithData:data]; return camera; } //选择地图方法 -(void)selectPosition{ MapViewController *mc=[MapViewController new]; mc.pDelegate=self; [self.navigationController pushViewController:mc animated:YES]; } //相机拍照方法 -(void)selectCamera{ DKCameraViewController *cameraVc = [[DKCameraViewController alloc] init]; __weak typeof(self) weakSelf = self; // 多选相册+相机多拍 回调 [cameraVc startCameraOrPhotoFileWithViewController:self complate:^(NSArray *object) { // 选择完照片、拍照完回调 [object enumerateObjectsUsingBlock:^(id asset, NSUInteger idx, BOOL *stop) { if(weakSelf.assets.count<9){ if ([asset isKindOfClass:[DKCamera class]]) { [weakSelf.assets addObject:asset]; }else{ [weakSelf.assets addObject:asset]; } }else{ [self showAlertViewText:@"最多只能选择9张图片"]; } }]; [weakSelf.collectionView reloadData]; }]; self.cameraVc = cameraVc; } #pragma mark - select Photo Library -(NSInteger)cameraCount{ NSInteger count=0; for (int i=0; i