gpt4 book ai didi

ios - 第一次点击按钮时不会播放 segue 的自定义动画

转载 作者:可可西里 更新时间:2023-11-01 03:18:23 27 4
gpt4 key购买 nike

我遇到了一个不寻常的行为,我有点卡住了,问题如下。

我正在使用 BWWalkthrough 库,以便将 4 张幻灯片作为启动屏幕。所以在我的 appdelegate 中,我有以下初始化 View Controller 的代码:

let storyboard = UIStoryboard(name: "SlidesFlow", bundle: nil)

let walkthrough = storyboard.instantiateViewController(withIdentifier: "SlidesView") as! BWWalkthroughViewController
let page_zero = storyboard.instantiateViewController(withIdentifier: "page_1")
let page_one = storyboard.instantiateViewController(withIdentifier: "page_2")
let page_two = storyboard.instantiateViewController(withIdentifier: "page_3")
let page_three = storyboard.instantiateViewController(withIdentifier: "page_4")

walkthrough.delegate = self
walkthrough.addViewController(page_zero)
walkthrough.addViewController(page_one)
walkthrough.addViewController(page_two)
walkthrough.addViewController(page_three)

一切都按预期工作,所以这里没有问题。在 viewController page_three 上,我有一个按钮,它使用自定义 segue 动画将我重定向到另一个 View Controller

class sentSegueFromRight: UIStoryboardSegue {

override func perform()
{
let src = self.source as UIViewController
let dst = self.destination as UIViewController

src.view.superview?.insertSubview(dst.view, aboveSubview: src.view)
dst.view.transform = CGAffineTransform(translationX: src.view.frame.size.width, y: 0)

UIView.animate(withDuration: 0.25,
delay: 0.0,
options: UIViewAnimationOptions.curveEaseInOut,
animations: {
dst.view.transform = CGAffineTransform(translationX: 0, y: 0)
},
completion: { finished in
src.present(dst, animated: false, completion: nil)
}
)
}
}

现在的问题是,如果我在普通 View Controller 上使用相同的代码,则按钮和动画可以正常工作。问题是当我使用上面从我的 BWWalkthrough 的最后一张幻灯片中定义的 segue 时。我第一次点击按钮时,应该出现的 View Controller 确实出现了,但没有相应的动画。关闭它并再次点击按钮后,播放动画但返回错误:

Presenting view controllers on detached view controllers is discouraged

如果我将按钮与标准动画一起使用(不使用我的自定义动画代码),我不会收到任何错误消息,并且会播放默认动画。

我似乎找不到解决这个问题的方法。有没有人偶然发现这样的事情?

最佳答案

这里的问题在于 BWWalkthrough 库,它使用 ScrollView 来呈现您添加的各种 ViewController 的所有 View 。

因此,您将 dst.view 添加到 ScrollView 的开头(偏移屏幕宽度为 0),然后将其转换为偏移量 (0,0)。

所有这些都在屏幕外,因为您当前处于演练的第三个屏幕(偏移量为 (screenwidth*3,0))。因此,当 segue 结束时,您不会看到动画,而是直接看到呈现的 View Controller 。

要解决这个问题,请将转场中的 dst.view 添加到 ScrollView 的 super View 中。即代替 src.view.superview?.insertSubview(dst.view, aboveSubview: src.view)在 segue 中写入 src.view.superview?.superview?.insertSubview(dst.view, aboveSubview: src.view)。 (假设您仅使用演练中的 segue)

如果你也打算在其他地方使用segue,那么你可以在segue中添加一个类型检查来检查src.view的superview是否为scrollview,如果是,则将dst.view添加到的superview中 ScrollView 。

关于ios - 第一次点击按钮时不会播放 segue 的自定义动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39796010/

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