如何在接近尾声时更改 UIProgressView 色调颜色
How to change UIProgressView tint colour as it approaches the end
所以,我在一个基本的费用跟踪应用程序中有一个 UIProgressView
,我想在它到达终点时逐渐从绿色变为红色来更改它的进度色调。我真的不知道该怎么做(我刚开始 swift 和 Xcode)。
大致应该是这样工作的:
我的进度是用这条线计算的:
let progress = Float(UserDefaults.standard.double(forKey: "spentThisMonth") / UserDefaults.standard.double(forKey: "monthlyExpenseLimit"))
progressView.progress = progress
这里我将用户本月花费的金额除以限额以获得进度。当用户接近限制时,进度色调变为红色,表示用户应降低支出。
类似,但我认为它是 Objective-C,而我使用 Swift 5. 有什么办法可以做到这一点,即可能吗?我也是 Stack Overflow 的新手,所以如果我做错了什么,我提前道歉。
当然可以使用 Swift 来完成。这里的代码来自 Swift:
中的答案
func changeSendProg(progress: CGFloat) {
let redBlueValue: CGFloat = (255 - (255 * progress)) / 255
let colorProg = UIColor(red: redBlueValue, green: redBlueValue, blue: 1.0, alpha: 1.0)
sendProgress.setTintColor(colorProg)
sendProgress.setProgress(progress)
}
您可以使用您已经计算出的 progress
值轻松设置绿色/红色色调:
let progress = Float(UserDefaults.standard.double(forKey: "spentThisMonth") / UserDefaults.standard.double(forKey: "monthlyExpenseLimit"))
// progress is now a value between 0.0 and 1.0
progressView.progress = progress
let red = CGFloat(progress)
let green = CGFloat(1.0 - progress)
progressView.progressTintColor = UIColor(red: red, green: green, blue: 0.0, alpha: 1.0)
这是一个工作示例 - 每次点击屏幕时,进度都会递增,直到达到 100%,届时它会递减回零:
class ProgressTintViewController: UIViewController {
let progressView = UIProgressView()
let statsLabel = UILabel()
var spentThisMonth: Float = 0
var monthlyExpenseLimit: Float = 100
var isAdding: Bool = true
override func viewDidLoad() {
super.viewDidLoad()
progressView.translatesAutoresizingMaskIntoConstraints = false
statsLabel.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(progressView)
view.addSubview(statsLabel)
let g = view.safeAreaLayoutGuide
NSLayoutConstraint.activate([
progressView.topAnchor.constraint(equalTo: g.topAnchor, constant: 80.0),
progressView.leadingAnchor.constraint(equalTo: g.leadingAnchor, constant: 20.0),
progressView.trailingAnchor.constraint(equalTo: g.trailingAnchor, constant: -20.0),
statsLabel.topAnchor.constraint(equalTo: progressView.bottomAnchor, constant: 20.0),
statsLabel.leadingAnchor.constraint(equalTo: g.leadingAnchor, constant: 20.0),
statsLabel.trailingAnchor.constraint(equalTo: g.trailingAnchor, constant: -20.0),
])
statsLabel.numberOfLines = 0
let t = UITapGestureRecognizer(target: self, action: #selector(self.tapped))
view.addGestureRecognizer(t)
updateProgress()
}
@objc func tapped() -> Void {
if isAdding {
spentThisMonth += 10
} else {
spentThisMonth -= 10
}
updateProgress()
if spentThisMonth >= monthlyExpenseLimit {
spentThisMonth = monthlyExpenseLimit
isAdding = false
}
if spentThisMonth <= 0 {
spentThisMonth = 0
isAdding = true
}
}
func updateProgress() -> Void {
//let progress = Float(UserDefaults.standard.double(forKey: "spentThisMonth") / UserDefaults.standard.double(forKey: "monthlyExpenseLimit"))
// progress is now a value between 0.0 and 1.0
let progress = spentThisMonth / monthlyExpenseLimit
progressView.progress = progress
let red = CGFloat(progress)
let green = CGFloat(1.0 - progress)
progressView.progressTintColor = UIColor(red: red, green: green, blue: 0.0, alpha: 1.0)
var str = ""
str += "Tap to change progress"
str += "\n\n"
str += "Spent This Month: \(spentThisMonth)"
str += "\n"
str += "Monthly Limit: \(monthlyExpenseLimit)"
str += "\n"
str += "Percent: \(progress)"
statsLabel.text = str
}
}
所以,我在一个基本的费用跟踪应用程序中有一个 UIProgressView
,我想在它到达终点时逐渐从绿色变为红色来更改它的进度色调。我真的不知道该怎么做(我刚开始 swift 和 Xcode)。
大致应该是这样工作的:
我的进度是用这条线计算的:
let progress = Float(UserDefaults.standard.double(forKey: "spentThisMonth") / UserDefaults.standard.double(forKey: "monthlyExpenseLimit"))
progressView.progress = progress
这里我将用户本月花费的金额除以限额以获得进度。当用户接近限制时,进度色调变为红色,表示用户应降低支出。
当然可以使用 Swift 来完成。这里的代码来自 Swift:
中的答案func changeSendProg(progress: CGFloat) {
let redBlueValue: CGFloat = (255 - (255 * progress)) / 255
let colorProg = UIColor(red: redBlueValue, green: redBlueValue, blue: 1.0, alpha: 1.0)
sendProgress.setTintColor(colorProg)
sendProgress.setProgress(progress)
}
您可以使用您已经计算出的 progress
值轻松设置绿色/红色色调:
let progress = Float(UserDefaults.standard.double(forKey: "spentThisMonth") / UserDefaults.standard.double(forKey: "monthlyExpenseLimit"))
// progress is now a value between 0.0 and 1.0
progressView.progress = progress
let red = CGFloat(progress)
let green = CGFloat(1.0 - progress)
progressView.progressTintColor = UIColor(red: red, green: green, blue: 0.0, alpha: 1.0)
这是一个工作示例 - 每次点击屏幕时,进度都会递增,直到达到 100%,届时它会递减回零:
class ProgressTintViewController: UIViewController {
let progressView = UIProgressView()
let statsLabel = UILabel()
var spentThisMonth: Float = 0
var monthlyExpenseLimit: Float = 100
var isAdding: Bool = true
override func viewDidLoad() {
super.viewDidLoad()
progressView.translatesAutoresizingMaskIntoConstraints = false
statsLabel.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(progressView)
view.addSubview(statsLabel)
let g = view.safeAreaLayoutGuide
NSLayoutConstraint.activate([
progressView.topAnchor.constraint(equalTo: g.topAnchor, constant: 80.0),
progressView.leadingAnchor.constraint(equalTo: g.leadingAnchor, constant: 20.0),
progressView.trailingAnchor.constraint(equalTo: g.trailingAnchor, constant: -20.0),
statsLabel.topAnchor.constraint(equalTo: progressView.bottomAnchor, constant: 20.0),
statsLabel.leadingAnchor.constraint(equalTo: g.leadingAnchor, constant: 20.0),
statsLabel.trailingAnchor.constraint(equalTo: g.trailingAnchor, constant: -20.0),
])
statsLabel.numberOfLines = 0
let t = UITapGestureRecognizer(target: self, action: #selector(self.tapped))
view.addGestureRecognizer(t)
updateProgress()
}
@objc func tapped() -> Void {
if isAdding {
spentThisMonth += 10
} else {
spentThisMonth -= 10
}
updateProgress()
if spentThisMonth >= monthlyExpenseLimit {
spentThisMonth = monthlyExpenseLimit
isAdding = false
}
if spentThisMonth <= 0 {
spentThisMonth = 0
isAdding = true
}
}
func updateProgress() -> Void {
//let progress = Float(UserDefaults.standard.double(forKey: "spentThisMonth") / UserDefaults.standard.double(forKey: "monthlyExpenseLimit"))
// progress is now a value between 0.0 and 1.0
let progress = spentThisMonth / monthlyExpenseLimit
progressView.progress = progress
let red = CGFloat(progress)
let green = CGFloat(1.0 - progress)
progressView.progressTintColor = UIColor(red: red, green: green, blue: 0.0, alpha: 1.0)
var str = ""
str += "Tap to change progress"
str += "\n\n"
str += "Spent This Month: \(spentThisMonth)"
str += "\n"
str += "Monthly Limit: \(monthlyExpenseLimit)"
str += "\n"
str += "Percent: \(progress)"
statsLabel.text = str
}
}