按鈕點(diǎn)擊 / collectionView滾動(dòng)切換 / pickerView的互相聯(lián)動(dòng)

需求:中間紅色框?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í)間

效果.jpg
點(diǎn)擊時(shí)間岛都,彈出pickerView與遮罩.jpg

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);
    }
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末哈打,一起剝皮案震驚了整個(gè)濱河市塔逃,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌料仗,老刑警劉巖湾盗,帶你破解...
    沈念sama閱讀 217,657評(píng)論 6 505
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場(chǎng)離奇詭異立轧,居然都是意外死亡格粪,警方通過(guò)查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,889評(píng)論 3 394
  • 文/潘曉璐 我一進(jìn)店門氛改,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái)帐萎,“玉大人,你說(shuō)我怎么就攤上這事胜卤〗迹” “怎么了?”我有些...
    開(kāi)封第一講書(shū)人閱讀 164,057評(píng)論 0 354
  • 文/不壞的土叔 我叫張陵葛躏,是天一觀的道長(zhǎng)澈段。 經(jīng)常有香客問(wèn)我,道長(zhǎng)舰攒,這世上最難降的妖魔是什么败富? 我笑而不...
    開(kāi)封第一講書(shū)人閱讀 58,509評(píng)論 1 293
  • 正文 為了忘掉前任,我火速辦了婚禮摩窃,結(jié)果婚禮上兽叮,老公的妹妹穿的比我還像新娘。我一直安慰自己,他們只是感情好鹦聪,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,562評(píng)論 6 392
  • 文/花漫 我一把揭開(kāi)白布账阻。 她就那樣靜靜地躺著,像睡著了一般泽本。 火紅的嫁衣襯著肌膚如雪宰僧。 梳的紋絲不亂的頭發(fā)上,一...
    開(kāi)封第一講書(shū)人閱讀 51,443評(píng)論 1 302
  • 那天观挎,我揣著相機(jī)與錄音,去河邊找鬼段化。 笑死嘁捷,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的显熏。 我是一名探鬼主播雄嚣,決...
    沈念sama閱讀 40,251評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼喘蟆!你這毒婦竟也來(lái)了缓升?” 一聲冷哼從身側(cè)響起,我...
    開(kāi)封第一講書(shū)人閱讀 39,129評(píng)論 0 276
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤蕴轨,失蹤者是張志新(化名)和其女友劉穎港谊,沒(méi)想到半個(gè)月后,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體橙弱,經(jīng)...
    沈念sama閱讀 45,561評(píng)論 1 314
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡歧寺,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,779評(píng)論 3 335
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了棘脐。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片斜筐。...
    茶點(diǎn)故事閱讀 39,902評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖蛀缝,靈堂內(nèi)的尸體忽然破棺而出顷链,到底是詐尸還是另有隱情,我是刑警寧澤屈梁,帶...
    沈念sama閱讀 35,621評(píng)論 5 345
  • 正文 年R本政府宣布嗤练,位于F島的核電站,受9級(jí)特大地震影響俘闯,放射性物質(zhì)發(fā)生泄漏潭苞。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,220評(píng)論 3 328
  • 文/蒙蒙 一真朗、第九天 我趴在偏房一處隱蔽的房頂上張望此疹。 院中可真熱鬧,春花似錦、人聲如沸蝗碎。這莊子的主人今日做“春日...
    開(kāi)封第一講書(shū)人閱讀 31,838評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)蹦骑。三九已至慈省,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間眠菇,已是汗流浹背边败。 一陣腳步聲響...
    開(kāi)封第一講書(shū)人閱讀 32,971評(píng)論 1 269
  • 我被黑心中介騙來(lái)泰國(guó)打工, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留捎废,地道東北人笑窜。 一個(gè)月前我還...
    沈念sama閱讀 48,025評(píng)論 2 370
  • 正文 我出身青樓,卻偏偏與公主長(zhǎng)得像登疗,于是被迫代替她去往敵國(guó)和親排截。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,843評(píng)論 2 354

推薦閱讀更多精彩內(nèi)容

  • 發(fā)現(xiàn) 關(guān)注 消息 iOS 第三方庫(kù)辐益、插件断傲、知名博客總結(jié) 作者大灰狼的小綿羊哥哥關(guān)注 2017.06.26 09:4...
    肇東周閱讀 12,103評(píng)論 4 62
  • 翻譯自“Collection View Programming Guide for iOS” 0 關(guān)于iOS集合視...
    lakerszhy閱讀 3,864評(píng)論 1 22
  • 緊緊抓住關(guān)鍵時(shí)刻
    固執(zhí)的魚(yú)閱讀 190評(píng)論 0 1
  • 聲音 是你的敲門磚 形態(tài) 是別人愿意停下來(lái)的理由 內(nèi)容 是愿意聽(tīng)下去的重點(diǎn) 直擊別人需求 這就是我的三部曲,...
    柚丠丠閱讀 225評(píng)論 2 2
  • 我的成長(zhǎng)是在挫折上過(guò)來(lái)的智政,而且我是一個(gè)打擊越大越自信的人认罩,有人說(shuō)誰(shuí)不是在挫折中成長(zhǎng)的,確實(shí)人都在挫折中成長(zhǎng)续捂,但忽略...
    Mr_Leekon閱讀 292評(píng)論 0 0