Вызов viewWillTransition в didSelectItemAt indexpath

У меня есть center ячейки представления коллекции в функции 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, в котором он находится?


person Coder221    schedule 17.01.2018    source источник
comment
Вы проверили этот вопрос stackoverflow.com/questions/42170373/ ?   -  person Mauricio Chirino    schedule 17.01.2018
comment
Да, это не сработает для меня, потому что viewDidLayout не будет выполнять обновления во время вращения.   -  person Coder221    schedule 17.01.2018
comment
@ Coder221 Coder221 Возникнут ли проблемы, если вы переместите код внутри viewWillTransition в didSelectItemAt и используете анимацию на основе UIView вместо метода анимации UIViewControllerTransitionCoordinator?   -  person trungduc    schedule 17.01.2018
comment
@trungduc, переход работает, если я перемещаю код внутрь, но центр не будет обновляться при повороте устройства. Все, что я ищу, это чтобы центр обновлялся всякий раз, когда устройство вращается, чтобы переход начинался и заканчивался в центре. Вот почему я использовал viewWillTransition. Так понятно?   -  person Coder221    schedule 17.01.2018
comment
@ Coder221 Я имею в виду, хранить код внутри viewWillTransition. Скопируйте этот код в didSelectItemAt и используйте анимацию на основе UIView.   -  person trungduc    schedule 17.01.2018
comment
Это то, что я сделал, это не сработает, и что означает анимация на основе UIView вместо метода анимации UIViewControllerTransitionCoordinator?   -  person Coder221    schedule 17.01.2018
comment
@Coder221Я имею в виду использование этого метода developer.apple.com/documentation/uikit/uiview/   -  person trungduc    schedule 17.01.2018