get index or tag value from imageview tap gesture

2020-02-04 10:56发布

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.

6条回答
劫难
2楼-- · 2020-02-04 11:27

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.

查看更多
Explosion°爆炸
3楼-- · 2020-02-04 11:35

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"]];
     }
}
查看更多
Animai°情兽
4楼-- · 2020-02-04 11:36

I have resolved the issue and really thanks to all to look upon my issue.

查看更多
闹够了就滚
5楼-- · 2020-02-04 11:43

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);
}
查看更多
Summer. ? 凉城
6楼-- · 2020-02-04 11:49

For those using storyboards and swift:

  1. Change tag of every UIView or UIImageView in IB (interface builder) ex:

    view1.tag = 1
    view2.tag = 2
    view3.tag = 3
    ...
    
  2. Still in storyboard add UITapGestureRecognizer to every UIView you have put in storyboard view (view1, view2, view3 ...) :

screenshot of UITapGestureRecognizers (9 in picture)

  1. Write your function in Swift file:

// code in swift file :

    @IBAction func viewTapped(sender: AnyObject) {
            // You can write anything here. I'm guessing you need something with 'enable/disable' function, and the best call is the CustomView which you create from File > New file
            print("tapped view is view with tag: \(sender.view!!.tag)")
        }
  1. Connect every TapGestureRecognizer to same function you have declared in parent UIViewController swift file:

Established connection between UITapGestureRecognizer and func written in swift file

  1. Done.
查看更多
Deceive 欺骗
7楼-- · 2020-02-04 11:54

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

查看更多
登录 后发表回答