3

I have a View, and on that view I have added a UITapGesture. Now, when I put a button in the view and click the button, it doesn't call the button action.

Here is my 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");

}
jonsca
  • 10,218
  • 26
  • 54
  • 62
Chithri Ajay
  • 907
  • 3
  • 16
  • 37
  • 5
    Look at this question, it should be you problem http://stackoverflow.com/questions/4825199/gesture-recognizer-and-button-actions – Roman Truba Jun 30 '12 at 06:42

3 Answers3

2

First, subscribe to the UITapGesture's delegate.

[singleTapGestureRecognizer setDelegate:self];

Then you will have to make the button an ivar of whatever class you are in. Then, add this method to your class:

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

Kyle Rosenbluth
  • 1,672
  • 4
  • 22
  • 38
1

In default, UIView can not response to user event.

Use this code after init ButtonView to enable its userInteraction:

ButtionView.userInteractionEnabled = YES;
OpenThread
  • 2,096
  • 3
  • 28
  • 49
0

Haven't changed anything significant.But wanna append that Your Button action method will be called after the UITapGestureRecognizer method is called.

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");
}
AJS
  • 1,403
  • 12
  • 17