【问题标题】:How to make transition of a ViewController from Bottom to Top?如何使 ViewController 从底部过渡到顶部?
【发布时间】:2020-03-20 14:45:28
【问题描述】:

所以这里有一个用于幻灯片转换的类,它添加了一个带有从左到右动画的 ViewController,它完美地工作我想要从底部到顶部的过渡。

import UIKit

class SlideInTransition: NSObject, UIViewControllerAnimatedTransitioning {

    var isPresenting = false
    let dimmingView = UIView()

    func transitionDuration(using transitionContext: UIViewControllerContextTransitioning?) -> TimeInterval {
        return 0.3
    }

    func animateTransition(using transitionContext: UIViewControllerContextTransitioning) {

        guard let toViewController = transitionContext.viewController(forKey: .to),
            let fromViewController = transitionContext.viewController(forKey: .from) else { return }

        let containerView = transitionContext.containerView

        let finalWidth = toViewController.view.bounds.width * 0.8
        let finalHeight = toViewController.view.bounds.height

        if isPresenting {
            // Add dimming view
            dimmingView.backgroundColor = .black
            dimmingView.alpha = 0.0
            containerView.addSubview(dimmingView)
            dimmingView.frame = containerView.bounds
            // Add menu view controller to container
            containerView.addSubview(toViewController.view)

            // Init frame off the screen
            toViewController.view.frame = CGRect(x: -finalWidth, y: 0, width: finalWidth, height: finalHeight)
        }

        // Move on screen
        let transform = {
            self.dimmingView.alpha = 0.5
            toViewController.view.transform = CGAffineTransform(translationX: finalWidth, y: 0)
        }

        // Move back off screen
        let identity = {
            self.dimmingView.alpha = 0.0
            fromViewController.view.transform = .identity
        }

        // Animation of the transition
        let duration = transitionDuration(using: transitionContext)
        let isCancelled = transitionContext.transitionWasCancelled
        UIView.animate(withDuration: duration, animations: {
            self.isPresenting ? transform() : identity()
        }) { (_) in
            transitionContext.completeTransition(!isCancelled)
        }
    }

}

说实话,我不久前从某个地方复制了这段代码,但我没有它的来源。

我对 iOS 还很陌生,因此我们将不胜感激。

【问题讨论】:

    标签: ios swift


    【解决方案1】:

    试试这个,

    class SlideInTransition: NSObject, UIViewControllerAnimatedTransitioning {
    
        var isPresenting = false
        let dimmingView = UIView()
    
        func transitionDuration(using transitionContext: UIViewControllerContextTransitioning?) -> TimeInterval {
            return 0.3
        }
    
        func animateTransition(using transitionContext: UIViewControllerContextTransitioning) {
    
            guard let toViewController = transitionContext.viewController(forKey: .to),
                let fromViewController = transitionContext.viewController(forKey: .from) else { return }
    
            let containerView = transitionContext.containerView
    
            let finalWidth = toViewController.view.bounds.width
            let finalHeight = toViewController.view.bounds.height * 0.8
    
            if isPresenting {
                // Add dimming view
                dimmingView.backgroundColor = .black
                dimmingView.alpha = 0.0
                containerView.addSubview(dimmingView)
                dimmingView.frame = containerView.bounds
                // Add menu view controller to container
                containerView.addSubview(toViewController.view)
    
                // Init frame off the screen
                toViewController.view.frame = CGRect(x: 0, y: finalHeight, width: finalWidth, height: finalHeight)
            }
    
            // Move on screen
            let transform = {
                self.dimmingView.alpha = 0.5
                toViewController.view.transform = CGAffineTransform(translationX: 0, y: -finalHeight)
            }
    
            // Move back off screen
            let identity = {
                self.dimmingView.alpha = 0.0
                fromViewController.view.transform = .identity
            }
    
            // Animation of the transition
            let duration = transitionDuration(using: transitionContext)
            let isCancelled = transitionContext.transitionWasCancelled
            UIView.animate(withDuration: duration, animations: {
                self.isPresenting ? transform() : identity()
            }) { (_) in
                transitionContext.completeTransition(!isCancelled)
            }
        }
    }
    

    【讨论】:

    • 在转换中遇到此错误Incorrect argument labels in call (have 'x:translationY:', expected 'translationX:y:')
    • 我已经从下到上更新了,请检查。我希望它有效
    • 现在什么都没有出现。只是因为dimmingView ,视野变暗了。
    • 这可能是因为它的高度,现在试试吧。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-09
    • 2012-06-01
    • 1970-01-01
    相关资源
    最近更新 更多