วิธีการเข้าถึงเฟรมของวัตถุ toView ในภาพเคลื่อนไหวที่กำหนดเอง

ฉันต้องการสร้างแอนิเมชั่นเช่นโฟลเดอร์หน้าจอหลักของ iOS และฉันต้องรู้เฟรมของตำแหน่งสุดท้ายของ "โฟลเดอร์":จับภาพหน้าจอของมุมมองสุดท้าย

ฉันใช้ทรานซิชั่นแบบกำหนดเอง และนี่คือโค้ดของไฟล์แอนิเมชัน:

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)
        }

    }
}

ทั้งหมดทำงานได้อย่างถูกต้อง ยกเว้น let finalFrame = containerView.convert(toViewC.containerView.frame, from: toViewC.view) ที่ตั้งค่า finalFrame (เป็นตัวแปร CGRect) เป็น (origin = (x = 0, y = 0), size = (width = 0, height = 0))

ฉันได้ติดตามบทช่วยสอนนี้ โดยเขียนโค้ดใน Swift

แล้วจะเข้าถึงทรัพย์สินได้อย่างถูกต้องได้อย่างไร?


person alessionossa    schedule 13.07.2016    source แหล่งที่มา


คำตอบ (1)


เฟรมเป็นศูนย์ตรงเนื่องจาก ณ จุดที่คุณกำลังเข้าถึงเฟรมนั้น ไม่มีการผ่านโครงร่างของมุมมอง หลังจากที่คุณตั้งค่าเฟรม toVC.view แล้ว ให้เพิ่มบรรทัดนี้:

toVC.view.layoutIfNeeded()

ฉันได้เขียนเกี่ยวกับการใช้สแน็ปช็อตในการเปลี่ยนตัวควบคุมมุมมอง ที่นี่ หากคุณสนใจ

person jrturton    schedule 15.07.2016
comment
ฉันต้องการเข้าถึง toViewC.containerView.frame ซึ่งเป็นรูปสี่เหลี่ยมผืนผ้าของโฟลเดอร์ ดังที่คุณเห็นได้จากภาพหน้าจอ ฉันลบการแปลงแล้ว แต่มันใช้งานไม่ได้... - person alessionossa; 15.07.2016
comment
แต่หากฉันส่ง false ไปที่ completeTransition ฉันจะกลับไปที่การดูครั้งแรก - person alessionossa; 18.07.2016
comment
ฮึฉันได้ทำงานที่แย่มากกับคำตอบนี้เพื่อคุณ ขออภัย ฉันก็ผิดเกี่ยวกับเรื่องนั้นเช่นกัน - person jrturton; 18.07.2016