2016-01-20 38 views
0

我正在制作基于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 
     } 
    } 
} 

回答

0

,如果你想零计数,然后作为一个可能的按钮操作返回0。您的评分已设置为零,因此在技术上它不计数。如果你不小心点击第一个圆圈并想撤消它,你可以尝试类似这样的评级按钮已打开

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