LightingAndSoundVC.m 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. //
  2. // LightingAndSoundVC.m
  3. // jiaPei
  4. //
  5. // Created by apple on 16/1/17.
  6. // Copyright © 2016年 JCZ. All rights reserved.
  7. //
  8. #import "LightingAndSoundVC.h"
  9. #import "LightingCell.h"
  10. #import <AVFoundation/AVFoundation.h>
  11. @interface LightingAndSoundVC ()<UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout,AVAudioPlayerDelegate>
  12. {
  13. UICollectionView *mainCollection;
  14. NSArray *lightTitle;
  15. NSArray *soundTitle;
  16. NSArray *imgArray;
  17. NSIndexPath *currentIndex;
  18. NSIndexPath *preIndex;
  19. AVAudioPlayer *_player;
  20. LightingCell *currentCell;
  21. BOOL isExchange;
  22. }
  23. /// viewModel
  24. @property (nonatomic, readonly, strong) LightingAndSoundViewModel *viewModel;
  25. @end
  26. @implementation LightingAndSoundVC
  27. @dynamic viewModel;
  28. - (void)viewDidLoad {
  29. [super viewDidLoad];
  30. preIndex = [NSIndexPath indexPathForRow:-1 inSection:0];
  31. [self myInit];
  32. }
  33. -(void)myInit
  34. {
  35. [self.view setBackgroundColor:backGroundColor];
  36. [self configNavigationBar];
  37. if (self.viewModel.styleTag) {
  38. _styleTag = self.viewModel.styleTag;
  39. }
  40. NSArray *segmentArray = [[NSArray alloc]initWithObjects:@"灯光模拟",@"语音模拟",nil];
  41. UISegmentedControl *segment = [[UISegmentedControl alloc] initWithItems:segmentArray];
  42. segment.selectedSegmentIndex = _styleTag - 4;
  43. segment.tintColor = [UIColor clearColor];
  44. NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:RQ_MAIN_COLOR,NSForegroundColorAttributeName, [UIFont fontWithName:@"Helvetica" size:18.f],NSFontAttributeName ,nil];
  45. NSDictionary *dicNormol = [NSDictionary dictionaryWithObjectsAndKeys:kTitleColor,NSForegroundColorAttributeName, [UIFont fontWithName:@"Helvetica" size:18.f],NSFontAttributeName ,nil];
  46. [segment setTitleTextAttributes:dic forState:UIControlStateSelected];
  47. [segment setTitleTextAttributes:dicNormol forState:UIControlStateNormal];
  48. [segment addTarget:self action:@selector(Selectbutton:) forControlEvents:UIControlEventValueChanged];
  49. [self.navigationItem setTitleView:segment];
  50. //创建collection
  51. lightTitle = [[NSArray alloc] initWithObjects:@"灯光1",@"灯光2",@"灯光3",@"灯光4",@"灯光5",@"灯光6",@"灯光7",@"灯光8", nil];
  52. soundTitle = [[NSArray alloc] initWithObjects:@"考试准备",@"起步",@"路口直行",@"变更车道",@"公共汽车站",@"通过学校",@"直线行驶",@"路口左转",@"路口右转",@"百米加减档",@"会车",@"超车",@"减速",@"限速",@"人行横道",@"人行横道-有行人通过",@"隧道",@"掉头",@"靠边停车", nil];
  53. imgArray = [NSArray arrayWithObjects:@"img01",@"img02",@"img03",@"img04",@"img05",@"img06",@"img07",@"img08",@"img09",@"img10",@"img11",@"img12",@"img13",@"img14",@"img15",@"img16",@"img17",@"img18",@"img19", nil];
  54. UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
  55. layout.itemSize = CGSizeMake(kSize.width/4.0 - 8, kSize.width/4.0);
  56. layout.sectionInset = UIEdgeInsetsMake(1, 1, 1, 1);
  57. mainCollection = [[UICollectionView alloc] initWithFrame:kFrame collectionViewLayout:layout];
  58. mainCollection.height -= kNavOffSet;
  59. mainCollection.backgroundColor = backGroundColor;
  60. mainCollection.delegate = self;
  61. mainCollection.dataSource = self;
  62. [self.view addSubview:mainCollection];
  63. [mainCollection registerClass:[LightingCell class] forCellWithReuseIdentifier:@"cell"];
  64. }
  65. #pragma mark collectionView delegate
  66. -(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
  67. {
  68. if (_styleTag == 4)
  69. {
  70. return lightTitle.count;
  71. }
  72. else
  73. {
  74. return soundTitle.count;
  75. }
  76. }
  77. -(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
  78. {
  79. LightingCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
  80. if (_styleTag == 4)
  81. {
  82. cell.titleLabel.text = lightTitle[indexPath.row];
  83. cell.imgView.image = [UIImage imageNamed:@"lighting"];
  84. }
  85. else
  86. {
  87. cell.titleLabel.text = soundTitle[indexPath.row];
  88. cell.imgView.image = [UIImage imageNamed:imgArray[indexPath.row]];
  89. }
  90. return cell;
  91. }
  92. -(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
  93. {
  94. //先处理之前的cell动画视图
  95. if (preIndex != [NSIndexPath indexPathForRow:-1 inSection:0])
  96. {
  97. LightingCell *oldCell = (LightingCell *)[collectionView cellForItemAtIndexPath:preIndex];
  98. if (oldCell.imgView.subviews.count > 0)
  99. {
  100. [[oldCell.imgView.subviews firstObject] removeFromSuperview];
  101. }
  102. oldCell.isMySelected = NO;
  103. }
  104. currentIndex = indexPath;
  105. //判断是否是按得同一个按钮 如果不是 判断是否正在播放 处理后播放新的音频 如果是 判断是否在播放 是则停止
  106. if (isExchange)
  107. {
  108. isExchange = NO;
  109. preIndex = [NSIndexPath indexPathForRow:-1 inSection:0];
  110. }
  111. LightingCell *cell = (LightingCell *)[collectionView cellForItemAtIndexPath:indexPath];
  112. if (currentIndex.row != preIndex.row)
  113. {
  114. if (_player.isPlaying)
  115. {
  116. [_player stop];
  117. _player = nil;
  118. }
  119. NSString *resourceName;
  120. if (_styleTag == 4)
  121. {
  122. resourceName = [NSString stringWithFormat:@"light%d",(int)indexPath.row + 1];
  123. }
  124. else
  125. {
  126. resourceName = [NSString stringWithFormat:@"voice%d",(int)indexPath.row];
  127. }
  128. _player = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL
  129. fileURLWithPath:[[NSBundle mainBundle] pathForResource:resourceName ofType:@"mp3"]] error:nil];
  130. _player.delegate = self;
  131. //设置音量大小 (0-1)
  132. _player.volume = 1.0;
  133. //设置播放速率
  134. _player.enableRate = YES;
  135. //0.5 慢一半 1.0 默认 2.0快
  136. _player.rate = 1.0;
  137. [_player play];
  138. }
  139. else
  140. {
  141. if (_player.isPlaying)
  142. {
  143. [_player stop];
  144. _player = nil;
  145. cell.isMySelected = 1;
  146. }
  147. else
  148. {
  149. NSString *resourceName;
  150. if (_styleTag == 4)
  151. {
  152. resourceName = [NSString stringWithFormat:@"light%d",(int)indexPath.row + 1];
  153. }
  154. else
  155. {
  156. resourceName = [NSString stringWithFormat:@"voice%d",(int)indexPath.row];
  157. }
  158. _player = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL
  159. fileURLWithPath:[[NSBundle mainBundle] pathForResource:resourceName ofType:@"mp3"]] error:nil];
  160. _player.delegate = self;
  161. //设置音量大小 (0-1)
  162. _player.volume = 1.0;
  163. //设置播放速率
  164. _player.enableRate = YES;
  165. //0.5 慢一半 1.0 默认 2.0快
  166. _player.rate = 1.0;
  167. [_player play];
  168. }
  169. }
  170. if (cell.isMySelected)
  171. {
  172. cell.isMySelected = NO;
  173. if (cell.imgView.subviews.count > 0)
  174. {
  175. [[cell.imgView.subviews firstObject] removeFromSuperview];
  176. }
  177. currentIndex = [NSIndexPath indexPathForRow:-1 inSection:0];
  178. }
  179. else
  180. {
  181. cell.isMySelected = YES;
  182. UIImageView *animationView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, cell.imgView.frame.size.width, cell.imgView.frame.size.height)];
  183. animationView.backgroundColor = backGroundColor;
  184. animationView.alpha = .8;
  185. //动画
  186. NSMutableArray *imgsArray = [NSMutableArray array];
  187. for (int i = 1; i < 4; i ++)
  188. {
  189. UIImage *img = [UIImage imageNamed:[NSString stringWithFormat:@"animate0%d",i]];
  190. [imgsArray addObject:img];
  191. }
  192. [animationView setAnimationImages:imgsArray];
  193. //动画持续时间
  194. [animationView setAnimationDuration:1];
  195. //循环次数 0代表无限
  196. [animationView setAnimationRepeatCount:0];
  197. [cell.imgView addSubview:animationView];
  198. //开始动画
  199. [animationView startAnimating];
  200. //记录当前cell
  201. currentCell = cell;
  202. }
  203. preIndex = currentIndex;
  204. }
  205. - (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag
  206. {
  207. if (currentCell.imgView.subviews.count > 0)
  208. {
  209. [[currentCell.imgView.subviews firstObject] removeFromSuperview];
  210. }
  211. currentIndex = [NSIndexPath indexPathForRow:-1 inSection:0];
  212. preIndex = [NSIndexPath indexPathForRow:-1 inSection:0];
  213. currentCell.isMySelected = NO;
  214. [_player stop];
  215. _player = nil;
  216. }
  217. -(void)Selectbutton:(UISegmentedControl *)segment
  218. {
  219. if (_player.isPlaying)
  220. {
  221. [_player stop];
  222. _player = nil;
  223. }
  224. if (currentCell.imgView.subviews.count > 0)
  225. {
  226. [[currentCell.imgView.subviews firstObject] removeFromSuperview];
  227. }
  228. currentIndex = [NSIndexPath indexPathForRow:-1 inSection:0];
  229. preIndex = [NSIndexPath indexPathForRow:-1 inSection:0];
  230. currentCell.isMySelected = NO;
  231. self.styleTag = segment.selectedSegmentIndex + 4;
  232. isExchange = YES;
  233. [mainCollection reloadData];
  234. }
  235. - (void)didReceiveMemoryWarning {
  236. [super didReceiveMemoryWarning];
  237. // Dispose of any resources that can be recreated.
  238. }
  239. @end