简体   繁体   中英

How to group multiple imageview to have 1 tap gesture recognizer?

I have 5 imageviews in it. I need to get id of the imageview that is tapped. I tried adding tap gesture identifier and get sender's id, but I am not getting it.

What I have tried till now is as follows

@IBOutlet weak var userRateViewStar1ImageOutlet         : UIImageView!

@IBOutlet weak var userRateViewStar2ImageOutlet         : UIImageView!

@IBOutlet weak var userRateViewStar3ImageOutlet         : UIImageView!

@IBOutlet weak var userRateViewStar4ImageOutlet         : UIImageView!

@IBOutlet weak var userRateViewStar5ImageOutlet         : UIImageView!

@IBOutlet weak var userRateViewTextAreaOutlet           : UITextView!

@IBOutlet var starOutletCollectionOutlet                : [UIImageView]!


 @IBAction func starRatingAction(_ sender: UITapGestureRecognizer)
{
    var tag = sender.view!.tag

    print(tag)

}

Basically I am trying to implement rate feature.

So can someone suggest me a way to implement this?

You could just use UIButton s.

If you want to use UIImageView s, here's how you do it:

  1. Drag out a UIImageView . In the Attributes Inspector on the right, set its image to your star.png image and check User Interaction Enabled .
  2. Make four copies with copy ( command -C) and paste ( command -V). Put them in a row, select them all and embed them in a StackView by selecting Editor->Embed In->Stack View from the menu. Choose Fill Alignment and Fill Proportionally for Distribution.
  3. Give your Stack View an Aspect Ratio constraint of 5:1 . Give it constraints to place it on the page and to define its width.
  4. Every image will need its own Tap Gesture Recognizer but they can all be hooked up to the same @IBAction routine. Drag out Tap Gesture Recognizer s and drop them on each image. Find the 5 Tap Gesture Recognizer s in the Document Outline View and control -drag from each of them to your @IBAction routine.
  5. Assign tags 1 through 5 to your star images.
  6. If you need @IBOutlet s to your UIImageView s, you can connect each of them to the same Outlet Collection such as your:

     @IBOutlet var starOutletCollection : [UIImageView]! 

    then you can loop through them to change their images, for instance:

     // turn off all of the stars for star in starOutletCollection { star.image = UIImage(named: starOff.png) } 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM