swift 評分控件星星的實(shí)現(xiàn)

評分控件可以只需要傳入顯示的view和所需評分,簡單易集成


class YWRatingView: UIView {

    //MARK -- property
    var max = 5.0   //最大的星星數(shù)
    var starHeight = 16.0 //星星高度
    var starSpace: Double = 4 //星星間距
    static var KeyNoRating = "KeyNoRating"
    
    var emptyImageViews = [UIImageView]()    //空星星圖片數(shù)組
    var fullImageViews = [UIImageView]()     //滿星星圖片數(shù)組
    
    var value = 0.0 {
        didSet {
            if value > max {
                value = 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 = value
                return
            }
        }
        let ratingView = YWRatingView(starHeight: Double(view.frame.size.height), max: max)
        ratingView.hidden = false
        view.addSubview(ratingView)
        ratingView.value = value
        
        if 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
        
    }

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

推薦閱讀更多精彩內(nèi)容

  • Android 自定義View的各種姿勢1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 173,466評論 25 708
  • 發(fā)現(xiàn) 關(guān)注 消息 iOS 第三方庫、插件、知名博客總結(jié) 作者大灰狼的小綿羊哥哥關(guān)注 2017.06.26 09:4...
    肇東周閱讀 12,251評論 4 61
  • 一 陌生人之間的不經(jīng)意關(guān)心,好像并不會讓人感到不習(xí)慣,反而會讓人有點(diǎn)小驚喜。 今天去買票,告訴那個(gè)人說買22號的,...
    虛妄閱讀 499評論 1 0
  • 1感賞爸爸對我的指導(dǎo),玩對生活開支有計(jì)劃,好好過日子。 2感賞自己覺察到要好好去練習(xí)了,一味的托延馬上一年又快過去...
    唐春娥_中閱讀 144評論 0 0
  • 每次要隨心紀(jì)錄下生活點(diǎn)滴的時(shí)候,想文章的標(biāo)題,是我最頭痛的事。大抵是我的敘述,形式上都比較散,所以往往不知道要以何...
    莫晚夏閱讀 238評論 0 0