Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/amazon-web-services/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 当添加为子视图时,如何设置交叉融合视图的动画?_Ios_Swift - Fatal编程技术网

Ios 当添加为子视图时,如何设置交叉融合视图的动画?

Ios 当添加为子视图时,如何设置交叉融合视图的动画?,ios,swift,Ios,Swift,我正在当前视图底部的UIViewController中添加一个视图作为子视图。我可以用alpha做淡入淡出动画,但我想把它显示得像键盘弹出窗口一样 let popup: UIView = .. popup.alpha = 0.0 self.view.addSubView(popup) UIView.animate(withDuration: 0.3, animations: { popup.alpha = 1.0 }) { _ in } 如何设置动画?您可以更新帧,但我喜欢使用变换设置

我正在当前视图底部的
UIViewController
中添加一个视图作为子视图。我可以用alpha做淡入淡出动画,但我想把它显示得像键盘弹出窗口一样

let popup: UIView = ..
popup.alpha = 0.0
self.view.addSubView(popup)
UIView.animate(withDuration: 0.3, animations: {
    popup.alpha = 1.0
}) { _ in
}

如何设置动画?

您可以更新帧,但我喜欢使用变换设置动画,这样您仍然可以使用约束,而不必弄乱它们

let popup: UIView = ..
popup.transform = CGAffineTransform(translationX: 0, y: popup.bounds.height)
self.view.addSubView(popup)
UIView.animate(withDuration: 0.3, animations: {
    popup.transform = .identity
}) { _ in
}

假设视图位于屏幕底部,将视图向下平移相当于其高度的量,然后将变换动画化回其标识(即原始位置)。

若要仅通过添加子视图实现交叉淡入淡出,请尝试
UIView.transition(使用:duration:options:animations:completion:)
在动画块期间添加视图

快速操场:

//: A UIKit based Playground for presenting user interface

import UIKit
import PlaygroundSupport

class MyViewController : UIViewController {
    override func loadView() {
        let view = UIView()
        view.backgroundColor = .white

        let label = UILabel()
        label.frame = CGRect(x: 150, y: 200, width: 200, height: 20)
        label.text = "Hello World!"
        label.textColor = .black

        view.addSubview(label)
        self.view = view
    }

    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)

        let orangeView = UIView(frame: CGRect(origin: CGPoint(x: view.bounds.midX, y: view.bounds.midY), size: CGSize(width: 100, height: 100)))

        orangeView.backgroundColor = .orange
        DispatchQueue.main.asyncAfter(deadline: .now() + .milliseconds(300)) {
            UIView.transition(with: self.view, duration: 0.6, options: .transitionCrossDissolve, animations: {
                self.view.addSubview(orangeView)
            }) { _ in

            }

        }
    }
}
// Present the view controller in the Live View window
PlaygroundPage.current.liveView = MyViewController()

交叉溶解是一种淡入/淡出,就像你已经做过的那样。是否要将视图从屏幕底部移动到最终位置?不,交叉融合与淡入不同。您还需要淡出下面的视图。@Daniel Storm:是的,类似于键盘弹出时。键盘从底部滑入,然后滑出。我正在寻找一个类似的动画。@JoshHomann在这个问题中没有提到另一个视图。@DanielStorm它清楚地说视图是在当前视图控制器上添加的。