Skip to content

Commit

Permalink
✨ RatingView addible in interface builder
Browse files Browse the repository at this point in the history
  • Loading branch information
ebPark9511 committed Sep 4, 2021
1 parent 239a7d5 commit 131b9fb
Show file tree
Hide file tree
Showing 2 changed files with 108 additions and 40 deletions.
6 changes: 3 additions & 3 deletions Soyeon/Common/RatingView/RatingStackView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -86,11 +86,11 @@ class RatingStackView: UIStackView {
}

func changeIndex(to index: Int) {
guard self.index != index, 0..<count ~= index else {
guard self.index != index, 0 <= index else {
return
}
self.index = index

self.index = min(index, count-1)
}

private func setHighlight(until i: Int) {
Expand Down
142 changes: 105 additions & 37 deletions Soyeon/Common/RatingView/RatingView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,39 +7,67 @@
//

import UIKit

//: Sample

protocol RatingViewDelegate: AnyObject {
func didChangeIndex(_ index: Int)
}

final class RatingView: UIView {

private var stackView: RatingStackView!

convenience init(named: String,
highlightColor: UIColor,
count: Int) {
self.init(frame: .zero)
weak var delegate: RatingViewDelegate?

private var imageViewAttributes: RatingImageViewsAttributes = .init()

private var stackViewAttributes: RatingStackViewAttributes = .init()

override func awakeFromNib() {
super.awakeFromNib()

setupLayout()

let imageViews = initImageViews(attributes: imageViewAttributes)

setupStackView(with: imageViews, attributes: stackViewAttributes)

addSubview(stackView)
setupStackViewConstraint()
}

private func setupLayout() {

translatesAutoresizingMaskIntoConstraints = false


tintColor = imageViewAttributes.highlightColor
}

private func initImageViews(attributes: RatingImageViewsAttributes) -> [UIImageView] {
let imageName = attributes.imageName
let highlightColor = attributes.highlightColor
let count = attributes.count

let imageViews: [UIImageView] = (0..<count).map { _ in
let image = UIImage(named: named)
let image = UIImage(named: imageName)
let highlight = image?.imageColor(to: highlightColor)

let iv = UIImageView(image: image,
highlightedImage: highlight)
return iv
}

return imageViews
}

private func setupStackView(with imageViews: [UIImageView], attributes: RatingStackViewAttributes) {
stackView = RatingStackView(arrangedImageSubviews: imageViews)
stackView.spacing = 13.0
stackView.delegate = self

stackView.changeIndex(to: 0)
stackView.spacing = attributes.spacing

setupStackViewConstraint()
stackView.delegate = self

layoutIfNeeded()
stackView.changeIndex(to: attributes.index)
}

private func setupStackViewConstraint() {
addSubview(stackView)

let constraints = [
stackView.leftAnchor.constraint(equalTo: leftAnchor),
Expand All @@ -50,33 +78,73 @@ final class RatingView: UIView {

NSLayoutConstraint.activate(constraints)
}

}

extension RatingView: RatingStackDelegate {
func didChangeIndex(_ index: Int) {
print(index)
delegate?.didChangeIndex(index)
}
}

// MARK: - Stack View
extension RatingView {
/// user defined runtime attributes
struct RatingStackViewAttributes {
var index: Int
var spacing: CGFloat

init() {
self.index = 0
self.spacing = 13.0
}
}

@objc var index: NSNumber {
get { NSNumber(value: stackViewAttributes.index) }
set { stackViewAttributes.index = newValue.intValue }
}

@objc var spacing: NSNumber {
get { NSNumber(value: Float(stackViewAttributes.spacing)) }
set { stackViewAttributes.spacing = CGFloat(newValue.floatValue) }
}
}

// MARK: - Rating Image View
extension RatingView {
/// user defined runtime attributes
struct RatingImageViewsAttributes {
var imageName: String
var highlightColor: UIColor
var count: Int

init() {
self.imageName = ""
self.highlightColor = UIColor.blue
self.count = 5
}
}

@objc var imageName: NSString {
get { NSString(string: imageViewAttributes.imageName) }
set { imageViewAttributes.imageName = String(newValue) }
}

@objc var highlightColor: UIColor {
get { imageViewAttributes.highlightColor }
set { imageViewAttributes.highlightColor = newValue }
}

@objc var count: NSNumber {
get { NSNumber(value: imageViewAttributes.count) }
set { imageViewAttributes.count = newValue.intValue }
}

}

fileprivate extension UIImage {
func imageColor(to color: UIColor) -> UIImage? {
let image = withRenderingMode(.alwaysTemplate)
return image.withTintColor(color)
}
func imageColor(to color: UIColor) -> UIImage? {
let image = withRenderingMode(.alwaysTemplate)
return image.withTintColor(color)
}
}

/*
let ratingView = RatingView(named: "btn_star_rate.png",
highlightColor: Colors.soyeonBlue.color(),
count: 5)

self.view.addSubview(ratingView)

let constraints = [
ratingView.centerXAnchor.constraint(equalTo: view.centerXAnchor),
ratingView.centerYAnchor.constraint(equalTo: view.centerYAnchor)
]

NSLayoutConstraint.activate(constraints)
*/

0 comments on commit 131b9fb

Please sign in to comment.