联系人:石虎 QQ:1224614774 昵称: 嗡嘛呢叭咪哄
QQ群:807236138 群称: iOS 技术交流学习群
QQ群:713799633 群称: iOS技术交流学习群-2
一、使用 参考核心类SHStarRatingView.swift
下载地址: 星星评分: swift实现星星评分功能(支持手势评分)
//
// ViewController.swift
// SHStarRatingView
//
// Created by shihu on 2021/11/3.
//
import UIKit
class ViewController: UIViewController {
let width: Int = 30 // 星星的宽、高
private lazy var starRatingView: SHStarRatingView = {
let temp = SHStarRatingView(frame: CGRect(x: 10, y: 200, width: width * 5 + 5 , height: width))
temp.setBottomImageName("icon_star_gray", topIconStr: "icon_star_yellow", starCount: 5)
temp.allowIncompleteStar = true // 是否允许, 整颗星
temp.allowAnimation = true // 动画
temp.allowProhibitManuallyStar = false // 禁止手动评分
temp.scorePercent = 3.6 / 5 // 默认评分 (0 - 1)
temp.delegate = self
temp.backgroundColor = .yellow
return temp
}()
lazy var nuberLabel: UILabel = { // 评分数值
let temp: UILabel = UILabel(frame: CGRect(x: 0, y: 260, width: 300, height: 20))
temp.text = "3.60"
temp.textAlignment = .center
temp.textColor = .red
temp.backgroundColor = .yellow
return temp
}()
override func viewDidLoad() {
super.viewDidLoad()
self.view.addSubview(self.starRatingView) //添加....五角星...
self.view.addSubview(self.nuberLabel) //添加....五角星数值...
}
}
// MARK: - Private method
extension ViewController: SHStarRatingViewDelegate {
func starRateView(_ starRateView: SHStarRatingView, currentScorePercent: CGFloat) {
print("newScorePercent======\(currentScorePercent * 5)")
self.nuberLabel.text = "\(currentScorePercent * 5)"
}
}
二、demo 效果图
码云:https://gitee.com/ShiHu132/SHUnitView
github:https://gitee.com/ShiHu132/SHUnitView
三、 界面实现截图
谢谢!!!
本账号主要分享我成长过程中的各种心得感悟,包括技术总结, iOS、Swift 和 Mac 相关技术文章、工具资源、参与技术讨论,整理开发技巧、让学习成为一种享受!