RatingControl - 捕获从viewController单击的星数

时间:2017-09-15 06:05:56

标签: ios swift

我在我的应用程序中使用RatingControl类(在gitHub上找到)来记录4个条件(实例)的反馈。我必须分别从viewController中捕获每个实例中点击的星数。如何捕获捕获从viewController单击的星数,并区分已单击的实例。我相信我必须使用RatingControl类中的ratingButtonTapped函数,但不知道如何实现这一点。

下面的ViewController代码片段:

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

下面的RatingControl类:

@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
    }
  }
}

4 个答案:

答案 0 :(得分:1)

也许您应该创建协议RatingControlDelegate

@objc protocol RatingControlDelegate: NSObjectProtocol {
    @objc optional func ratingButtonTapped(_ ratingControl: RatingControl)
}

然后在delegate中添加RatingControl var:

var delegate: RatingControlDelegate?

然后在ratingButtonTapped(:_)

的末尾添加委托方法
func ratingButtonTapped(button: UIButton) {
    // ... end of func
    delegate?.ratingButtonTapped?(self)
}

DetailsViewController viewDidLoad(:_)

ratingControl1.delegate = self
ratingControl2.delegate = self
ratingControl3.delegate = self
ratingControl4.delegate = self

RatingControlDelegate的{​​{1}}实施中,您将DetailsViewController变量:

ratingControl.rating

答案 1 :(得分:1)

我使用HCSStarRatingView在我的项目中显示星级率 您可以查看以下链接。

它为我提供星级价值 (例如viwFeedBackStar.value)

https://github.com/hsousa/HCSStarRatingView

我知道根据你的实现它是不同的库,但它确实有效

在ios中获取明星评分

答案 2 :(得分:1)

无需对代码进行任何更改。只需使用.rating属性访问当前评级值。

因此,在ViewController类中,当您需要访问评级值时,只需编写

print(ratingControl1.rating)
print(ratingControl2.rating)

但如果您需要在用户点击任何评级控件时访问评级值,请使用Aleksandr Zarubin回答来修改您的代码。

答案 3 :(得分:0)

你应该使用苹果推荐的评级视图控制器&#34; SKStoreReviewController&#34;来自StoreKit

https://developer.apple.com/documentation/storekit/skstorereviewcontroller