UICollection 實現分頁橫向滾動到屏幕中央

參考鏈接

  • 創(chuàng)建一個UICollectionView
- (void)viewDidLoad {
    [super viewDidLoad];
    
    CGFloat x = 0.0f;
    CGFloat y = 60.0f;
    CGFloat width = self.view.frame.size.width;
    CGFloat height = 200.0f;
    
    _flowLayout = [[UICollectionViewFlowLayout alloc] init];
    _flowLayout.minimumLineSpacing = 10.0f;
    _flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    _flowLayout.itemSize = CGSizeMake(width - 50.0f, height - 20.0f);
//    _flowLayout.sectionInset = UIEdgeInsetsMake(10.0f, 10.0f, 10.0f, 10.0f);
    
    _collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(x, y, width, height) collectionViewLayout:_flowLayout];
    _collectionView.backgroundColor = [UIColor orangeColor];
    _collectionView.delegate = self;
    _collectionView.dataSource = self;
    _collectionView.pagingEnabled = NO;
    [_collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:CollectionCellIdentifier];
    [self.view addSubview:_collectionView];
    
}
  • 數據源代理
#pragma mark - UICollectionViewDataSource
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return 20;
}

- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:CollectionCellIdentifier forIndexPath:indexPath];
    
    cell.backgroundColor = [UIColor colorWithRed:arc4random_uniform(255)/255.0 green:arc4random_uniform(255)/255.0 blue:arc4random_uniform(255)/255.0 alpha:1.0f];
    
    return cell;
}
  • 關鍵代碼 UIScrollViewDelegate的一個方法
- (void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset
{
    if (![scrollView isKindOfClass:[UICollectionView class]]) {
        return;
    }
    UICollectionView *collectionView = (UICollectionView *)scrollView;
    
    CGPoint originalTargetContentOffset = CGPointMake(targetContentOffset->x, targetContentOffset->y);
    CGPoint targetCenter = CGPointMake(originalTargetContentOffset.x + CGRectGetWidth(collectionView.bounds)/2, CGRectGetHeight(collectionView.bounds) / 2);
    NSIndexPath *indexPath = nil;
    while (indexPath == nil) {
        targetCenter = CGPointMake(originalTargetContentOffset.x + CGRectGetWidth(collectionView.bounds)/2, CGRectGetHeight(collectionView.bounds) / 2);
        indexPath = [collectionView indexPathForItemAtPoint:targetCenter];
    }
    
    // 這里用attributes比用cell要好很多,因為cell可能因為不在屏幕范圍內導致cellForItemAtIndexPath返回nil
    UICollectionViewLayoutAttributes *attributes = [collectionView.collectionViewLayout layoutAttributesForItemAtIndexPath:indexPath];
    if (attributes) {
        
        *targetContentOffset = CGPointMake(attributes.center.x - CGRectGetWidth(collectionView.bounds)/2, originalTargetContentOffset.y);
    } else {
        NSLog(@"center is %@; indexPath is {%@, %@}; cell is %@",NSStringFromCGPoint(targetCenter), @(indexPath.section), @(indexPath.item), attributes);
    }
    NSLog(@"indexPath.row - %ld",(long)indexPath.row);
    
}
?著作權歸作者所有,轉載或內容合作請聯系作者
  • 序言:七十年代末秩伞,一起剝皮案震驚了整個濱河市,隨后出現的幾起案子脸爱,更是在濱河造成了極大的恐慌,老刑警劉巖歪赢,帶你破解...
    沈念sama閱讀 221,635評論 6 515
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件白对,死亡現場離奇詭異,居然都是意外死亡媳拴,警方通過查閱死者的電腦和手機,發(fā)現死者居然都...
    沈念sama閱讀 94,543評論 3 399
  • 文/潘曉璐 我一進店門小压,熙熙樓的掌柜王于貴愁眉苦臉地迎上來怠益,“玉大人烤咧,你說我怎么就攤上這事〖⒛裕” “怎么了?”我有些...
    開封第一講書人閱讀 168,083評論 0 360
  • 文/不壞的土叔 我叫張陵,是天一觀的道長。 經常有香客問我,道長清焕,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 59,640評論 1 296
  • 正文 為了忘掉前任最盅,我火速辦了婚禮,結果婚禮上督函,老公的妹妹穿的比我還像新娘灌灾。我一直安慰自己些己,他們只是感情好炉奴,可當我...
    茶點故事閱讀 68,640評論 6 397
  • 文/花漫 我一把揭開白布砸逊。 她就那樣靜靜地躺著司倚,像睡著了一般拍柒。 火紅的嫁衣襯著肌膚如雪种呐。 梳的紋絲不亂的頭發(fā)上爽室,一...
    開封第一講書人閱讀 52,262評論 1 308
  • 那天,我揣著相機與錄音,去河邊找鬼。 笑死,一個胖子當著我的面吹牛,可吹牛的內容都是我干的。 我是一名探鬼主播,決...
    沈念sama閱讀 40,833評論 3 421
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了?” 一聲冷哼從身側響起,我...
    開封第一講書人閱讀 39,736評論 0 276
  • 序言:老撾萬榮一對情侶失蹤粤铭,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后,有當地人在樹林里發(fā)現了一具尸體,經...
    沈念sama閱讀 46,280評論 1 319
  • 正文 獨居荒郊野嶺守林人離奇死亡是钥,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 38,369評論 3 340
  • 正文 我和宋清朗相戀三年鸥鹉,在試婚紗的時候發(fā)現自己被綠了祝蝠。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 40,503評論 1 352
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖,靈堂內的尸體忽然破棺而出,到底是詐尸還是另有隱情,我是刑警寧澤,帶...
    沈念sama閱讀 36,185評論 5 350
  • 正文 年R本政府宣布,位于F島的核電站,受9級特大地震影響,放射性物質發(fā)生泄漏。R本人自食惡果不足惜股耽,卻給世界環(huán)境...
    茶點故事閱讀 41,870評論 3 333
  • 文/蒙蒙 一震嫉、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧证芭,春花似錦蝇完、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,340評論 0 24
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽道伟。三九已至蜜徽,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間盆色,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,460評論 1 272
  • 我被黑心中介騙來泰國打工叛薯, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人。 一個月前我還...
    沈念sama閱讀 48,909評論 3 376
  • 正文 我出身青樓,卻偏偏與公主長得像惨好,于是被迫代替她去往敵國和親矩乐。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 45,512評論 2 359

推薦閱讀更多精彩內容

  • Swift1> Swift和OC的區(qū)別1.1> Swift沒有地址/指針的概念1.2> 泛型1.3> 類型嚴謹 對...
    cosWriter閱讀 11,111評論 1 32
  • 因為要結局swift3.0中引用snapKit的問題,看到一篇介紹Xcode8,swift3變化的文章,覺得很詳細...
    uniapp閱讀 4,422評論 0 12
  • 國家電網公司企業(yè)標準(Q/GDW)- 面向對象的用電信息數據交換協(xié)議 - 報批稿:20170802 前言: 排版 ...
    庭說閱讀 11,004評論 6 13
  • 翻譯自“Collection View Programming Guide for iOS” 0 關于iOS集合視...
    lakerszhy閱讀 3,872評論 1 22
  • 葉芽昨天拉了5次屎嘁字,今天到目前為止拉了4次纯续。每次又多又臭茸歧,一屁股的屎比較難清理铜涉。熏得我和她爸都快受不了了纹烹。 葉芽是...
    青青葉芽閱讀 198評論 0 0