我在center
函数中有viewWIllTransition
个collectionview单元格如下
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)!)")
}
}
})}
据我所知,在设备旋转之前,上述内容无法实现。但我想通过center
中的didSelectItemAt indexpath
作为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函数。那么有没有办法在viewWillTransition
中调用didSelectItemAt indexpath
,以便在cell
为selected
时调用它,具体取决于它所在的orientation
?