UICollectionView 滑動(dòng)到指定item失敗

具體場(chǎng)景:

在進(jìn)入某控制器后滑動(dòng)到指定item,我是這樣操作的:

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    self.automaticallyAdjustsScrollViewInsets = NO;
//在這個(gè)方法里面初始化 UICollectionView(代理方法也已實(shí)現(xiàn))  
    [self setupSubviewsContraints];
}


- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:YES];
    
    if (_currentIndex) {
    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:_currentIndex inSection:0];
    [self.collectionView scrollToItemAtIndexPath:indexPath atScrollPosition:UICollectionViewScrollPositionLeft animated:NO];
    }
}

UICollectionView 滑動(dòng)到指定item 失敗原因分析:

UICollectionView 代理方法還沒(méi)有執(zhí)行完成就執(zhí)行如下,滑動(dòng)到某指定item代碼:

 [self.collectionView scrollToItemAtIndexPath:indexPath atScrollPosition:UICollectionViewScrollPositionLeft animated:NO];

所以在執(zhí)行這個(gè)操作前一定要確定代理方法是否已經(jīng)執(zhí)行。

衍生疑惑:

UICollectionView 代理方法在什么時(shí)候開(kāi)始執(zhí)行

解決方法:

想了很久,沒(méi)有什么好的解決辦法,很多情況下進(jìn)入這個(gè)界面會(huì)閃一下。
最終只發(fā)現(xiàn)一種相對(duì)比較流暢的解決方案:

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:YES];
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.01 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        if (_currentIndex) {
            NSIndexPath *indexPath = [NSIndexPath indexPathForRow:_currentIndex inSection:0];
            [self.collectionView scrollToItemAtIndexPath:indexPath atScrollPosition:UICollectionViewScrollPositionLeft animated:NO];
        }
    });
    
}

真的是投機(jī)取巧,可這是目前想到的比較好的一個(gè)解決辦法。
當(dāng)然還有一種方法,看起來(lái)還不錯(cuò):

_collectionView.contentSize = CGSizeMake(self.models.count * (self.view.frame.size.width + 20), 0);
if (_currentIndex) [_collectionView setContentOffset:CGPointMake((self.view.tz_width + 20) * _currentIndex, 0) animated:NO];

目前就只有這兩種方法。

這個(gè)問(wèn)題是在做查看相冊(cè)->預(yù)覽時(shí)候發(fā)現(xiàn)的。

另:
在獲取圖片資源asset時(shí)會(huì)遇到[[info objectForKey:PHImageResultIsDegradedKey] boolValue] Bool值

PHImageRequestID imageRequestID = [[PHImageManager defaultManager] requestImageForAsset:asset targetSize:imageSize contentMode:PHImageContentModeAspectFill options:option resultHandler:^(UIImage * _Nullable result, NSDictionary * _Nullable info) {
}

這個(gè)回調(diào)會(huì)走兩次,Bool = Yes 是低質(zhì)量圖片,Bool = NO則為高質(zhì)量圖片

當(dāng)我只想要高質(zhì)量圖片時(shí),用了if判斷,導(dǎo)致我在由相冊(cè)->預(yù)覽會(huì)閃一下。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

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

  • Android 自定義View的各種姿勢(shì)1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 173,558評(píng)論 25 708
  • 因?yàn)橐Y(jié)局swift3.0中引用snapKit的問(wèn)題,看到一篇介紹Xcode8,swift3變化的文章,覺(jué)得很詳細(xì)...
    uniapp閱讀 4,537評(píng)論 0 12
  • 發(fā)現(xiàn) 關(guān)注 消息 iOS 第三方庫(kù)、插件、知名博客總結(jié) 作者大灰狼的小綿羊哥哥關(guān)注 2017.06.26 09:4...
    肇東周閱讀 12,257評(píng)論 4 61
  • 你會(huì)迷茫嗎?我想問(wèn)你,親愛(ài)的朋友。你不用回答我,你可以反問(wèn)我,我的回答是:“我會(huì)”。 為什么我會(huì)...
    萌者閱讀 321評(píng)論 5 2
  • 如果時(shí)間是一匹馬, 你會(huì)抱怨它的捉迷藏。 有時(shí), 它那白駒的模樣, 溜的連它的尾巴都抓不著。 如果時(shí)間是一匹馬, ...
    好奇是病閱讀 303評(píng)論 5 3