tags:

views:

48

answers:

1

Hi,

I have a uiscrollview and i am showing some dynamically generated uiimageview over it. Now i want to do an action on touch at any of these images. I tried using touchbegn method but it is not being called. Please advice me what i am doing wrong.

int numofRows = [arrMyPhotos count]/3;
    if([arrMyPhotos count]%3>0)
        numofRows++;
    scrlPhotoList.contentSize = CGSizeMake(320, numofRows*100);
    for(int i = 0;i<numofRows;i++) {
        x = 10;
        j = 0;
        while (j<3 && k<[arrMyPhotos count]) {
            NSString *pngFilePath = [[NSString stringWithFormat:@"%@/",docDir] stringByAppendingFormat:@"%@",[(NSDictionary*)[arrMyPhotos objectAtIndex:k] objectForKey:@"picName"]];
            NSLog(@"%@",pngFilePath);
            UIImage *img = [UIImage imageWithContentsOfFile:pngFilePath];
            if(img!=nil) {
                UIImageView *imgVw = [[UIImageView alloc] init];
                imgVw.image = [self imageByScalingAndCroppingForSize:CGRectMake(0, 0, 95, 95) image:img];
                imgVw.tag = k;
                imgVw.frame = CGRectMake(x, y, 95, 95);
                imgVw.userInteractionEnabled = TRUE;
                imgVw.multipleTouchEnabled = TRUE;

                [scrlPhotoList addSubview:imgVw];
                //k++;
                x = x+105;
                j++;
            }
            k++;
        }
        y = y+105;
    }
    [self.view addSubview:scrlPhotoList];



-(void)touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event {
    UITouch *touch = [[event allTouches] anyObject];
    //CGPoint touchLocation = [touch locationInView:self.view];

    if(([touch view]).tag>0) {
        NSLog(@"%d",([touch view]).tag);
    }
}
A: 

Your code seems to be in the UIControllerView, for handling touch events you need to extend the view you want to implement the touch on and the controller!. please read the tutorial:http://www.iphonesdkarticles.com/2008/09/multi-touch-tutorial-part-2.html

If what you want, is a basic touch events you'll need to have UIButton or some other UIView that supports touch events

Guy Ephraim
I am using the code on UIViewController, so u mean i should make a class likw PhotoView which should b subclass of uiimageview and there use this code??
pankaj
yes, or if you need simple events like touchUpInside you can put a transparent button over it
Guy Ephraim
yes i can do that but there could be 50-60 images or more as it is going to b photo library, will that be ok?....... or i may even have button with background set as image
pankaj
ok i did that, created subclass of uiimageview and the touch event is working now, thanks guy!!
pankaj
Great, please accept my answer.
Guy Ephraim