需求:中間紅色框?yàn)闄M向滾動(dòng)的collectionView(前一場(chǎng)/后一場(chǎng)的可用:當(dāng)前第0場(chǎng)僧界,前一場(chǎng)不可用节腐;當(dāng)前最后一場(chǎng)猜揪,后一場(chǎng)不可用)。
1. 點(diǎn)擊前一場(chǎng)/后一場(chǎng), collectionView滾動(dòng)到對(duì)應(yīng)位置聋庵,中間顯示當(dāng)前選擇時(shí)間膘融,控制前一場(chǎng)/后一場(chǎng)是否可用
2. 滾動(dòng)collectionView,控制前一場(chǎng)/后一場(chǎng)是否可用祭玉,中間顯示當(dāng)前選擇時(shí)間
3. 選擇pickerView氧映,collectionView滾動(dòng)到對(duì)應(yīng)位置,控制前一場(chǎng)/后一場(chǎng)是否可用脱货,中間顯示當(dāng)前選擇時(shí)間
1. 設(shè)置默認(rèn)顯示:
- (void)viewDidLoad {
[super viewDidLoad];
// 默認(rèn)顯示第最后一場(chǎng),此時(shí)不能加collectionView的滾動(dòng)動(dòng)畫
_currentScreening = self.movieHallSeatModels.count-1;
[self showDataWithCurrentScreening:_currentScreening animated:NO];
}
2. 點(diǎn)擊前一場(chǎng):
- (void)didClickLastFilmBtn {
if (_currentScreening == 0) return;
_currentScreening--;
[self showDataWithCurrentScreening:_currentScreening animated:YES];
}
3. 點(diǎn)擊后一場(chǎng):
- (void)didClickNextFilmBtn {
if (_currentScreening == self.movieHallSeatModels.count-1) return;
_currentScreening++;
[self showDataWithCurrentScreening:_currentScreening animated:YES];
}
4. 點(diǎn)擊時(shí)間振峻,彈出pickerView臼疫,并選中到當(dāng)前場(chǎng)次:
- (void)didClickChooseTimeBtn {
[self.view addSubview:self.maskview];
[self.maskview addSubview:self.chooseTimeView];
CGFloat y = SCREEN_HEIGHT - FYChooseTimeView_H;
CGFloat height = FYChooseTimeView_H;
[UIView animateWithDuration:0.3 animations:^{
self.chooseTimeView.frame = CGRectMake(0, y, SCREEN_WIDTH, height);
}];
// pickerView顯示當(dāng)前選中場(chǎng)次
[self.timePickerView selectRow:_currentScreening inComponent:0 animated:NO];
}
選中pickerView的代理方法 :
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
_chosenRow = row;
_currentScreening = row;
_chosenTimeStr = self.chooseTimes[row];
}
點(diǎn)擊確定:
- (void)didClickConfirmTimeBtn {
[self showDataWithCurrentScreening:_chosenRow animated:YES];
[self didClickMaskviewBtn];
}
核心方法:
- (void)showDataWithCurrentScreening:(NSInteger)index animated:(BOOL)animated{
// 設(shè)置可用
[self isFilmBtnEnableWithButton:self.lastFilmBtn condition:0];
[self isFilmBtnEnableWithButton:self.nextFilmBtn condition:self.movieHallSeatModels.count-1];
// 設(shè)置顯示時(shí)間,及pickView的選中
_chosenTimeStr = self.chooseTimes[index];
[self.chooseTimeBtn setTitle:_chosenTimeStr forState:UIControlStateNormal];
[self.timePickerView selectRow:_currentScreening inComponent:0 animated:NO];
// 滾動(dòng)到指定cell
NSIndexPath *indexPath = [NSIndexPath indexPathForItem:_currentScreening inSection:0];
[self.collectionView scrollToItemAtIndexPath:indexPath atScrollPosition:UICollectionViewScrollPositionCenteredHorizontally animated:animated];
}
- (void)isFilmBtnEnableWithButton:(UIButton *)button condition:(NSInteger)index{
if (_currentScreening == index) {
button.userInteractionEnabled = NO;
button.alpha = 0.4;
}else {
button.userInteractionEnabled = YES;
button.alpha = 1;
}
}
5. 滾動(dòng)collectionView扣孟,顯示對(duì)應(yīng)場(chǎng)次烫堤,且控制collectionView滾動(dòng)不足半個(gè)屏幕的時(shí)候跳回到上一個(gè),滾動(dòng)超過(guò)半個(gè)屏幕跳到下一個(gè):
- (void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset {
// 獲取滾動(dòng)的當(dāng)前位置
CGPoint originalTargetContentOffset = CGPointMake(targetContentOffset->x, targetContentOffset->y);
CGPoint targetCenter = CGPointMake(originalTargetContentOffset.x + CGRectGetWidth(self.collectionView.bounds)/2, CGRectGetHeight(self.collectionView.bounds) / 2);
NSIndexPath *indexPath = nil;
NSInteger i = 0;
while (indexPath == nil) {
targetCenter = CGPointMake(originalTargetContentOffset.x + CGRectGetWidth(self.collectionView.bounds)/2 + 10*i, CGRectGetHeight(self.collectionView.bounds) / 2);
indexPath = [self.collectionView indexPathForItemAtPoint:targetCenter];
i++;
}
_currentScreening = indexPath.item;
// 設(shè)置對(duì)應(yīng)顯示
_chosenTimeStr = self.chooseTimes[_currentScreening];
[self.chooseTimeBtn setTitle:_chosenTimeStr forState:UIControlStateNormal];
[self isFilmBtnEnableWithButton:self.lastFilmBtn condition:0];
[self isFilmBtnEnableWithButton:self.nextFilmBtn condition:self.movieHallSeatModels.count-1];
[self.timePickerView selectRow:_currentScreening inComponent:0 animated:NO];
// 調(diào)轉(zhuǎn)到指定位置
UICollectionViewLayoutAttributes *attributes = [self.collectionView.collectionViewLayout layoutAttributesForItemAtIndexPath:indexPath];
if (attributes) {
*targetContentOffset = CGPointMake(attributes.center.x - CGRectGetWidth(self.collectionView.bounds)/2, originalTargetContentOffset.y);
}
}