以编程方式快速定制半填充星级

发布于 2025-01-13 04:04:11 字数 1400 浏览 0 评论 0原文

我正在尝试创建一个半填充的星级评级视图。

这是我编写的代码:

func setUpStarStack() {
        for i in 0..<5 {
            let imageView = UIImageView()
            if(i==0){
                imageView.tag = 5009
            }
            imageView.image = UIImage(named: "img_star_nofill")
            stackViewStar.addArrangedSubview(imageView)
            arrayImage.append(imageView)
        }
    self.addSubview(stackViewStar)
    stackViewStar.snp.makeConstraints { make in
        make.top.left.right.bottom.equalToSuperview()
    }
}

向 StackView 添加了五个空星形图像。

    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
    let touchLocation = touches.first
    let location = touchLocation?.location(in: stackViewStar)
    if(touchLocation?.view?.tag == 5007){
        var intRating:Float = 0
        arrayImage.forEach { (imageView) in
            if ((location?.x)! > imageView.frame.origin.x) {
                let i = arrayImage.firstIndex(of: imageView)
                intRating = Float(i!) + 1
                intRate = Float(intRating)
                imageView.image = UIImage(named: "img_star_fill")
            } else {
                if(imageView.tag != 5009){
                    imageView.image = UIImage(named: "img_star_nofill")
                }
            }
        }
    }
}

我不知道如何根据触摸位置填写半颗星。

I'm trying to create a half-filled star rating view.

Here is the code I wrote:

func setUpStarStack() {
        for i in 0..<5 {
            let imageView = UIImageView()
            if(i==0){
                imageView.tag = 5009
            }
            imageView.image = UIImage(named: "img_star_nofill")
            stackViewStar.addArrangedSubview(imageView)
            arrayImage.append(imageView)
        }
    self.addSubview(stackViewStar)
    stackViewStar.snp.makeConstraints { make in
        make.top.left.right.bottom.equalToSuperview()
    }
}

Added five empty star images to StackView.

    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
    let touchLocation = touches.first
    let location = touchLocation?.location(in: stackViewStar)
    if(touchLocation?.view?.tag == 5007){
        var intRating:Float = 0
        arrayImage.forEach { (imageView) in
            if ((location?.x)! > imageView.frame.origin.x) {
                let i = arrayImage.firstIndex(of: imageView)
                intRating = Float(i!) + 1
                intRate = Float(intRating)
                imageView.image = UIImage(named: "img_star_fill")
            } else {
                if(imageView.tag != 5009){
                    imageView.image = UIImage(named: "img_star_nofill")
                }
            }
        }
    }
}

I don't know how to fill in half a star depending on the touch location.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

望笑 2025-01-20 04:04:11
arrayImage.forEach { (imageView) in
           if imageView.frame.origin.x < (location?.x)! && (location?.x)! <= imageView.frame.origin.x + imageView.frame.size.width / 2 {
                    
                    imageView.image = UIImage(named: "img_star_half")
            } else if ((location?.x)! > imageView.frame.origin.x) {
          
                imageView.image = UIImage(named: "img_star_fill")
            } else {
                if(imageView.tag != 5009){
                    imageView.image = UIImage(named: "img_star_nofill")
                }
            }    
arrayImage.forEach { (imageView) in
           if imageView.frame.origin.x < (location?.x)! && (location?.x)! <= imageView.frame.origin.x + imageView.frame.size.width / 2 {
                    
                    imageView.image = UIImage(named: "img_star_half")
            } else if ((location?.x)! > imageView.frame.origin.x) {
          
                imageView.image = UIImage(named: "img_star_fill")
            } else {
                if(imageView.tag != 5009){
                    imageView.image = UIImage(named: "img_star_nofill")
                }
            }    
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文