在didSelectItemAt索引路径中调用viewWillTransition

我在viewWIllTransition函数中有collectionview单元的center ,如下所示

 var center = CGPoint() override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) { super.viewWillTransition(to: size, with: coordinator) coordinator.animate(alongsideTransition: { (context) -> Void in if (UIDevice.current.orientation == UIDeviceOrientation.portrait) { if let indexPath = self.categoryCV.indexPathsForSelectedItems?.first { print("Portrait") let attributes: UICollectionViewLayoutAttributes? = self.categoryCV.layoutAttributesForItem(at: indexPath) self.center = (attributes?.center)! print("Center: \((attributes?.center)!)") } } else { if let indexPath = self.categoryCV.indexPathsForSelectedItems?.first { print("Landscape") let attributes: UICollectionViewLayoutAttributes? = self.categoryCV.layoutAttributesForItem(at: indexPath) self.center = (attributes?.center)! print("Center: \((attributes?.center)!)") } } })} 

据我所知,在设备旋转之前,上述内容无法实现。 但我喜欢将didSelectItemAt indexpathcenter作为transition.start = self.center

 func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) { let detailViewController = self.storyboard?.instantiateViewController(withIdentifier: "TVC") as? ThirdViewController // other code transition.start = self.center print("outside center: \(self.center)") } 

这是因为我希望转换从单元格中心开始,但是直到我旋转设备才会调用viewWillTransition函数。 那么有没有办法在didSelectItemAt indexpath路径中调用viewWillTransition ,以便在selected cell时根据它的orientation调用它?