【问题标题】:Hide a black fade when self.dismiss with CATransition使用 CATransition 关闭 self.dismiss 时隐藏黑色渐变
【发布时间】:2017-12-31 07:34:26
【问题描述】:

我想让我的 viewController 关闭到带有动画的旧 viewController。但是当我使用 CATransition 时,我的 viewController 和旧的 viewController 会变黑。有什么办法让它不掉色吗?

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)

【问题讨论】:

  • 你对fades black 意味着什么?我试过这样的代码,但我没有注意到类似的东西

标签: ios iphone swift swift4 catransition


【解决方案1】:

阅读更多关于transition here的信息并尝试修改可用参数以达到所需的效果。

关于过渡available here的良好无证信息。

如果你想要一些好的解决方案 - 你应该为你的屏幕准备自定义转换 - 检查这个WWDC video

另一种快速(但不是最好的)解决方案可能是在您的 viewController 中手动控制显示/关闭:

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

在情节提要中不要忘记将 segue 参数设置为:

并将 panGesture 添加到您的根视图或任何其他平移手势到最顶部的视图,可用于平移 - 在我的情况下 - 我只是将平移添加到 rootView:

结果:

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-23
    • 2017-03-26
    • 2016-08-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多