Swift 中的评分栏百分比
Percent Rating Bar in Swift
我有关于 votes/total votes
的数据,我想添加一个显示投票百分比的百分比评级栏。即:95% 并填充栏 95%。我找不到任何关于此的 swift 说明(除了尝试 UISlider)。
示例:
Rating Bar (350/700 votes):
[==========50% ]
Rating Bar (180/200 votes):
[==========90%========== ]
Rating Bar (213/709 votes):
[====== 30% ]
使用 UIView
作为背景视图。然后,添加另一个视图作为进度条的子视图。让我们将其实现为 class:
class ProgressView: UIView {
var progress: CGFloat = 0
var filledView: UIView
override init(frame: CGRect) {
filledView = UIView(frame: CGRect(x: frame.origin.x, y: frame.origin.y, width: 0, height: frame.height))
filledView.backgroundColor = Colors.fontColor
super.init(frame: frame)
addSubview(filledView)
}
required init(coder aDecoder: NSCoder) { // <-- You need to implement this
fatalError()
}
func setProgess(var progress: CGFloat) {
progress = min(max(progress, 0), 1) // Between 0 and 1
self.progress = progress
filledView.frame.size.width = self.frame.width * progress
}
}
现在您还可以根据需要添加 UILabel
来显示百分比。
我有关于 votes/total votes
的数据,我想添加一个显示投票百分比的百分比评级栏。即:95% 并填充栏 95%。我找不到任何关于此的 swift 说明(除了尝试 UISlider)。
示例:
Rating Bar (350/700 votes):
[==========50% ]
Rating Bar (180/200 votes):
[==========90%========== ]
Rating Bar (213/709 votes):
[====== 30% ]
使用 UIView
作为背景视图。然后,添加另一个视图作为进度条的子视图。让我们将其实现为 class:
class ProgressView: UIView {
var progress: CGFloat = 0
var filledView: UIView
override init(frame: CGRect) {
filledView = UIView(frame: CGRect(x: frame.origin.x, y: frame.origin.y, width: 0, height: frame.height))
filledView.backgroundColor = Colors.fontColor
super.init(frame: frame)
addSubview(filledView)
}
required init(coder aDecoder: NSCoder) { // <-- You need to implement this
fatalError()
}
func setProgess(var progress: CGFloat) {
progress = min(max(progress, 0), 1) // Between 0 and 1
self.progress = progress
filledView.frame.size.width = self.frame.width * progress
}
}
现在您还可以根据需要添加 UILabel
来显示百分比。