Ios 在没有DispatchQueue.main.async的情况下,接口不会在willTransition(到:,带:)中更新

Ios 在没有DispatchQueue.main.async的情况下,接口不会在willTransition(到:,带:)中更新,ios,swift,uicollectionview,Ios,Swift,Uicollectionview,我正在尝试处理设备旋转,并且(在旋转过程中)做出一些“努力”将我的collectionView滚动到指定的项目。 所以,如果我在DispatchQueue.main.async()中执行此操作,所有内容都会更新 override func willTransition(to newCollection: UITraitCollection, with coordinator: UIViewControllerTransitionCoordinator) { DispatchQueue

我正在尝试处理设备旋转,并且(在旋转过程中)做出一些“努力”将我的collectionView滚动到指定的项目。 所以,如果我在DispatchQueue.main.async()中执行此操作,所有内容都会更新

 override func willTransition(to newCollection: UITraitCollection, with coordinator: UIViewControllerTransitionCoordinator)
{ 
    DispatchQueue.main.async {    
    let indexPath = IndexPath(item: self.pageControl.currentPage, section: 0)
    self.collectionView.scrollToItem(at: indexPath, at: .centeredHorizontally, animated: true)
  }
}
但我只在willTransition func中执行,不涉及DispatchQueue.main.async()接口不更新,什么也不发生

 override func willTransition(to newCollection: UITraitCollection, with coordinator: UIViewControllerTransitionCoordinator)
{  
    let indexPath = IndexPath(item: self.pageControl.currentPage, section: 0)
    self.collectionView.scrollToItem(at: indexPath, at: .centeredHorizontally, animated: true)
}

为什么??它是什么(Willtransition func上下文中的DispatchQueue?

尝试使用协调器,以便滚动集合视图作为旋转的一部分

override func willTransition(to newCollection: UITraitCollection, with coordinator: UIViewControllerTransitionCoordinator)
{
    coordinator.animate(alongsideTransition: { (context) in
        let indexPath = IndexPath(item: self.pageControl.currentPage, section: 0)
        self.collectionView.scrollToItem(at: indexPath, at: .centeredHorizontally, animated: true)
    }, completion: nil)
}

不使用
DispatchQueue.main.async
时,代码失败的最可能原因是您尝试滚动的时间太短,并且当您使用它时,滚动会延迟足够长的时间以使其生效。

所有显示更新都是在主线程上进行的
DispatchQueue.main.async
告诉iOS在main上执行它thread@GIJOW您只需要显式地将与UI相关的任务分派到主线程,以防函数调用发生在另一个线程上(这里不应该是这种情况,但如果没有更多上下文,就无法确定)更多上下文?当我旋转到横向时,发现collectionViewCell没有放置在中心,所以它尝试将其放置在中心,如果不进行调度,queue.async如果在dispatch.queue.async中执行,则不会发生任何变化。它试图得到它为什么?