3

У меня есть вид, и в этом представлении я добавил UITapGesture. Теперь, когда я помещаю кнопку в представление и нажимаю кнопку, она не вызывает действие кнопки.Действие UIButton не вызывается, когда UITapGesture в представлении

Вот мой код:

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

Посмотрите на этот вопрос, он должен быть вам проблема http://stackoverflow.com/questions/4825199/gesture-recognizer-and-button-actions –

ответ

0

ничего не изменил significant.But хочет добавляемое, что Ваш метод действия кнопки будет вызываться после того, как метод UITapGestureRecognizer называется.

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

По умолчанию UIView не может отвечать на события пользователя.

Используйте этот код после инициализации ButtonView, чтобы включить его userInteraction:

ButtionView.userInteractionEnabled = YES; 
2

Первого, подписаться на делегат UITapGesture в. .

[singleTapGestureRecognizer setDelegate:self]; 

Тогда вам придется сделать кнопку в Ивар из любого класса, который в Затем добавьте этот метод в класс:

- (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;  
} 

Надеется, что это помогло.