Adding touch events will be used so that the user can change the rating to their desired rating. Open RatingView. Let's add the methods we need to get our control to accept touch events. Start by adding the following inside the main class:
override var canBecomeFirstResponder: Bool {
return shouldBecomeFirstResponder
}
override func beginTracking(_ touch: UITouch, with event: UIEvent?) -> Bool {
if self.isEnabled {
super.beginTracking(touch, with: event)
if (shouldBecomeFirstResponder && self.isFirstResponder) {
becomeFirstResponder()
}
handle(with: touch)
return true
} else {
return false
}
}
Then, add the following in the private extension:
func handle(with touch: UITouch) {
let cellWidth = self.bounds.size.width / CGFloat(totalStars)
let location = touch.location...