提交 6b3d9f91 编写于 作者: J John Hampton

Adds property observers

上级 4823baec
...@@ -18,8 +18,8 @@ ...@@ -18,8 +18,8 @@
<rect key="frame" x="0.0" y="0.0" width="375" height="667"/> <rect key="frame" x="0.0" y="0.0" width="375" height="667"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/> <autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<subviews> <subviews>
<stackView opaque="NO" contentMode="scaleToFill" ambiguous="YES" axis="vertical" alignment="top" spacing="8" translatesAutoresizingMaskIntoConstraints="NO" id="l36-fK-dfQ"> <stackView opaque="NO" contentMode="scaleToFill" axis="vertical" alignment="top" spacing="8" translatesAutoresizingMaskIntoConstraints="NO" id="l36-fK-dfQ">
<rect key="frame" x="0.0" y="40" width="375" height="542.5"/> <rect key="frame" x="0.0" y="40" width="375" height="532.5"/>
<subviews> <subviews>
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="Meal Name" textAlignment="natural" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="n9u-9b-TSl"> <label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="Meal Name" textAlignment="natural" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="n9u-9b-TSl">
<rect key="frame" x="0.0" y="0.0" width="86.5" height="20.5"/> <rect key="frame" x="0.0" y="0.0" width="86.5" height="20.5"/>
...@@ -50,8 +50,16 @@ ...@@ -50,8 +50,16 @@
<outletCollection property="gestureRecognizers" destination="UAM-fK-D64" appends="YES" id="uEG-WL-m3E"/> <outletCollection property="gestureRecognizers" destination="UAM-fK-D64" appends="YES" id="uEG-WL-m3E"/>
</connections> </connections>
</imageView> </imageView>
<stackView opaque="NO" contentMode="scaleToFill" ambiguous="YES" translatesAutoresizingMaskIntoConstraints="NO" id="5FE-RR-A0X" customClass="RatingControl" customModule="iOSTemplate" customModuleProvider="target"> <stackView opaque="NO" contentMode="scaleToFill" spacing="8" translatesAutoresizingMaskIntoConstraints="NO" id="5FE-RR-A0X" customClass="RatingControl" customModule="iOSTemplate" customModuleProvider="target">
<rect key="frame" x="0.0" y="432.5" width="200" height="110"/> <rect key="frame" x="0.0" y="432.5" width="132" height="100"/>
<userDefinedRuntimeAttributes>
<userDefinedRuntimeAttribute type="size" keyPath="starSize">
<size key="value" width="20" height="100"/>
</userDefinedRuntimeAttribute>
<userDefinedRuntimeAttribute type="number" keyPath="starCount">
<integer key="value" value="5"/>
</userDefinedRuntimeAttribute>
</userDefinedRuntimeAttributes>
</stackView> </stackView>
</subviews> </subviews>
<constraints> <constraints>
......
...@@ -7,7 +7,22 @@ ...@@ -7,7 +7,22 @@
import UIKit import UIKit
class RatingControl: UIStackView { @IBDesignable class RatingControl: UIStackView {
// MARK: Properties
private var ratingButtons = [UIButton]()
@IBInspectable var starSize: CGSize = CGSize(width: 44.0, height: 44.0) {
didSet {
setupButtons()
}
}
@IBInspectable var starCount: Int = 5 {
didSet {
setupButtons()
}
}
var rating = 0
// MARK: Initialization // MARK: Initialization
override init(frame: CGRect) { override init(frame: CGRect) {
...@@ -27,20 +42,32 @@ class RatingControl: UIStackView { ...@@ -27,20 +42,32 @@ class RatingControl: UIStackView {
// MARK: Private Methods // MARK: Private Methods
private func setupButtons() { private func setupButtons() {
// Create the button // clear any existing buttons
let button = UIButton() for button in ratingButtons {
button.backgroundColor = UIColor.red removeArrangedSubview(button)
button.removeFromSuperview()
// Add constraints }
button.translatesAutoresizingMaskIntoConstraints = false ratingButtons.removeAll()
button.heightAnchor.constraint(equalToConstant: 44.0).isActive = true
button.widthAnchor.constraint(equalToConstant: 44.0).isActive = true
// Setup the button action
button.addTarget(self, action: #selector(RatingControl.ratingButtonTapped(button:)), for: .touchUpInside)
// Add the button to the stack for _ in 0..<starCount {
addArrangedSubview(button) // Create the button
let button = UIButton()
button.backgroundColor = UIColor.red
// Add constraints
button.translatesAutoresizingMaskIntoConstraints = false
button.heightAnchor.constraint(equalToConstant: starSize.height).isActive = true
button.widthAnchor.constraint(equalToConstant: starSize.width).isActive = true
// 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)
}
} }
} }
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册