如何在棒球计数上做空圈?

时间:2016-01-20 19:31:00

标签: ios rating

我根据FoodTracker源代码制作棒球outcount代码。这是我的代码,但空代码在此代码中不计算在内。如何制作空圈评级?如果你们能帮助我,我将不胜感激。谢谢!!

///////

import UIKit

class RatingControl: UIView {
    // MARK: Properties

    var rating = 0 {
        didSet {
            setNeedsLayout()
        }
    }
    var ratingButtons = [UIButton]()
    var spacing = 3
    var circles = 3

    // MARK: Initialization

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)

        let filledCircleImage = UIImage(named: "checkedRed")
        let emptyCircleImage = UIImage(named: "UncheckedRed")

        for _ in 0..<3 {
            let button = UIButton()

            button.setImage(emptyCircleImage, forState: .Normal)
            button.setImage(filledCircleImage, forState: .Selected)
            button.setImage(filledCircleImage, forState: [.Highlighted, .Selected])

            button.adjustsImageWhenHighlighted = false

            button.addTarget(self, action: "ratingButtonTapped:", forControlEvents: .TouchDown)
            ratingButtons += [button]
            addSubview(button)
        }
    }

    override func layoutSubviews() {
        // Set the button's width and height to a square the size of the frame's height.
        let buttonSize = Int(frame.size.height)
        var buttonFrame = CGRect(x: 0, y: 0, width: buttonSize, height: buttonSize)

        // Offset each button's origin by the length of the button plus spacing.
        for (index, button) in ratingButtons.enumerate() {
            buttonFrame.origin.x = CGFloat(index * (buttonSize + spacing))
            button.frame = buttonFrame
        }
        updateButtonSelectionStates()
    }

    override func intrinsicContentSize() -> CGSize {
        let buttonSize = Int(frame.size.height)
        let width = (buttonSize + spacing) * circles

        return CGSize(width: width, height: buttonSize)
    }

    // MARK: Button Action

    func ratingButtonTapped(button: UIButton) {


            rating = ratingButtons.indexOf(button)! + 1
            updateButtonSelectionStates()

           }

    func updateButtonSelectionStates() {
        for (index, button) in ratingButtons.enumerate() {
            // If the index of a button is less than the rating, that button should be selected.
            button.selected = index < rating
        }
    }
}

1 个答案:

答案 0 :(得分:0)

如果您想要零计数,则返回0作为可能的按钮操作。您的评级已经设置为零,所以从技术上来说它确实很重要。如果您不小心点击了第一个圈并想要撤消它,您可以在ratingButtonTapped中尝试类似这样的内容

//rating = ratingButtons.index(of: button)! + 1
rating = (rating == 1) && (ratingButtons.index(of: button) == 0) ? 0 : (ratingButtons.index(of: button)! + 1)