swift|swift 评分控件星星的实现
评分控件可以只需要传入显示的view和所需评分,简单易集成
class YWRatingView: UIView {//MARK -- property
var max = 5.0//最大的星星数
var starHeight = 16.0 //星星高度
var starSpace: Double = 4 //星星间距
static var KeyNoRating = "KeyNoRating"var emptyImageViews = [UIImageView]()//空星星图片数组
var fullImageViews = [UIImageView]()//满星星图片数组var value = https://www.it610.com/article/0.0 {
didSet {
if value> max {
value = https://www.it610.com/article/max
}else if value < 0 {
value = 0
}for (i,imageView) in fullImageViews.enumerate() {
let i = Double(i)
if value>= i+1 {
imageView.layer.mask = nil
imageView.hidden = false
}else if value>i && value < i+1 {
let maskLayer = CALayer()
maskLayer.frame = CGRect(x: 0, y: 0, width: (value - i) * starHeight, height: starHeight)
maskLayer.backgroundColor = UIColor.blackColor().CGColor
imageView.layer.mask = maskLayer
imageView.hidden = false
}else if value <= i {
imageView.layer.mask = nil
imageView.hidden = true
}
}
}
}init(starHeight: Double, max: Double) {
self.starHeight = starHeight
self.max = max
self.starSpace = starHeight * 0.15
let frame = CGRect(x: 0, y: 0, width: starHeight * max + starSpace * (max - 1), height: starHeight)
super.init(frame: frame)
for i in 0...4 {
let i = Double(i)
let emptyImageView = UIImageView(image: UIImage(named: "empty_star"))
let fullImageView = UIImageView(image: UIImage(named: "full_star"))
let frame = CGRect(x: starHeight * i + starSpace * i , y: 0, width: starHeight, height: starHeight)
emptyImageView.frame = frame
fullImageView.frame = frame
emptyImageViews.append(emptyImageView)
fullImageViews.append(fullImageView)
addSubview(emptyImageView)
addSubview(fullImageView)
}
}required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}static func showInView(view: UIView, value: Double, max: Double = 5) {for subView in view.subviews {
if let subView = subView as? YWRatingView {
subView.value = https://www.it610.com/article/value
return
}
}
let ratingView = YWRatingView(starHeight: Double(view.frame.size.height), max: max)
ratingView.hidden = false
view.addSubview(ratingView)
ratingView.value = valueif let label = objc_getAssociatedObject(view, &KeyNoRating) as? UILabel {
label.hidden = true
}}//没有评分,显示无评分的label
static func showNoRating(view: UIView) {
for subview in view.subviews {
if let subview = subview as? YWRatingView {
subview.hidden = true
}
}var label = objc_getAssociatedObject(view, &KeyNoRating) as? UILabel
if label == nil {
label = UILabel(frame: CGRectMake(0, 0, view.frame.size.width , view.frame.size.height))
label!.font = UIFont.systemFontOfSize(13)
view.addSubview(label!)
label?.text ="暂无评分"
objc_setAssociatedObject(view, &KeyNoRating, label, objc_AssociationPolicy.OBJC_ASSOCIATION_ASSIGN)
}
label?.hidden = false}
推荐阅读
- Swift中willSet和didSet的简述
- Hacking|Hacking with iOS: SwiftUI Edition - SnowSeeker 项目(一)
- LeetCode算法题-11.|LeetCode算法题-11. 盛最多水的容器(Swift)
- iOS-Swift-map|iOS-Swift-map filter reduce、函数式编程
- Swift|Swift ----viewController 中addChildViewController
- SwiftUI|SwiftUI iOS 瀑布流组件之仿CollectionView不规则图文混合(教程含源码)
- 《赘婿》结局评分下跌,求求“男德”放过女人吧
- Swift高级应用|Swift高级应用 -01
- Swift5.0|Swift5.0 UITexview的基本使用
- Swift7|Swift7 - 循环、函数