Bagaimana mengakses bingkai objek toView dalam animasi khusus

Saya ingin membuat animasi seperti folder layar beranda iOS, dan saya harus mengetahui bingkai posisi akhir "folder":tangkapan layar tampilan akhir

Saya menggunakan transisi khusus dan berikut adalah kode file animasinya:

class FolderAnimationController: NSObject, UIViewControllerAnimatedTransitioning {

    let duration    = 5.0
    var presenting  = true

    func transitionDuration(_ transitionContext: UIViewControllerContextTransitioning?) -> TimeInterval {
        return duration
    }

    func animateTransition(_ transitionContext: UIViewControllerContextTransitioning) {

        let containerView = transitionContext.containerView()
        let toViewC = transitionContext.viewController(forKey: UITransitionContextToViewControllerKey)! as! ProjectViewController
        let fromViewC = transitionContext.viewController(forKey: UITransitionContextFromViewControllerKey)!as! ViewController

        let cell : FolderCollectionViewCell = fromViewC.folderCollectionView.cellForItem(at: (fromViewC.folderCollectionView.indexPathsForSelectedItems()?.first!)!) as! FolderCollectionViewCell
        let cellSnapshot: UIView = cell.snapshotView(afterScreenUpdates: false)!
        let cellFrame = containerView.convert(cell.frame, from: cell.superview)
        cellSnapshot.frame = cellFrame
        cell.isHidden = true

        toViewC.view.frame = transitionContext.finalFrame(for: toViewC)
        toViewC.view.alpha = 0
        containerView.addSubview(toViewC.view)
        containerView.addSubview(cellSnapshot)

        UIView.animate(withDuration: duration, animations: {
            toViewC.view.alpha = 1.0

            let finalFrame = containerView.convert(toViewC.containerView.frame, from: toViewC.view)
            cellSnapshot.frame = finalFrame
            }) { (_) in
                cellSnapshot.removeFromSuperview()
                transitionContext.completeTransition(true)
        }

    }
}

Semua berfungsi dengan benar, kecuali let finalFrame = containerView.convert(toViewC.containerView.frame, from: toViewC.view) yang menetapkan nilai finalFrame (adalah variabel CGRect) ke (origin = (x = 0, y = 0), size = (width = 0, height = 0)).

Saya telah mengikuti tutorial ini, menulis kode saya di Swift

Jadi, bagaimana cara mengakses properti dengan benar?


person alessionossa    schedule 13.07.2016    source sumber


Jawaban (1)


Bingkainya adalah persegi nol karena pada titik Anda mengaksesnya, tata letak tampilan belum terjadi. Setelah Anda mengatur bingkai toVC.view, tambahkan baris ini:

toVC.view.layoutIfNeeded()

Saya telah menulis tentang penggunaan snapshot dalam transisi pengontrol tampilan di sini jika Anda tertarik.

person jrturton    schedule 15.07.2016
comment
Saya ingin mengakses toViewC.containerView.frame, itu adalah bagian kanan folder, seperti yang Anda lihat di tangkapan layar. Saya telah menghapus konversi tetapi tidak berhasil... - person alessionossa; 15.07.2016
comment
Tetapi jika saya mengirim false ke completeTransition saya kembali ke tampilan pertama saya - person alessionossa; 18.07.2016
comment
Ugh, saya telah melakukan pekerjaan yang buruk dalam jawaban ini untuk Anda. Maaf, aku juga salah dalam hal itu. - person jrturton; 18.07.2016