【问题标题】:UIViewControllerAnimatedTransitioning only works every other time?UIViewControllerAnimatedTransitioning 每隔一段时间才有效吗?
【发布时间】:2017-08-29 17:36:35
【问题描述】:

我已经在这几个小时了,无法理解我做错了什么。我已经按照我之前的Swift: Problems with custom UIView.transition? 中描述的UITabBarControllerDelegate 装配了自定义标签栏控制器转换

我不使用普通的故事板标签栏按钮,我以编程方式切换 selectedIndex。我的问题是,只有实现了这个:

func tabBarController(_ tabBarController: UITabBarController, animationControllerForTransitionFrom fromVC: UIViewController, to toVC: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        let animator = ModalTransition()
        animator.fromView = fromVC.view
        animator.toView = toVC.view
        return  animator

    }

索引的动画和切换仅每隔一次发生一次。我有自定义按钮来切换索引,每隔一段时间,当我单击切换按钮时没有任何反应。这是我的动画:

//
//  ModalTransition.swift
//  Adventures In Design
//
//  Created by Skylar Thomas on 8/28/17.
//

import UIKit

class ModalTransition: NSObject, UIViewControllerAnimatedTransitioning {

    weak var transitionContext: UIViewControllerContextTransitioning?
    var fromView = UIView()
    var toView = UIView()
    var duration = 1.1


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

    func animateTransition(using transitionContext: UIViewControllerContextTransitioning) {

        let containerView = transitionContext.containerView
        containerView.addSubview(toView)
        containerView.sendSubview(toBack: toView)

        print("ANIMATING")

        UIView.animate(withDuration: duration, delay: 0.0, usingSpringWithDamping: 0.5, initialSpringVelocity: 0.0, options: .curveLinear, animations: {
            self.fromView.center.y += 900

        }, completion: {
            finished in

            //only works every OTHER click
            transitionContext.completeTransition(!transitionContext.transitionWasCancelled)
            self.fromView.center.y -= 900
        })


    }
}

这是什么原因造成的?是什么东西吗

【问题讨论】:

    标签: ios swift swift3 uitabbarcontroller transition


    【解决方案1】:

    我认为你没有分配你的 toView 和 FromView。试试这样的

    func animateTransition(using transitionContext: UIViewControllerContextTransitioning) {
    
        let fromViewController = transitionContext.viewController(forKey: UITransitionContextViewControllerKey.from)!
        let toViewController = transitionContext.viewController(forKey: UITransitionContextViewControllerKey.to)!
    
        let fromView = fromViewController.view
        let toView = toViewController.view
        let container = transitionContext.containerView
        container.addSubview(toView!)
    
        // Replace your animations here
        toView?.frame = transitionContext.finalFrame(for: toViewController)
        toView?.alpha = 0
    
        let duration = self.transitionDuration(using: transitionContext)
    
        UIView.animate(withDuration: duration, delay: 0, options: .curveEaseInOut, animations: {
          toView?.alpha = 1
          fromView?.alpha = 0
    
        }, completion: { finished in
    
          toView?.alpha = 1.0
          fromView?.alpha = 1
          fromView?.removeFromSuperview()
          transitionContext.completeTransition(true)
        })
      }
    

    【讨论】:

    • 仍然只适用于其他所有。你能展示一个动画例子吗?也许我的动画不正确
    • 当然..检查编辑的答案。让我知道是否有帮助
    • 里面的东西修复了它。谢谢!
    猜你喜欢
    • 2011-04-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-16
    • 2011-01-25
    • 2010-10-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多