3

J'ai une vue, et sur cette vue j'ai ajouté un UITapGesture. Maintenant, quand je mets un bouton dans la vue et clique sur le bouton, il n'appelle pas l'action du bouton.L'action UIButton n'est pas appelée lorsque UITapGesture dans la vue

Voici mon code:

ButtionView=[[UIView alloc]initWithFrame:CGRectMake(x, y, 84, 84)]; 
    ButtionView.tag=i; 
    UIImageView *coverImageView = [[UIImageView alloc]  

    initWithFrame:CGRectMake(0,0,ButtionView.frame.size.width,84)]; 
    coverImageView.tag = i; 
    UIButton *notesbutton=[UIButton buttonWithType:UIButtonTypeRoundedRect]; 
    notesbutton.frame=CGRectMake(0, 0,20,20); 
    notesbutton.tag=i; 

    [notesbutton addTarget:self action:@selector(buttonClickedForNotes:)  
    forControlEvents:UIControlEventTouchUpInside]; 
    [ButtionView addSubview:coverImageView]; 
    [ButtionView addSubview:notesbutton]; 
    [notesbutton bringSubviewToFront:ButtionView]; 
    [self.scrollview addSubview:ButtionView]; 
    [ButtionView addGestureRecognizer:oneFingerSingleTap]; 

-(IBAction)buttonClickedForNotes:(id) sender 
{ 
    NSLog(@"buttion action call"); 

} 
+5

Regardez cette question, il devrait vous être un problème http://stackoverflow.com/questions/4825199/gesture-recognizer-and-button-actions –

Répondre

0

ont rien changé significant.But voulez append que votre méthode d'action Button sera appelée après la méthode UITapGestureRecognizer est appelée.

ButtionView=[[UIView alloc]initWithFrame:CGRectMake(x, y, 84, 84)]; 
ButtionView.tag=i; 
UIImageView *coverImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0,0,ButtionView.frame.size.width,84)]; 
coverImageView.tag = i; 

UIButton *notesbutton=[UIButton buttonWithType:UIButtonTypeRoundedRect]; 
notesbutton.frame=CGRectMake(0, 0,40,40); 
notesbutton.tag=i; 

[notesbutton addTarget:self action:@selector(buttonClickedForNotes:) forControlEvents:UIControlEventTouchUpInside]; 
[ButtionView addSubview:coverImageView]; 
[ButtionView addSubview:notesbutton]; 
[notesbutton bringSubviewToFront:ButtionView]; 
[self.view addSubview:ButtionView]; 
//[ButtionView addGestureRecognizer:oneFingerSingleTap]; 

UITapGestureRecognizer *singleTapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self 
                         action:@selector(singleTap:)]; 
singleTapGestureRecognizer.numberOfTapsRequired = 1; 
singleTapGestureRecognizer.enabled = YES; 
singleTapGestureRecognizer.cancelsTouchesInView = NO; 
[ButtionView addGestureRecognizer:singleTapGestureRecognizer]; 
[singleTapGestureRecognizer release]; 
} 

- (void)singleTap:(UITapGestureRecognizer *)gesture{ 
    NSLog(@"handle taps"); 
} 

-(IBAction)buttonClickedForNotes:(id)sender{ 
    NSLog(@"buttion action call"); 
} 
1

Par défaut, UIView ne peut pas répondre à un événement utilisateur.

Utilisez ce code après initialisation ButtonView pour permettre son userInteraction:

ButtionView.userInteractionEnabled = YES; 
2

Tout d'abord, abonnez-vous au délégué du UITapGesture. .

[singleTapGestureRecognizer setDelegate:self]; 

Ensuite, vous devrez faire le bouton une Ivar de quelque classe que vous êtes alors , ajoutez cette méthode à votre classe:

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch 
{ 
    // Check if the touch was on the button. If it was, 
    // don't have the gesture recognizer intercept the touch 
    if (CGRectContainsPoint(notesButton.frame, [touch locationInView:self.view])) 
     return NO; 
    return YES;  
} 

Hope this aidé.

Questions connexes