gpt4 book ai didi

ios - 同时运行两个动画

转载 作者:搜寻专家 更新时间:2023-10-31 22:40:13 25 4
gpt4 key购买 nike

我正在尝试创建具有两个 View 的动画,但在执行它们时遇到了一些意外行为。

我想在做第二个动画时为两个 View 位置设置动画,即 transitionFlipFromBottom

代码如下:

let initialFrame = CGRect(x: xpos, y: -310, width: 300, height: 300)

let firstView = UIView()
firstView.backgroundColor = .red
firstView.frame = initialFrame

let secondView = UIView()
secondView.backgroundColor = .white
secondView.frame = initialFrame
secondView.isHidden = false


self.view.addSubview(firstView)
self.view.addSubview(secondView)


// Here I try to move the views on screen while fliping them
UIView.animate(withDuration: 1, delay: 0, options: .curveEaseOut, animations: {
secondView.center = self.view.center
firstView.center = self.view.center
self.flip(firstView: firstView, secondView: secondView)
}, completion: nil)



// This function flips the views vertically while it animates the transition from the first to the second view
fileprivate func flip(firstView: UIView, secondView: UIView) {
let transitionOptions: UIViewAnimationOptions = [.transitionFlipFromBottom, .showHideTransitionViews]
UIView.transition(with: firstView, duration: 0.5, options: transitionOptions, animations: {
firstView.isHidden = true
})

UIView.transition(with: secondView, duration: 0.5, options: transitionOptions, animations: {
secondView.isHidden = false
})
}

上面的代码无法同时执行两个动画。

只有在第一个动画(移动帧)完成后,将 flip 函数调用放在 completion block 中,它才有效,如下所示:

UIView.animate(withDuration: 1, delay: 0, options: .curveEaseOut, animations: {
secondView.center = self.view.center
firstView.center = self.view.center
}, completion: {(_) in
self.flip(firstView: dummyView, secondView: newGroupView)
})

我什至尝试使用 UIView.animateKeyframes 但它仍然不起作用。

我是不是漏掉了什么?

谢谢。

最佳答案

一些事情:

  1. transition中,指定.allowAnimatedContent选项。

  2. 延迟动画:

    DispatchQueue.main.async {
    UIView.animate(withDuration: 1, delay: 0, options: [.curveEaseOut], animations: {
    secondView.center = CGPoint(x: self.view.bounds.midX, y: self.view.bounds.midY)
    firstView.center = CGPoint(x: self.view.bounds.midX, y: self.view.bounds.midY)
    self.flip(firstView: firstView, secondView: secondView)
    }, completion: { _ in
    })
    }
  3. 有点无关,你不想要:

    secondView.center = self.view.center

    相反,做:

    secondView.center = CGPoint(x: self.view.bounds.midX, y: self.view.bounds.midY)

    您想在 viewbounds 的坐标空间中设置 secondView.center,而不是在 view 中> 的 superview

关于ios - 同时运行两个动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48325430/

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