У меня есть 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
, в котором он находится?
viewWillTransition
вdidSelectItemAt
и используете анимацию на основе UIView вместо метода анимацииUIViewControllerTransitionCoordinator
? - person trungduc   schedule 17.01.2018viewWillTransition
. Скопируйте этот код вdidSelectItemAt
и используйте анимацию на основе UIView. - person trungduc   schedule 17.01.2018