1

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?

Coder221
  • 1,403
  • 2
  • 19
  • 35
  • Did you check this question https://stackoverflow.com/questions/42170373/swift-viewwilltransition-not-called ? – Mauricio Chirino Jan 17 '18 at 14:49
  • Yes, that wont work for me because viewDidLayout will not perform updates during rotation. – Coder221 Jan 17 '18 at 14:51
  • @Coder221 Is there any problem if you move code inside `viewWillTransition` to `didSelectItemAt ` and using UIView-based animations instead of `UIViewControllerTransitionCoordinator` animate method ? – trungduc Jan 17 '18 at 15:30
  • @trungduc, Transition works if I move the code inside but center will not get updated when I rotate the device. All I am looking is for the center to get updated whenever the device rotates so that the transition will start and end at the center. That is the reason I was using viewWillTransition. Is it clear? – Coder221 Jan 17 '18 at 15:48
  • @Coder221 I mean keep the code inside `viewWillTransition`. Copy this code to `didSelectItemAt` and use UIView-based animations. – trungduc Jan 17 '18 at 15:49
  • That's what I did, it wont work and what is meaning of UIView-based animations instead of UIViewControllerTransitionCoordinator animate method? – Coder221 Jan 17 '18 at 15:57
  • @Coder221I mean using this method https://developer.apple.com/documentation/uikit/uiview/1622574-transition – trungduc Jan 17 '18 at 17:37

0 Answers0