Skip to content

Commit

Permalink
– adds boolean for haptic feedback generation
Browse files Browse the repository at this point in the history
– adds property to adjust spacing between stars
  • Loading branch information
hujaber committed Oct 15, 2019
1 parent ade32f4 commit c5a040c
Showing 1 changed file with 31 additions and 7 deletions.
38 changes: 31 additions & 7 deletions SwiftyStars/StarsView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@

import UIKit

final class StarsView: UIView {
public class StarsView: UIView {

/// The total number of stars in the view. Default value is 5
@IBInspectable public var numberOfStars: Int = 5 {
didSet {
var arr = [UIImageView]()
Expand All @@ -23,30 +24,48 @@ final class StarsView: UIView {
}
}

/// Set the current rating, default is 3.
public var currentRating = 3 {
didSet {
updateCurrentStars(oldValue: oldValue)
}
}

/// Indicates if a haptic feedback should be generated when tapping a star. Default is true
@IBInspectable public var generateHapticFeedbackOnSelection: Bool = true

/// Private: The selected rating,
private(set) var selectedRating: Int = 1 {
didSet {
if selectedRating != oldValue {
UISelectionFeedbackGenerator().selectionChanged()
if generateHapticFeedbackOnSelection {
UISelectionFeedbackGenerator().selectionChanged()
}
didChangeSelection?(selectedRating)
}
}
}

/// The color of the star
@IBInspectable public var starColor: UIColor? {
didSet {
imageViews.forEach({ $0.tintColor = starColor })
}
}

/// Closure indicating that the selection did change, returns the value
/// of the selection
public var didChangeSelection: ((Int) -> Void)?


/// Used to adjust the spacing distance between stars
@IBInspectable public var starsSpacing: NSNumber? {
didSet {
stackView.spacing = CGFloat(starsSpacing!.floatValue)
}
}

/// StackView that holds imageviews
private let stackView: UIStackView = {
let stackView = UIStackView()
stackView.translatesAutoresizingMaskIntoConstraints = false
Expand All @@ -56,6 +75,7 @@ final class StarsView: UIView {
return stackView
}()

/// Image view initiated with empty stars images
private var emptyStarImageView: UIImageView {
let imgView = UIImageView()
imgView.image = emptyImage
Expand All @@ -64,14 +84,17 @@ final class StarsView: UIView {
return imgView
}

private var filledImage: UIImage {
/// The image of the filled star
open var filledImage: UIImage {
UIImage(named: "starFilledLarge", in: Bundle(for: Self.self), compatibleWith: nil)!.withRenderingMode(.alwaysTemplate)
}

private var emptyImage: UIImage {
/// The image of the empty star
open var emptyImage: UIImage {
UIImage(named: "starEmptyLarge", in: Bundle(for: Self.self), compatibleWith: nil)!.withRenderingMode(.alwaysTemplate)
}

/// Initial image views
private lazy var imageViews: [UIImageView] = {
var arr = [UIImageView]()
for i in 0..<numberOfStars {
Expand All @@ -80,6 +103,7 @@ final class StarsView: UIView {
return arr
}()


override init(frame: CGRect) {
super.init(frame: frame)
setup()
Expand All @@ -90,7 +114,7 @@ final class StarsView: UIView {
setup()
}

override func awakeFromNib() {
override public func awakeFromNib() {
super.awakeFromNib()
setup()
}
Expand All @@ -111,12 +135,12 @@ final class StarsView: UIView {
}


override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
override public func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
super.touchesBegan(touches, with: event)
handleTouches(touches)
}

override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
override public func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
super.touchesMoved(touches, with: event)
handleTouches(touches)
}
Expand Down

0 comments on commit c5a040c

Please sign in to comment.