Call viewWillTransition in didSelectItemAt indexpath

梦想的初衷 提交于 2019-12-11 05:09:30

问题


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?

来源:https://stackoverflow.com/questions/48303609/call-viewwilltransition-in-didselectitemat-indexpath

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!