2010-11-25 4 views
0

Salut J'ai peuplé uiscrollview avec des images (le code est ci-dessous), mais je ne sais pas comment ajouter un événement à des images à l'intérieur. Je veux appuyer deux fois sur l'image à l'intérieur de scrollview et obtenir un événement. Toute direction comment y parvenir?Comment attraper un événement à l'intérieur de uiscrollview sur l'image

arrayOfImages = [[NSMutableArray alloc] init]; 
    NSString *img; 
    for (img in imgArray) 
    { 
     [arrayOfImages addObject:[UIImage imageNamed:img]]; 
    } 

    NSLog(@"Array initialization complete..."); 

    scrollView = [[UIScrollView alloc] init]; 
    scrollView.scrollEnabled = YES; 
    scrollView.pagingEnabled = YES; 
    scrollView.directionalLockEnabled = YES; 
    scrollView.showsVerticalScrollIndicator = NO; 
    scrollView.showsHorizontalScrollIndicator = NO; 
    scrollView.delegate = self; 
    scrollView.backgroundColor = [UIColor blueColor]; 
    scrollView.autoresizesSubviews = YES; 
    scrollView.frame = CGRectMake(0, 0, 320, 29); 
    [self.view addSubview:scrollView]; 

    NSLog(@"Scroll View initialization setup complete..."); 

    UIImage *imageToAdd; 
    int x = 0; 
    int y = 0; 
    for (imageToAdd in arrayOfImages) 
    { 
     UIImageView *temp = [[UIImageView alloc] initWithImage:imageToAdd];  

     temp.frame = CGRectMake(x, y, 29, 29); 
     x += 29; 
     [scrollView addSubview:temp]; 

    } 

    NSLog(@"Adding images to outlet complete..."); 

Répondre

2

Ajoutez un UITapGestureRecognizer à UIImageView (s). Pour plus d'informations, consultez this guide pour plus d'informations.

+0

mais selon iCodeBlog, l'uiimageview n'obtiendra pas les taps, donc vous devrez peut-être prendre le uiimageview dans un autre uiview. – harshalb

+0

J'ai ajouté maintenant UIImageView * temp = [[UIImageView alloc] initWithImage: imageToAdd]; \t \t \t \t \t \t temp.frame = CGRectMake (x, y, 29, 29); \t \t temp.userInteractionEnabled = YES; \t \t \t \t x + = 29; \t \t \t \t \t \t UITapGestureRecognizer * robinet = [[UITapGestureRecognizer alloc] initWithTarget: self Action: @selector (imageTapped :)]; \t \t [temp addGestureRecognizer: tap]; \t \t [libération de robinet]; Mais je reçois: erreur SIGABRT ... Mettre fin application en raison d'une exception non interceptée 'NSInvalidArgumentException', raison: '- [DemoImageSlider2ViewController imageTapped:]: sélecteur non reconnu envoyé à l'instance 0x6c28a00' – 1110

+0

J'ai changé la déclaration de méthode de gestionnaire à: - (void) imageTapped: (UIGestureRecognizer *) expéditeur { \t NSLog (@ "ITEM >>>% @", [arrayOfImages objectAtIndex: 8]); } Et cela fonctionne, merci :) – 1110

Questions connexes