peterprokop / StarryStars

StarryStars is iOS GUI library for displaying and editing ratings
MIT License
174 stars 23 forks source link

How can I set a value on click ? #4

Closed nahouto closed 8 years ago

nahouto commented 8 years ago

Hi,

Thank you for your easy to use pod !

I would like to allow the user to set a rating value when clicking on a star. How can I do that ?

Thanks.

peterprokop commented 8 years ago

Hello,

Can you describe in more detail what do you have in mind? You want to show some popup when one of the stars is tapped?

On Thu, Mar 17, 2016 at 11:14 AM, nahouto notifications@github.com wrote:

Hi,

Thank you for your easy to use pod !

I would like to allow the user to set a rating value when clicking on a star. How can I do that ?

Thanks.

— You are receiving this because you are subscribed to this thread. Reply to this email directly or view it on GitHub https://github.com/peterprokop/StarryStars/issues/4

All the best, Peter Prokop.

nahouto commented 8 years ago

Hello, Actually, the user can select a rate by "sliding" to the right (in RatingView.swift, you in fact commented "rating when user touches begin/move/end") I would like to allow him to select a rate just by tapping a star.

nahouto commented 8 years ago

Ok, I made it by adding a tap recognizer in customInit :

// add tap recognizer
let gestureRecognizer = UITapGestureRecognizer(target: self, action: "handleTap:")
self.addGestureRecognizer(gestureRecognizer)

and adding the following function :

/**
 Compute and adjust rating when user taps
 */
func handleTap(gestureRecognizer: UIGestureRecognizer) {
    let touchLocation = gestureRecognizer.locationInView(self)

    for var i = starCount - 1; i >= 0; i-- {
        let imageView = stars[i]

        let x = touchLocation.x;

        if x >= imageView.center.x {
            rating = Float(i) + 1
            return
        } else if x >= CGRectGetMinX(imageView.frame) && halfStarsAllowed {
            rating = Float(i) + 0.5
            return
        }
    }

    rating = 0
}