LightingAndSoundVC.m 9.2 KB

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