gpt4 book ai didi

ios - UIViewControllerAnimatedTransitioning 每隔一段时间才有效?

转载 作者:行者123 更新时间:2023-11-28 06:12:59 27 4
gpt4 key购买 nike

我已经在这里工作了几个小时,无法理解我做错了什么。我已经按照我之前的 Swift: Problems with custom UIView.transition? 中描述的 UITabBarControllerDelegate 设置了自定义选项卡栏 Controller 转换。

我不使用普通的 Storyboard 标签栏按钮,我以编程方式切换 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
})


}
}

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

最佳答案

我认为您没有分配 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)
})
}

关于ios - UIViewControllerAnimatedTransitioning 每隔一段时间才有效?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45945182/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com