How to get indexPath when image inside cell tapped
1: set tag of ImageView in cellForRowAtIndexpath method , tag will be equal to indexPath.row
imgView.tag = indexPath.row
2: add a target to tapGuestureRecognizer attached on imageView
3: get tag of imageView in that method
let imgView = sender as! UIImageView
let tag = imgView.tag
4: get data accordingly ,and push
let next = self.storyBoard.instatiateViewController(WithIdentifer:"detailVC")
next.data = datas[tag]
self.navigationController.pushViewController(next)
Tap on cell and get index
Think about it. The sender
is the tap gesture recognizer. The g.r.'s view
is the cell. Now you can ask the collection view what the index path of this cell is (indexPathForCell:
).
Find sender's tag with gesture recognizer and swift
I think this code does what you want. I commented some stuff out, since I didn't know what it was, and I hard coded the image and text in the label. As I said in my comment, you don't need to use tags, since each recognizer knows its own view. I commented out the button stuff, but I do see the "if" and "else" log statements fire as I move the views up and down the screen,
class ViewController: UIViewController {
@IBAction func showContent(sender: AnyObject) {
var rond1 = UIImageView(frame: CGRectMake(0, 0, 100, 100))
rond1.image = UIImage(named:"Lofoten.jpg")
var label = UILabel(frame: CGRectMake(0, 0, 200, 21))
label.center = CGPointMake(rond1.frame.origin.x+50, rond1.frame.origin.y+100)
label.textAlignment = NSTextAlignment.Center
label.text = "Picture"
label.textColor = UIColor.whiteColor()
label.frame = CGRectMake(0, rond1.frame.size.height - 25, 100, 25)
rond1.addSubview(label)
rond1.userInteractionEnabled = true
view.addSubview(rond1)
let recognizer = UIPanGestureRecognizer(target: self, action:Selector("handlePan:"))
rond1.addGestureRecognizer(recognizer)
}
func handlePan(recognizer:UIPanGestureRecognizer) {
let iv = recognizer.view
let translation = recognizer.translationInView(self.view)
iv.center.x += translation.x
iv.center.y += translation.y
recognizer.setTranslation(CGPointZero, inView: self.view)
// var switchRang = premierRang
var centerBoardY = self.view.center.y
var centerRondY = iv.center.y
//DistanceCenterY.text = " \(centerRondY - centerBoardY)"
if centerRondY - centerBoardY < 100 {
//switchRang.setOn(true, animated: true)
println("dans switch if")
} else {
//switchRang.setOn(false, animated: true)
println("dans switch else")
}
}
}
recognize which UIImageView has been tapped and get its index in an array
Since you already have an array storing all your images, it's fairly easy to know what image (and index) has been tapped by searching the image index within houseImages
.
Because you don't need complex verifications to get your index with firstIndex(where: [...])
, I would suggest to simply use firstIndex(of: UIImageView)
in your case.
@IBAction func imageTapped(recognizer: UIGestureRecognizer) {
guard let tappedView = recognizer.view as? UIImageView else {
return
}
let index = self.houseImages.firstIndex(of: tappedView)
print("House image index = \(index)")
}
How to get the indexpath.row when a UIImageView in a cell is tapped?
this might help you,
add an UITapGestureRecognizer
to UIImageView
You can store indexpath.row
in tag property of UIImageView
and access that tag on UITapGestureRecognizer
event
for example (Objective-C) :
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleImageTap:)];
tap.cancelsTouchesInView = YES;
tap.numberOfTapsRequired = 1;
[cell.imageView addGestureRecognizer:tap];
cell.imageView.tag = indexPath.row;
and get indexpath.row
-(void)handleImageTap:(UITapGestureRecognizer *)gestureRecognizer{
UIView* view = gestureRecognizer.view;
CGPoint loc = [gestureRecognizer locationInView:view];
NSInteger indexpath = [view hitTest:loc withEvent:nil].tag;
NSLog(@"%ld",(long)indexpath);
}
How to get cell on label or imageview tap - UITapGestureRecognizer
Am not really fond of playing with points and Superview. What is can suggest is to make a class for UITapGestureRecognizer as follows which can hold extra data. In your case it would be an index path
class CustomGesture: UITapGestureRecognizer {
let indexPath:NSIndexPath? = nil
}
And then in your didSelect
you can add the index path to the newly created CustomGesture class which be would be like:
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{
cell.imageView.userInteractionEnabled = true
let tapImageView = CustomGesture(target: self, action: #selector(HomeFeedViewController.tapImageView(_:)))
tapImageView.indexPath = indexPath// Add the index path to the gesture itself
cell.imageView.addGestureRecognizer(tapImageView)
return cell as CastleCell
}
Now since you have added the indexPath you don't need to play around with super view's and you can access the cell like this:
func tapImageView(gesture: CustomGesture) {
let indexPath = gesture.indexPath!
let cell = self.tableView.cellForRowAtIndexPath(indexPath!) as! CastleCell
performSegueWithIdentifier("SegueName", sender: self)
}
Swift 3 - Image view with Tap Gesture
Following code may help you more with Swift 4.
As you said you want to detect image tap on tableview cell please go through this code:
let tapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(ViewController.cellTappedMethod(_:)))
cell.yourImageView.isUserInteractionEnabled = true
cell.yourImageView.tag = indexPath.row
cell.yourImageView.addGestureRecognizer(tapGestureRecognizer)
And add below method to your ViewController:
@objc func cellTappedMethod(_ sender:AnyObject){
print("you tap image number: \(sender.view.tag)")
}
Related Topics
Uipageviewcontroller Gesture Is Calling Viewcontrollerafter: But Doesn't Animate
Objective C- Trouble Updating UI on Main Thread
Swift 3- How to Get Button in Uicollectionviewcell Work
A Lighter Way of Discovering Text Writing Direction
Uisegmentedcontrol iOS 13 Clear Color
How to Show Specific Language Keyboard When User Input Values in Uitextfield in iPhone App
Programmatically Creating Uinavigationcontroller in iOS
Uisplitviewcontroller on iPad with Storyboards
Will iOS Awake My App When I Receive Silent Push Notification(When App Is Not in Running State)
How to Use Phcachingimagemanager
Allow Full Access Check in Keyboards iOS10
Preventing Avcapturevideopreviewlayer from Rotating, But Allow UI Layer to Rotate with Orientation
Obtaining Bluetooth Le Scan Response Data with iOS
How to Load Multiple Storyboard Files Depending on iOS Version? (5 and 6)