gpt4 book ai didi

ios - 为什么 UILabel TextColor 在 Playground 中是动画的,而不是在设备上?

转载 作者:行者123 更新时间:2023-11-28 08:24:44 24 4
gpt4 key购买 nike

所以我想为某些 UILabel 设置文本颜色变化的动画。我的成功喜忧参半。

我最终要做的事情之一是使用 Xcode 8 在 Swift Playground 中进行尝试。运行动画的代码最简单的形式是:

UIView.animate(withDuration: 5.0) {
label.textColor = UIColor.red
label.transform = CGAffineTransform(scaleX: 2, y: 2)
}

但是,当我将相同的代码插入一个空的 (Swift) iOS 项目时,文本颜色不再是动画的。

我知道 UILabel textColor 属性不应该是动画的,但在这里它是动画的。关于发生了什么的任何线索?也许与 PlaygroundSupport 模块有关?

这是在 Playground 中看到的动画:

Playground Animation

这是在模拟器中绘制的动画:

Simulator Animation

编辑:添加动画以更好地阐明正在发生的事情

最佳答案

更新的答案:

注意:只需将以下代码复制并粘贴到您的 viewController 中,然后连接您的 IBOutlets 并运行该项目。

import UIKit

class ViewController: UIViewController {

override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}

@IBOutlet weak var animateLabel: UILabel!

@IBAction func animateButton(_ sender: UIButton) {
// textColor will change from blue to red...
animateLabel.textColor = .blue // set any colour
UIView.animate(withDuration: 5.0) {
self.animateLabel.textColor = UIColor.red
self.animateLabel.transform = CGAffineTransform(scaleX: 2, y: 2)
}
}
}

输出:

enter image description here

您的代码看起来不错。将您的 textColor 设置为不同的颜色而不是白色。正如您所提到的。您在空项目上运行此代码,这意味着默认的 viewController 背景也是白色,这就是为什么您看不到动画的原因。希望下面的代码有所帮助。

 label.textColor = UIColor.red // set to any colour

注意:如果您想将文本颜色保持为白色..更改文本颜色背景或对标签应用一些阴影以使其可见。

//apply backgroundcolor to your label
label.backgroundColor = UIColor.green // set to any colour

// apply shadow to your label
self.label.layer.shadowColor = UIColor.red.cgColor
self.label.layer.shadowOpacity = 1
self.label.layer.shadowOffset = CGSize.zero
self.label.layer.shadowRadius = 10

关于ios - 为什么 UILabel TextColor 在 Playground 中是动画的,而不是在设备上?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40353809/

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