Call viewWillTransition in didSelectItemAt indexpath

323 views Asked by At

I have center of collectionview cell in viewWIllTransition function as follows

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)!)")
      }
        }
        })}

I understand that the above will not get implemented until the device rotates. But I like to pass the center in didSelectItemAt indexpath as 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)")

}

This is because I want the transition to start from center of cell but viewWillTransition function is not getting called until I rotate the device. So is there a way to call viewWillTransition in didSelectItemAt indexpath so that it gets called when cell is selected depending on the orientation it is in?

0

There are 0 answers