// // LightingAndSoundVC.m // jiaPei // // Created by apple on 16/1/17. // Copyright © 2016年 JCZ. All rights reserved. // #import "LightingAndSoundVC.h" #import "LightingCell.h" #import @interface LightingAndSoundVC () { UICollectionView *mainCollection; NSArray *lightTitle; NSArray *soundTitle; NSArray *imgArray; NSIndexPath *currentIndex; NSIndexPath *preIndex; AVAudioPlayer *_player; LightingCell *currentCell; BOOL isExchange; } /// viewModel @property (nonatomic, readonly, strong) LightingAndSoundViewModel *viewModel; @end @implementation LightingAndSoundVC @dynamic viewModel; - (void)viewDidLoad { [super viewDidLoad]; preIndex = [NSIndexPath indexPathForRow:-1 inSection:0]; [self myInit]; } -(void)myInit { [self.view setBackgroundColor:backGroundColor]; [self configNavigationBar]; if (self.viewModel.styleTag) { _styleTag = self.viewModel.styleTag; } NSArray *segmentArray = [[NSArray alloc]initWithObjects:@"灯光模拟",@"语音模拟",nil]; UISegmentedControl *segment = [[UISegmentedControl alloc] initWithItems:segmentArray]; segment.selectedSegmentIndex = _styleTag - 4; segment.tintColor = [UIColor clearColor]; NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:RQ_MAIN_COLOR,NSForegroundColorAttributeName, [UIFont fontWithName:@"Helvetica" size:18.f],NSFontAttributeName ,nil]; NSDictionary *dicNormol = [NSDictionary dictionaryWithObjectsAndKeys:kTitleColor,NSForegroundColorAttributeName, [UIFont fontWithName:@"Helvetica" size:18.f],NSFontAttributeName ,nil]; [segment setTitleTextAttributes:dic forState:UIControlStateSelected]; [segment setTitleTextAttributes:dicNormol forState:UIControlStateNormal]; [segment addTarget:self action:@selector(Selectbutton:) forControlEvents:UIControlEventValueChanged]; [self.navigationItem setTitleView:segment]; //创建collection lightTitle = [[NSArray alloc] initWithObjects:@"灯光1",@"灯光2",@"灯光3",@"灯光4",@"灯光5",@"灯光6",@"灯光7",@"灯光8", nil]; soundTitle = [[NSArray alloc] initWithObjects:@"考试准备",@"起步",@"路口直行",@"变更车道",@"公共汽车站",@"通过学校",@"直线行驶",@"路口左转",@"路口右转",@"百米加减档",@"会车",@"超车",@"减速",@"限速",@"人行横道",@"人行横道-有行人通过",@"隧道",@"掉头",@"靠边停车", nil]; imgArray = [NSArray arrayWithObjects:@"img01",@"img02",@"img03",@"img04",@"img05",@"img06",@"img07",@"img08",@"img09",@"img10",@"img11",@"img12",@"img13",@"img14",@"img15",@"img16",@"img17",@"img18",@"img19", nil]; UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init]; layout.itemSize = CGSizeMake(kSize.width/4.0 - 8, kSize.width/4.0); layout.sectionInset = UIEdgeInsetsMake(1, 1, 1, 1); mainCollection = [[UICollectionView alloc] initWithFrame:kFrame collectionViewLayout:layout]; mainCollection.height -= kNavOffSet; mainCollection.backgroundColor = backGroundColor; mainCollection.delegate = self; mainCollection.dataSource = self; [self.view addSubview:mainCollection]; [mainCollection registerClass:[LightingCell class] forCellWithReuseIdentifier:@"cell"]; } #pragma mark collectionView delegate -(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section { if (_styleTag == 4) { return lightTitle.count; } else { return soundTitle.count; } } -(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath { LightingCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath]; if (_styleTag == 4) { cell.titleLabel.text = lightTitle[indexPath.row]; cell.imgView.image = [UIImage imageNamed:@"lighting"]; } else { cell.titleLabel.text = soundTitle[indexPath.row]; cell.imgView.image = [UIImage imageNamed:imgArray[indexPath.row]]; } return cell; } -(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath { //先处理之前的cell动画视图 if (preIndex != [NSIndexPath indexPathForRow:-1 inSection:0]) { LightingCell *oldCell = (LightingCell *)[collectionView cellForItemAtIndexPath:preIndex]; if (oldCell.imgView.subviews.count > 0) { [[oldCell.imgView.subviews firstObject] removeFromSuperview]; } oldCell.isMySelected = NO; } currentIndex = indexPath; //判断是否是按得同一个按钮 如果不是 判断是否正在播放 处理后播放新的音频 如果是 判断是否在播放 是则停止 if (isExchange) { isExchange = NO; preIndex = [NSIndexPath indexPathForRow:-1 inSection:0]; } LightingCell *cell = (LightingCell *)[collectionView cellForItemAtIndexPath:indexPath]; if (currentIndex.row != preIndex.row) { if (_player.isPlaying) { [_player stop]; _player = nil; } NSString *resourceName; if (_styleTag == 4) { resourceName = [NSString stringWithFormat:@"light%d",(int)indexPath.row + 1]; } else { resourceName = [NSString stringWithFormat:@"voice%d",(int)indexPath.row]; } _player = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:resourceName ofType:@"mp3"]] error:nil]; _player.delegate = self; //设置音量大小 (0-1) _player.volume = 1.0; //设置播放速率 _player.enableRate = YES; //0.5 慢一半 1.0 默认 2.0快 _player.rate = 1.0; [_player play]; } else { if (_player.isPlaying) { [_player stop]; _player = nil; cell.isMySelected = 1; } else { NSString *resourceName; if (_styleTag == 4) { resourceName = [NSString stringWithFormat:@"light%d",(int)indexPath.row + 1]; } else { resourceName = [NSString stringWithFormat:@"voice%d",(int)indexPath.row]; } _player = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:resourceName ofType:@"mp3"]] error:nil]; _player.delegate = self; //设置音量大小 (0-1) _player.volume = 1.0; //设置播放速率 _player.enableRate = YES; //0.5 慢一半 1.0 默认 2.0快 _player.rate = 1.0; [_player play]; } } if (cell.isMySelected) { cell.isMySelected = NO; if (cell.imgView.subviews.count > 0) { [[cell.imgView.subviews firstObject] removeFromSuperview]; } currentIndex = [NSIndexPath indexPathForRow:-1 inSection:0]; } else { cell.isMySelected = YES; UIImageView *animationView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, cell.imgView.frame.size.width, cell.imgView.frame.size.height)]; animationView.backgroundColor = backGroundColor; animationView.alpha = .8; //动画 NSMutableArray *imgsArray = [NSMutableArray array]; for (int i = 1; i < 4; i ++) { UIImage *img = [UIImage imageNamed:[NSString stringWithFormat:@"animate0%d",i]]; [imgsArray addObject:img]; } [animationView setAnimationImages:imgsArray]; //动画持续时间 [animationView setAnimationDuration:1]; //循环次数 0代表无限 [animationView setAnimationRepeatCount:0]; [cell.imgView addSubview:animationView]; //开始动画 [animationView startAnimating]; //记录当前cell currentCell = cell; } preIndex = currentIndex; } - (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag { if (currentCell.imgView.subviews.count > 0) { [[currentCell.imgView.subviews firstObject] removeFromSuperview]; } currentIndex = [NSIndexPath indexPathForRow:-1 inSection:0]; preIndex = [NSIndexPath indexPathForRow:-1 inSection:0]; currentCell.isMySelected = NO; [_player stop]; _player = nil; } -(void)Selectbutton:(UISegmentedControl *)segment { if (_player.isPlaying) { [_player stop]; _player = nil; } if (currentCell.imgView.subviews.count > 0) { [[currentCell.imgView.subviews firstObject] removeFromSuperview]; } currentIndex = [NSIndexPath indexPathForRow:-1 inSection:0]; preIndex = [NSIndexPath indexPathForRow:-1 inSection:0]; currentCell.isMySelected = NO; self.styleTag = segment.selectedSegmentIndex + 4; isExchange = YES; [mainCollection reloadData]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } @end