iOS swift : Custom interactive transition

标签 ios swift uiviewanimationtransition

我编写了一个自定义的滑动过渡,它在模态演示中运行良好。但在推送演示中,“目标” View 位置不是动画的。

我尝试过用 alpha 切换翻译的相同代码,它有效。

from View 工作完美,只是 to View 在动画过程中保持固定。

func transitionAnimator(using transitionContext: UIViewControllerContextTransitioning) -> UIViewImplicitlyAnimating {
    let duration = transitionDuration(using: transitionContext)
    let container = transitionContext.containerView

    let toController = transitionContext.viewController(forKey: .to)
    toController?.beginAppearanceTransition(true, animated: true)

    guard
    let to = transitionContext.view(forKey: .to),
    let from = transitionContext.view(forKey: .from)
        else {
        print("To or from view are nil!")
        fatalError()
    }

    container.addSubview(to)

    let animator = UIViewPropertyAnimator(duration: duration, curve: .linear)
    var toStartingPoint: CGPoint
    var fromEndingPoint: CGPoint

    switch self.from {
    case .down:
        toStartingPoint = CGPoint(x: 0, y: -from.bounds.height)
        fromEndingPoint = CGPoint(x: 0, y: from.bounds.height)
    case .top:
        toStartingPoint = CGPoint(x: 0, y: from.bounds.height)
        fromEndingPoint = CGPoint(x: 0, y: -from.bounds.height)
    case .right:
        toStartingPoint = CGPoint(x: from.bounds.width, y: 0)
        fromEndingPoint = CGPoint(x: -from.bounds.width, y: 0)
    case .left:
        toStartingPoint = CGPoint(x: -from.bounds.width, y: 0)
        fromEndingPoint = CGPoint(x: from.bounds.width, y: 0)
    }

    to.transform = CGAffineTransform(translationX: toStartingPoint.x, y: toStartingPoint.y)

    animator.addAnimations({
        from.transform = CGAffineTransform(translationX: fromEndingPoint.x, y: fromEndingPoint.y)
    }, delayFactor: 0.0)

    animator.addAnimations({
        to.transform = .identity
    }, delayFactor: 0.0)


    animator.addCompletion { [weak self] position in
        switch position {
        case .start:
            self?.auxCancelCompletion?()
            transitionContext.completeTransition(false)
            self?.auxAnimationsCancel?()
        case .end:
            self?.auxEndCompletion?()
            transitionContext.completeTransition(!transitionContext.transitionWasCancelled)
            from.transform = .identity
            to.transform = .identity
        default:
            transitionContext.completeTransition(false)
            self?.auxAnimationsCancel?()
        }
    }

    if let auxAnimations = auxAnimations {
        animator.addAnimations(auxAnimations)
    }

    self.animator = animator
    self.context = transitionContext

    animator.addCompletion { [unowned self] _ in
        self.animator = nil
        self.context = nil
    }
    animator.isUserInteractionEnabled = true

    return animator
}

我认为这是关于委托(delegate)的问题,但 navigationDelgate 设置正确,否则我认为我不会看到任何动画..

委托(delegate)设置:

override func viewDidLoad() {
    super.viewDidLoad()
    transitionHelper = SwipeInteractiveTransitionHelper(withDelegate: self)
}


extension TodayViewController: UINavigationControllerDelegate {

    func navigationController(_ navigationController: UINavigationController, animationControllerFor operation: UINavigationController.Operation, from fromVC: UIViewController, to toVC: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        return transitionHelper?.swipeTransition
    }

    func navigationController(_ navigationController: UINavigationController, interactionControllerFor animationController: UIViewControllerAnimatedTransitioning) -> UIViewControllerInteractiveTransitioning? {
        return transitionHelper?.swipeTransition
    }
}

这是自定义推送协调器,其中 viewController 是下一个 View Controller ,也是我附加委托(delegate)的位置。

    case .pushCustom:
        guard let navigationController = currentViewController.navigationController else {
            fatalError("Can't push a view controller without a current navigation controller")
        }
        guard let current = currentViewController as? UINavigationControllerDelegate else {
            fatalError("Can't push a view controller without a current  navigation delegate")
        }
        navigationController.delegate = current
        navigationController.pushViewController(viewController, animated: true) { [weak self] in
            self?.currentViewController = SceneCoordinator.actualViewController(for: viewController)
            completion?()
        }

最佳答案

解决了对目标 View 的快照进行动画处理的问题,而不是直接对目标 View 进行动画处理。

let to =transitionContext.view(forKey: .to) 让 toViewSnapshot = to.snapshotView(afterScreenUpdates: true)

只需使用 toViewSnapshot 进行动画

关于iOS swift : Custom interactive transition,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54733151/

相关文章:

ios - SpriteKit 中滑动旋转 Sprite 节点

ios - 动画约束时高度 anchor 的自动布局错误

iOS - Google 登录库问题

ios - 检查 UITextField 中的更改

ios - Alamofire 忽略设置/处理数据的闭包

swift - 如何在 swift 中为浮点值编写通用函数

ios - 如何为导航 Controller 提供自定义 UIViewControllerAnimatedTransitioning 和使用默认 UIViewControllerInteractiveTransitioning

ios - 自定义转场崩溃

objective-c - 使用 Erica Sadun 示例的 UIImageView 调整大小和旋转问题 - 预填充位置

ios - 哪个 API 可以从 ios 和 osx 中的缓冲区播放音频?