gpt4 book ai didi

ios - Swift 中的标签动画

转载 作者:搜寻专家 更新时间:2023-11-01 06:19:21 25 4
gpt4 key购买 nike

我想在我的标签上创建一个过渡效果,以便它从 View 的左侧出现并在屏幕中间居中。我正在做:

override func viewDidLoad() {
super.viewDidLoad()

label.center.x -= view.bounds.width

}

将标签放在 View 之外,但这不起作用,因为标签仍然存在于 View 中。请帮忙。

最佳答案

因为 label.center.x -= view.bounds.width 会让它出现在 View 的左边,所以你看不到它有什么效果。

可以使用UIView.animateWithDuration来制作动画效果:

UIView.animateWithDuration(0.5, delay: 0, options: [.CurveEaseOut], animations: {
label.center.x += view.bounds.width
self.view.layoutIfNeeded()
}, completion: nil)

你应该把它们放在你的 viewDidAppear 而不是 viewDidLoad 中,所以每次你的 View 恢复时,它都会为你的标签设置动画。您的代码将如下所示:

override func viewDidAppear(animated: Bool) {
super.viewDidAppear(animated)
label.center.x = view.center.x // Place it in the center x of the view.
label.center.x -= view.bounds.width // Place it on the left of the view with the width = the bounds'width of the view.
// animate it from the left to the right
UIView.animateWithDuration(0.5, delay: 0, options: [.CurveEaseOut], animations: {
label.center.x += view.bounds.width
self.view.layoutIfNeeded()
}, completion: nil)
}

这将使您的标签从左到右显示。

关于ios - Swift 中的标签动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37170719/

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