Saya ingin membuat viewController saya diabaikan ke viewController lama dengan animasi. Tetapi ketika saya menggunakan CATransition viewController dan viewController lama saya memudar menjadi hitam. Apakah ada cara agar saya tidak memudar?

let transition = CATransition()
transition.duration = 1.0
transition.type = kCATransitionPush
transition.subtype = kCATransitionFromLeft
view.window!.layer.add(transition, forKey: kCATransition)
self.dismiss(animated: false, completion: nil)
2
Dojyouge 31 Desember 2017, 10:34

1 menjawab

Jawaban Terbaik

Baca selengkapnya tentang transisi di sini dan mencoba untuk mengubah parameter yang tersedia untuk mencapai efek yang diperlukan.

Info bagus yang tidak terdokumentasi tentang transisi tersedia di sini.

Jika Anda menginginkan solusi yang baik - Anda harus menyiapkan transisi khusus untuk layar Anda - periksa Video WWDC

Solusi cepat alternatif (tetapi bukan yang terbaik) mungkin berupa kontrol manual dari presentasi/singkirkan di viewController Anda:

import UIKit

final class AnimatableViewController: UIViewController {

    private var panStartLocation:CGPoint = CGPoint.zero
    private var panPrevLocation:CGPoint = CGPoint.zero

    var onClose:(()->())?

    // MARK: - LifeCycle

    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)

        openAnimation()
    }

    // MARK: - IBAction

    @objc private func closeButtonAction(_ sender: UIButton) {
        dismiss(animated: true, completion: { [weak self] in
            self?.onClose?()
        })
    }

    // MARK: - Gestures

    @IBAction private func didDetectPan(_ sender: UIPanGestureRecognizer) {
        switch sender.state {
            case .began:
                panStartLocation = sender.location(in: navigationController?.view)
                panPrevLocation = panStartLocation
            case .cancelled, .ended:
                finishAnimation()
            case .changed:
                let newPoint:CGPoint = sender.location(in: navigationController?.view)
                let dy:CGFloat = newPoint.y - panPrevLocation.y
                var newPosition:CGPoint = view.center

                if (newPoint.y - panStartLocation.y > 0) {
                    newPosition.y += dy
                } else {
                    newPosition.y = self.view.bounds.height / 2
                }
                view.center = newPosition
                panPrevLocation = newPoint
            default:
                break
        }
    }

    //MARK: Animation

    private func finishAnimation() {
        let center:CGPoint = view.center
        if (center.y > self.view.bounds.height / 2 * 1.35) {
            closeAnimation()
        } else {
            openAnimation()
        }
    }

    private func closeAnimation() {
        let fullAnimTime:CGFloat = 0.5
        let endPoint:CGPoint = CGPoint(x:view.center.x, y:view.bounds.height / 2 * 3)
        let animTime:CGFloat = ((endPoint.y - view.center.y) / self.view.bounds.height) * fullAnimTime

        UIView.animate(withDuration: TimeInterval(animTime), animations: { [weak self] in
            self?.view.center = endPoint
            }, completion: { [weak self] _ in
                self?.dismiss(animated: false, completion: {
                    self?.onClose?()
                })
        })
    }

    private func openAnimation() {
        let fullAnimTime:CGFloat = 0.5
        let endPoint:CGPoint = CGPoint(x: view.center.x, y:self.view.bounds.height / 2)
        let animTime:CGFloat = ((view.center.y - endPoint.y) / (self.view.bounds.height / 2)) * fullAnimTime
        UIView.animate(withDuration: TimeInterval(animTime)) { [weak self] in
            self?.view.center = endPoint
        }
    }
}

extension AnimatableViewController: UIGestureRecognizerDelegate {
    // MARK: - UIGestureRecognizerDelegate

    func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer) -> Bool {
        if gestureRecognizer is UIPanGestureRecognizer && otherGestureRecognizer is UIPanGestureRecognizer {
            return false
        }
        return true
    }
}

Di storyboard jangan lupa untuk mengatur parameter segue Anda sebagai:

enter image description here

Dan tambahkan panGesture ke tampilan root Anda atau Gesture pan tambahan apa pun ke tampilan paling atas, yang dapat digunakan untuk pan - dalam kasus saya - saya hanya menambahkan pan ke rootView:

enter image description here

Hasil:

enter image description here

-1
hbk 31 Desember 2017, 08:08