get index or tag value from imageview tap gesture

后端 未结 6 536
情深已故
情深已故 2020-12-09 11:04

\"enter

This is the image from app store whenever we search for any app. I also want t

相关标签:
6条回答
  • 2020-12-09 11:26

    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 ...) :

    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:

    1. Done.
    0 讨论(0)
  • 2020-12-09 11:27

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

    0 讨论(0)
  • 2020-12-09 11:28

    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"]];
         }
    }
    
    0 讨论(0)
  • 2020-12-09 11:42

    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.

    0 讨论(0)
  • 2020-12-09 11:50

    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

    0 讨论(0)
  • 2020-12-09 11:53

    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);
    }
    
    0 讨论(0)
提交回复
热议问题