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:
UIImageView
. In the Attributes Inspector on the right, set its image to your star.png
image and check User Interaction Enabled . StackView
by selecting Editor->Embed In->Stack View from the menu. Choose Fill Alignment and Fill Proportionally for Distribution. 5:1
. Give it constraints to place it on the page and to define its width. @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. 1
through 5
to your star images. 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.