Swift基础之两种选择星星的评价样式并获取星星的索引值

时间:2024-06-15 18:07:56

想练练手,所以封装了一个两种选择星星的评价样式的Demo,并且可以获取到点击的星星的索引值,方便记录值,上传数据时使用

首先创建View类,设计初始化方法,并且用到了枚举类型和代理方法

方式一:默认的有文字的5颗星评价的样式

case .TextType://有文字样式
            
            //计算每个星星的大小宽、高
            let itemWidthF:CGFloat = viewWidthF / 5.0
            let itemHeightF:CGFloat = viewHeightF / 2.0
            //将小的值赋值给星星
            let starWidth:CGFloat = 30.0;
            let textArr:NSArray = ["很差","凑合","一般","不错","完美"]
            
            //创建按钮和星星
            for i in 0...4 {
                let starImgV = UIImageView.init(frame: CGRect.init(x: (itemWidthF-starWidth)/2+itemWidthF*CGFloat(i), y: (itemHeightF-starWidth)/2, width: starWidth, height: starWidth))
                starImgV.tag = i+100
                starImgV.image = UIImage.init(named: "StarUnSelect")
                self.addSubview(starImgV)
                
                let butt = UIButton.init(frame: CGRect.init(x: itemWidthF*CGFloat(i), y: 0, width: itemWidthF, height: itemHeightF));
                butt.tag = i+200
                butt.addTarget(self, action: #selector(buttClick), for: UIControlEvents.touchUpInside)
                self.addSubview(butt)
                
                let textLabel = UILabel.init(frame: CGRect.init(x: itemWidthF*CGFloat(i), y: itemHeightF, width: itemWidthF, height: itemHeightF))
                textLabel.text = textArr[i] as? String
                textLabel.adjustsFontSizeToFitWidth = true
                textLabel.textColor = UIColor.lightGray
                textLabel.font = UIFont.systemFont(ofSize: 13)
                textLabel.textAlignment = NSTextAlignment.center;
                self.addSubview(textLabel)
            }
            
            break

方式二:没有文字描述的评价程度,同时要注意视图的宽高大小问题

case .NoTextType:
            //无文字样式
            //计算每个星星的大小宽、高
            let itemWidthF:CGFloat = viewWidthF / CGFloat(aNum)
            let itemHeightF:CGFloat = viewHeightF
            //设置星星的大小
            var starWidth:CGFloat = 0.0
            if itemWidthF>30 {
                //如果大于,保持大小为30
                starWidth = 30
                //创建按钮和星星
                for i in 0...aNum-1 {
                    let starImgV = UIImageView.init(frame: CGRect.init(x: (itemWidthF-starWidth)/2+itemWidthF*CGFloat(i), y: (itemHeightF-starWidth)/2, width: starWidth, height: starWidth))
                    starImgV.tag = i+100
                    starImgV.image = UIImage.init(named: "StarUnSelect")
                    self.addSubview(starImgV)
                    
                    let butt = UIButton.init(frame: CGRect.init(x: itemWidthF*CGFloat(i), y: 0, width: itemWidthF, height: itemHeightF));
                    butt.tag = i+200
                    butt.addTarget(self, action: #selector(buttClick), for: UIControlEvents.touchUpInside)
                    self.addSubview(butt)
                    
                }
            }else{
                //如果小于,判断宽、高的大小,将小的值赋值给星星
                //三目运算
                //starWidth = itemWidthF<itemHeightF ? itemWidthF:itemHeightF
                
                if itemWidthF<=itemHeightF {
                    //如果宽小于高
                    starWidth = itemWidthF
                    //创建按钮和星星
                    for i in 0...aNum-1 {
                        let starImgV = UIImageView.init(frame: CGRect.init(x: starWidth*CGFloat(i), y: (itemHeightF-starWidth)/2, width: starWidth, height: starWidth))
                        starImgV.tag = i+100
                        starImgV.image = UIImage.init(named: "StarUnSelect")
                        self.addSubview(starImgV)
                        
                        let butt = UIButton.init(frame: CGRect.init(x: starWidth*CGFloat(i), y: 0, width: starWidth, height: itemHeightF));
                        butt.tag = i+200
                        butt.addTarget(self, action: #selector(buttClick), for: UIControlEvents.touchUpInside)
                        self.addSubview(butt)
                    }
                }else{
                    //如果宽大于高
                    starWidth = itemHeightF
                    //创建按钮和星星
                    for i in 0...aNum-1 {
                        let starImgV = UIImageView.init(frame: CGRect.init(x: (itemWidthF-starWidth)/2+itemWidthF*CGFloat(i), y: 0, width: starWidth, height: starWidth))
                        starImgV.tag = i+100
                        starImgV.image = UIImage.init(named: "StarUnSelect")
                        self.addSubview(starImgV)
                        
                        let butt = UIButton.init(frame: CGRect.init(x: itemWidthF*CGFloat(i), y: 0, width: itemWidthF, height: itemHeightF));
                        butt.tag = i+200
                        butt.addTarget(self, action: #selector(buttClick), for: UIControlEvents.touchUpInside)
                        self.addSubview(butt)
                    }
                }
            }
            
            break

设计代理方法,并实现

//定义协议,知道点击的哪一个
protocol BHClickMarkStarDelegate:NSObjectProtocol{
    
    //定义一个可以记录显示的哪颗星星的方法
    func clickWhichStar(bhMarkView:BHMarkStarView,indexNum:Int)
    
}

//实现协议方法
    func clickWhichStar(bhMarkView: BHMarkStarView, indexNum: Int) {
        switch bhMarkView.tag {
        case 1:
            print("输出的是第。。。一。。。个视图的第...\(indexNum+1)...个星星")
            break
        case 2:
            print("输出的是第。。。二。。。个视图的第...\(indexNum+1)...个星星")
            break
        case 3:
            print("输出的是第。。。三。。。个视图的第...\(indexNum+1)...个星星")
            break
        case 10:
            print("输出的是第。。。四。。。个视图的第...\(indexNum+1)...个星星")
            break
        case 11:
            print("输出的是第。。。五。。。个视图的第...\(indexNum+1)...个星星")
            break
        case 12:
            print("输出的是第。。。六。。。个视图的第...\(indexNum+1)...个星星")
            break
        default:
            break
        }
    }

效果图:(源码下载:https://github.com/hbblzjy/SwiftMarkStartDemo

Swift基础之两种选择星星的评价样式并获取星星的索引值     Swift基础之两种选择星星的评价样式并获取星星的索引值