以编程方式快速定制半填充星级
我正在尝试创建一个半填充的星级评级视图。
这是我编写的代码:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)