开发者

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

开发者 https://www.devze.com 2023-02-12 19:53 出处:网络
I want to used image view from nos. of image views in scro开发者_JAVA百科ll view on single tapping any particular image view.If you are insistent on using images instead of buttons you can use Gesture

I want to used image view from nos. of image views in scro开发者_JAVA百科ll view on single tapping any particular image view.


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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消