我center
的collectionview单元的viewWIllTransition
功能如下
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
在的位置?