Ios RatingControl-捕获从viewController单击的星星数
我在我的应用程序中使用RatingControl类(在gitHub上找到)记录4个标准(实例)的反馈。我必须从viewController中分别捕获每个实例中单击的星星数。如何捕获从viewController单击的星星数,并区分已单击的实例。我相信我必须使用RatingControl类中的RatingButton函数,但不确定如何实现这一点 下面是ViewController代码段:Ios RatingControl-捕获从viewController单击的星星数,ios,swift,Ios,Swift,我在我的应用程序中使用RatingControl类(在gitHub上找到)记录4个标准(实例)的反馈。我必须从viewController中分别捕获每个实例中单击的星星数。如何捕获从viewController单击的星星数,并区分已单击的实例。我相信我必须使用RatingControl类中的RatingButton函数,但不确定如何实现这一点 下面是ViewController代码段: class DetailsViewController: UIViewController { @IBO
class DetailsViewController: UIViewController {
@IBOutlet weak var ratingControl1: RatingControl!
@IBOutlet weak var ratingControl2: RatingControl!
@IBOutlet weak var ratingControl3: RatingControl!
@IBOutlet weak var ratingControl4: RatingControl!
//I want to use some code within ViewController to capture the #stars clicked
评级控制等级如下:
@IBDesignable class RatingControl: UIStackView {
//MARK: Properties
private var ratingButtons = [UIButton]()
var rating = 0 {
didSet {
updateButtonSelectionStates()
}
}
@IBInspectable var starSize: CGSize = CGSize(width: 44.0, height: 44.0) {
didSet {
setupButtons()
}
}
@IBInspectable var starCount: Int = 5 {
didSet {
setupButtons()
}
}
//MARK: Initialization
override init(frame: CGRect) {
super.init(frame: frame)
setupButtons()
}
required init(coder: NSCoder) {
super.init(coder: coder)
setupButtons()
}
//MARK: Button Action
func ratingButtonTapped(button: UIButton) {
guard let index = ratingButtons.index(of: button) else {
fatalError("The button, \(button), is not in the ratingButtons array: \(ratingButtons)")
}
// Calculate the rating of the selected button
let selectedRating = index + 1
if selectedRating == rating {
// If the selected star represents the current rating, reset the rating to 0.
rating = 0
} else {
// Otherwise set the rating to the selected star
rating = selectedRating
}
print(selectedRating)
}
//MARK: Private Methods
private func setupButtons() {
// Clear any existing buttons
for button in ratingButtons {
removeArrangedSubview(button)
button.removeFromSuperview()
}
ratingButtons.removeAll()
// Load Button Images
let bundle = Bundle(for: type(of: self))
let filledStar = UIImage(named: "filledStar", in: bundle, compatibleWith: self.traitCollection)
let emptyStar = UIImage(named:"emptyStar", in: bundle, compatibleWith: self.traitCollection)
let highlightedStar = UIImage(named:"highlightedStar", in: bundle, compatibleWith: self.traitCollection)
for index in 0..<starCount {
// Create the button
let button = UIButton()
// Set the button images
button.setImage(emptyStar, for: .normal)
button.setImage(filledStar, for: .selected)
button.setImage(highlightedStar, for: .highlighted)
button.setImage(highlightedStar, for: [.highlighted, .selected])
// Add constraints
button.translatesAutoresizingMaskIntoConstraints = false
button.heightAnchor.constraint(equalToConstant: starSize.height).isActive = true
button.widthAnchor.constraint(equalToConstant: starSize.width).isActive = true
// Set the accessibility label
button.accessibilityLabel = "Set \(index + 1) star rating"
// Setup the button action
button.addTarget(self, action: #selector(RatingControl.ratingButtonTapped(button:)), for: .touchUpInside)
// Add the button to the stack
addArrangedSubview(button)
// Add the new button to the rating button array
ratingButtons.append(button)
}
updateButtonSelectionStates()
}
private func updateButtonSelectionStates() {
for (index, button) in ratingButtons.enumerated() {
// If the index of a button is less than the rating, that button should be selected.
button.isSelected = index < rating
// Set accessibility hint and value
let hintString: String?
if rating == index + 1 {
hintString = "Tap to reset the rating to zero."
} else {
hintString = nil
}
let valueString: String
switch (rating) {
case 0:
valueString = "No rating set."
case 1:
valueString = "1 star set."
default:
valueString = "\(rating) stars set."
}
button.accessibilityHint = hintString
button.accessibilityValue = valueString
}
}
}
@IBDesignable class RatingControl:UIStackView{
//马克:财产
私有变量比率按钮=[UIButton]()
var评级=0{
迪塞特{
updateButtonSelectionStates()
}
}
@IBInspectable var starSize:CGSize=CGSize(宽:44.0,高:44.0){
迪塞特{
设置按钮()
}
}
@IBInspectable var starCount:Int=5{
迪塞特{
设置按钮()
}
}
//标记:初始化
重写初始化(帧:CGRect){
super.init(frame:frame)
设置按钮()
}
必需初始化(编码者:NSCoder){
super.init(编码器:编码器)
设置按钮()
}
//标记:按钮动作
功能分级按钮已标记(按钮:UIButton){
防护装置let index=额定按钮。索引(of:按钮)else{
fatalError(“按钮\(按钮)不在ratingButtons数组中:\(ratingButtons)”)
}
//计算所选按钮的额定值
让selectedRating=index+1
如果选择了额定值==额定值{
//如果选定的星形代表当前额定值,请将额定值重置为0。
评级=0
}否则{
//否则,将额定值设置为选定的星形
评级=选择评级
}
打印(已选择打印)
}
//标记:私有方法
专用功能设置按钮(){
//清除所有现有按钮
用于分级按钮中的按钮{
removeArrangedSubview(按钮)
按钮。removeFromSuperview()
}
ratingButtons.removeAll()
//加载按钮图像
let bundle=bundle(for:type(of:self))
让filledStar=UIImage(名为:“filledStar”,in:bundle,compatibleWith:self.traitCollection)
让emptyStar=UIImage(名为:“emptyStar”,位于:bundle中,与:self.traitCollection兼容)
让highlightedStar=UIImage(名为:“highlightedStar”,位于:bundle中,与:self.traitCollection兼容)
对于0..中的索引,也许您应该创建协议分级控制委托
:
@objc protocol RatingControlDelegate: NSObjectProtocol {
@objc optional func ratingButtonTapped(_ ratingControl: RatingControl)
}
然后将委托
变量添加到评级控制
:
var delegate: RatingControlDelegate?
然后在分级按钮标记(:)
的末尾添加委托方法:
在DetailsViewController
viewdiload(:))
:
在DetailsViewController
中RatingControlDelegate
的实现中,您将拥有ratingControl.rating
变量:
func ratingButtonTapped(_ ratingControl: RatingControl) {
ratingControl.rating // shows your rating
// detect which instance has been clicked
switch ratingControl {
case ratingControl1:
case ratingControl2:
// etc.
}
}
也许您应该创建协议分级控制委托
:
@objc protocol RatingControlDelegate: NSObjectProtocol {
@objc optional func ratingButtonTapped(_ ratingControl: RatingControl)
}
然后将委托
变量添加到评级控制
:
var delegate: RatingControlDelegate?
然后在分级按钮标记(:)
的末尾添加委托方法:
在DetailsViewController
viewdiload(:))
:
在DetailsViewController
中RatingControlDelegate
的实现中,您将拥有ratingControl.rating
变量:
func ratingButtonTapped(_ ratingControl: RatingControl) {
ratingControl.rating // shows your rating
// detect which instance has been clicked
switch ratingControl {
case ratingControl1:
case ratingControl2:
// etc.
}
}
我在我的项目中使用了HCSStaratingView来显示星率获取
你可以在下面的链接中查看相同的内容
它为我提供了明星价值
(例如viwFeedBackStar.值)
我知道根据您的实现,它是不同的库,但它确实有效
为了在ios中获取星光率,我使用了HCSStaratingView在我的项目中显示星光率获取
你可以在下面的链接中查看相同的内容
它为我提供了明星价值
(例如viwFeedBackStar.值)
我知道根据您的实现,它是不同的库,但它确实有效
要在ios中获取星级ratting,您应该使用StoreKit中苹果推荐的分级视图控制器“SKStoreReviewController”
您应该使用StoreKit中苹果推荐的分级视图控制器“SKStoreReviewController”
无需对代码进行任何更改。只需使用.rating属性访问当前额定值。
所以在ViewController类中,当您需要访问额定值时,只需编写
print(ratingControl1.rating)
print(ratingControl2.rating)
但是,如果您需要在用户点击任何评级控件时访问评级值,则可以使用答案修改代码。无需对代码进行任何更改。只需使用.rating属性访问当前评级值。
所以在ViewController类中,当您需要访问额定值时,只需编写
print(ratingControl1.rating)
print(ratingControl2.rating)
但是,如果您需要在用户点击任何评级控件时访问评级值,那么请修改您的代码和答案。您使用的是哪种回购?应该有一些函数或属性来访问提供的评级当前值。评级控件类中有一个函数评级按钮,但我不知道如何从ViewController类中捕获单击按钮的时间以及如何区分单击了哪个实例按钮。在此处共享repo链接。检查我的答案。您正在使用哪个repo?应该有一些函数或属性来访问提供的当前评级值。RatingControl类中有一个函数RatingButton,但我不知道如何从ViewControlle中捕获单击按钮时的r类,以及如何区分单击了哪个实例按钮。此处共享回购链接。检查我的答案。谢谢。这很简单,也是我一直在寻找的方法。谢谢。这很简单,也是我一直在寻找的方法。