在didSelectItemAt索引路径中调用viewWillTransition

时间:2018-01-17 14:35:40

标签: ios swift uicollectionview viewwilltransitiontosize

我在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,以便在cellselected时调用它,具体取决于它所在的orientation

0 个答案:

没有答案