Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

get index or tag value from imageview tap gesture

enter image description here

This is the image from app store whenever we search for any app. I also want to add the same scrollview concept in which it show the currentimage with small preview of previous and next image. I am able to make this view with the help of Sample code. But didn't find any solution that how to get index or tagvalue when user tap any of the image. So that I would be able to open the detail page for each image. If Someone has idea about this please help me.

Thanks in advance.

like image 733
Minkle Garg Avatar asked Jun 30 '13 11:06

Minkle Garg


4 Answers

Add gesture recognizer to the necessary image view:

UITapGestureRecognizer *frameTapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(frameTapGesture:)];
[_imgView addGestureRecognizer:frameTapGesture];

Then in the gesture handler get access to the view gesture recognizer attached to:

- (void)frameTapGesture:(UITapGestureRecognizer*)sender
{
    UIView *view = sender.view; //cast pointer to the derived class if needed
    NSLog(@"%d", view.tag);
}
like image 158
SoftDesigner Avatar answered Dec 30 '22 12:12

SoftDesigner


The Best way to achieve what you wish is to add a UITapGesture to your image:

let tapgesture = UITapGestureRecognizer(target: self, action: Selector("openImage:"))
tapgesture.numberOfTapsRequired = 1
//if you're using a collectionView or tableview add this code inside cellForItemAtIndexPath
cell.Img.addGestureRecognizer(tapgesture)
//otherwise
myImage..addGestureRecognizer(tapgesture)

Get the superview of your tap gesture, this will give you the correct indexPath. Try this:

func openImage(sender: UITapGestureRecognizer) 
{
     let point = sender.view
     let mainCell = point?.superview
     let main = mainCell?.superview
     let cell: myViewCell = main as! myViewCell
     let indexPath = collectionView.indexPathForCell(cell)
}

You can increase or reduce the superview's depending on you hierarchy level. Start from one superview and keep increasing till you get the indexPath

like image 20
Uriel Avatar answered Dec 30 '22 10:12

Uriel


At the risk of making a recommendation without seeing the full picture of your app, why not use a custom UIButton instead of UIImageViews for this? With a UIButton you can setup an action and pass the sender id, from which you can easily access your tags and pull the data from your array.

  UIButton *button = [[UIImageView alloc] initWithFrame:CGRectMake(10, i*100 + i*15, 300, 100)];
button.backgroundColor = [UIColor blueColor];
button.tag = i;
[button addTarget:self action:@selector(touchDown:) controlEvent:UIControlEventTouchDown]; 

And inside of the touchDown: method you simply have to cast the sender to a UIButton in order to access the tag

- (void)touchDown:(id)sender    
{
    UIButton* button = (UIButton*)sender;
    switch(button.tag)
    {
        case TAG1:
          break;
        //etc
    }    
}

OR check this out

UIScrollView *myScroll = [[UIScrollView alloc] initWithFrame: CGRectMake (0,100,200,30)];
NSMutableArray = *images = [NSMutableArray alloc] initWithObjects: img1,img2,img3,nil];


for (int i=0; i<3; i++)
{
    // set `imageView` frame 
    UIImageView *imageV = [[UIImageView alloc]initWithFrame:yourFrame];
    [imageV setImage:[images objectAtIndex:i]];
    [imageV setUserInteractionEnabled:YES];
    UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:     self action:@selector (getTag:)];
    [imageV addGestureRecognizer: singleTap];
    [myScroll addSubview:imageV];

}

set tag to images as per you want After adding all imageView successfully you get them click like this :-

-(void)getTag:(id)sender
{
     UIGestureRecognizer *recognizer = (UIGestureRecognizer*)sender;
     UIImageView *imageView = (UIImageView *)recognizer.view;

     if(imageView.image.tag==1)
     {
         [imageView setImage:[UIImage imageNamed:@"anyImage.png"]];
     }
}
like image 38
iAhmed Avatar answered Dec 30 '22 10:12

iAhmed


Suppose you have an array with the objects to display and a UIImageView to display the image, just do this when setting up the view with the index i:

imageView.tag = kImageTag + i; 

where kImageTag is any constant > 1 to make sure you don't get a 0 as the tag.

Now when the view is selected you can simply check for the tag of the image view.

like image 20
Mundi Avatar answered Dec 30 '22 11:12

Mundi