iphoneobjective-cuiscrollviewimageviewtap

how can I get image tag from different image views on tapping on it


I want to used image view from nos. of image views in scroll view on single tapping any particular image view.


Solution

  • If you are insistent on using images instead of buttons you can use Gesture Recognizer. Create an imageView and enable its userInteraction

    UIImageView *testImageView              =   [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"someImage"]];
    testImageView.frame                     =   CGRectMake(30.0,30.0,60.0,40.0);
    testImageView.tag                       =   30;
    testImageView.userInteractionEnabled    =   TRUE;
    [tempPlotView addSubview: testImageView];
    [testImageView release];    
    

    Now allocate a gesture Recognizer object and add it to your imageView...

    UITapGestureRecognizer *testGesture =   [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(singletap:)];
    [testGesture setNumberOfTapsRequired:1];
    [testImageView addGestureRecognizer: testGesture];
    [testGesture     release];
    

    Now in the selector "singleTap" you can do whatever your action is..

    -(void)singleTap:(UIImageView*)sender{
        if(sender.tag == 30){
            //do your stuff here...
        }
    }
    

    Hope this help...cheers....