Question

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

Était-ce utile?

La 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....

Autres conseils

Create a button and set image as background.while creating button you can set tag like

button.tag=yourtag;//your tag integer value 
[button addTarget:self action:@selector(buttonTouched:) forControlEvents:UIControlEventTouchDown];

  [button setBackgroundImage:[UIImage imageNamed:@"img.png"] forState:UIControlStateNormal];

and implement this function in your clss

 - (void)buttonTouched:(UIButton *)sender 
   {
NSLog(@"touched %i",[sender tag]);
   }    

while tapping the particular button this function will get called.

Rather than use image views, you could use UIButtons with image views as their content.

That way, you'll get a callback when a given image is tapped with a reference to the button. From there you should be able to get the tag of that which has been tapped!

I hope this helps,

You can do according to what Nick has suggested. Or else, you can create a subclass of the imageview. Set frame and tag to it. In touchesEnded method of this custom class, you can find which imageview it is based on its tag.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top