swift 简单封装的一个五星评分器

swift 封装的一个五星评分器,可以用于单纯展示评分,也可以用来手动打分。



gitHub地址:https://github.com/NinoWang/RatingBar

import UIKit
protocol ratingBarDelegate {
    func ratingChanged(ratingBar:ratingBar,newrating:Float)
}

class ratingBar: UIView {
    var delegate:ratingBarDelegate?

    var starrating:Float?
    var lastrating:Float?
    
    var starWidth:CGFloat?
    var starHeight:CGFloat?

    var unSelectedImage:UIImage?
    var halfSelectedImage:UIImage?
    var fullSelectedImage:UIImage?
    
    var s1:UIImageView?
    var s2:UIImageView?
    var s3:UIImageView?
    var s4:UIImageView?
    var s5:UIImageView?
    //是否是指示器
    var isIndicator:Bool = false
    
    func setseletedState(deselectedname:String?,halfSelectedname:String?,fullSelectedname:String?,starSideLength:CGFloat,delegate:ratingBarDelegate){
        self.delegate = delegate
        unSelectedImage = UIImage(named: deselectedname!)
        fullSelectedImage = UIImage(named: fullSelectedname!)
        halfSelectedImage = halfSelectedname == nil ? fullSelectedImage:UIImage(named: halfSelectedname!)
        
        
        starWidth = 0
        starHeight = 0
        if (starHeight < starSideLength) {
            starHeight = starSideLength
        }
        if (starWidth < starSideLength) {
            starWidth = starSideLength
        }
      

        //控件宽度适配
        var frame = self.frame
        var viewWidth:CGFloat = starWidth! * 5
        
        if (frame.size.width) > viewWidth {
            viewWidth = frame.size.width
        }
        frame.size.width = viewWidth
        self.frame = frame
        
        starrating = 0
        lastrating = 0
        
        s1 = UIImageView(image: unSelectedImage)
        s2 = UIImageView(image: unSelectedImage)
        s3 = UIImageView(image: unSelectedImage)
        s4 = UIImageView(image: unSelectedImage)
        s5 = UIImageView(image: unSelectedImage)
        
        //星星间距
        let space:CGFloat = (viewWidth - starWidth!*5)/6
        var starX = space
        let starY = (frame.height - starHeight!)/2
        s1?.frame = CGRectMake(starX,starY,starWidth!,starHeight!)
        starX = starX + starWidth! + space
        s2?.frame = CGRectMake(starX,starHeight!)
        starX = starX + starWidth! + space
        s3?.frame = CGRectMake(starX,starHeight!)
        starX = starX + starWidth! + space
        s4?.frame = CGRectMake(starX,starHeight!)
        starX = starX + starWidth! + space
        s5?.frame = CGRectMake(starX,starHeight!)
        starX = starX + starWidth! + space
        
        s1?.userInteractionEnabled = false
        s2?.userInteractionEnabled = false
        s3?.userInteractionEnabled = false
        s4?.userInteractionEnabled = false
        s5?.userInteractionEnabled = false
        
        self.addSubview(s1!)
        self.addSubview(s2!)
        self.addSubview(s3!)
        self.addSubview(s4!)
        self.addSubview(s5!)
    }
    //设置评分值
    func displayrating(rating:Float){
        s1?.image = unSelectedImage
        s2?.image = unSelectedImage
        s3?.image = unSelectedImage
        s4?.image = unSelectedImage
        s5?.image = unSelectedImage
        
        if (rating >= 1) {
            s1?.image = halfSelectedImage
        }
        if (rating >= 2) {
           s1?.image = fullSelectedImage
        }
        if (rating >= 3) {
            s2?.image = halfSelectedImage
        }
        if (rating >= 4) {
            s2?.image = fullSelectedImage
        }
        if (rating >= 5) {
            s3?.image = halfSelectedImage
        }
        if (rating >= 6) {
            s3?.image = fullSelectedImage
        }
        if (rating >= 7) {
            s4?.image = halfSelectedImage
        }
        if (rating >= 8) {
            s4?.image = fullSelectedImage
        }
        if (rating >= 9) {
            s5?.image = halfSelectedImage
        }
        if (rating >= 10) {
            s5?.image = fullSelectedImage
        }
        
        starrating = rating
        lastrating = rating
        delegate?.ratingChanged(self,newrating: rating)

    }
    
    func rating() -> Float{
        return starrating!
    }
    //手势
    override func touchesBegan(touches: Set<UITouch>,withEvent event: UIEvent?) {
        super.touchesBegan(touches,withEvent: event)
    }
    
    override func touchesEnded(touches: Set<UITouch>,withEvent event: UIEvent?) {
        super.touchesEnded(touches,withEvent: event)
        self.touchesrating(touches)
    }
    
    override func touchesMoved(touches: Set<UITouch>,withEvent event: UIEvent?) {
        super.touchesMoved(touches,withEvent: event)
        self.touchesrating(touches)
    }
    //触发
    func touchesrating(touches:NSSet){
        if(self.isIndicator == false){
            return
        }
        
        let point:CGPoint = touches.anyObject()!.locationInView(self)
        let space:CGFloat = (self.frame.size.width - starWidth!*5)/6
        
        var newrating:Float = 0
        if (point.x >= 0 && point.x <= self.frame.size.width) {
            
            if (point.x <= space+starWidth!*0.5) {
                newrating = 1;
            }else if (point.x < space*2+starWidth!){
                newrating = 2;
            }else if (point.x < space*2+starWidth!*1.5){
                newrating = 3;
            }else if (point.x <= 3*space+2*starWidth!){
                newrating = 4;
            }else if (point.x <= 3*space+2.5*starWidth!){
                newrating = 5;
            }else if (point.x <= 4*space+3*starWidth!){
                newrating = 6;
            }else if (point.x <= 4*space+3.5*starWidth!){
                newrating = 7;
            }else if (point.x <= 5*space+4*starWidth!){
                newrating = 8;
            }else if (point.x <= 5*space+4.5*starWidth!){
                newrating = 9;
            }else {
                newrating = 10;
            }
        }
        
        if (newrating != lastrating){
            self.displayrating(newrating)
        }
    }

}

相关文章

软件简介:蓝湖辅助工具,减少移动端开发中控件属性的复制和粘...
现实生活中,我们听到的声音都是时间连续的,我们称为这种信...
前言最近在B站上看到一个漂亮的仙女姐姐跳舞视频,循环看了亿...
【Android App】实战项目之仿抖音的短视频分享App(附源码和...
前言这一篇博客应该是我花时间最多的一次了,从2022年1月底至...
因为我既对接过session、cookie,也对接过JWT,今年因为工作...