gpt4 book ai didi

ios - UITapGestureRecognizer 阻止 subview 中 UIButton 的触摸事件

转载 作者:塔克拉玛干 更新时间:2023-11-01 21:20:16 25 4
gpt4 key购买 nike

我相信我在使用 UITapGestureRecognizer 时遇到问题,因为在聊天室区域中点击时会关闭键盘,从而阻止或阻止触摸 previewCancelButton。以下是我的相关代码:

BaseTemplateVC.m

- (void)addDismissKeyboardGesture {

UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(dismissKeyboard:)];
tapGesture.cancelsTouchesInView = NO;
tapGesture.delegate = self;
self.view.tag = 111;
[self.view addGestureRecognizer:tapGesture];
}

- (void) dismissKeyboard:(id)sender {
UITapGestureRecognizer *gesture = sender;
UIView *view = gesture.view;
NSLog(@"%ld", (long)view.tag);
[self.view endEditing:YES];
}

ChatroomVC.m

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
{

//Disallow recognition of tap gestures in the segmented control.
if (([touch.view isKindOfClass:[UIButton class]])) {
NSLog(@"noooooooo");
return NO;
}
return YES;
NSLog(@"yesssssss");
}

InputFunctionView.m

- (void)selectedSticker:(NSString *)stickerURLString {

/* Sticker preview subview */
stickerPreviewView = [[UIImageView alloc] initWithFrame:CGRectMake(0, -120, FrameWidth, 120)];
stickerPreviewView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.5f];
stickerPreviewView.userInteractionEnabled = YES;
[self addSubview:stickerPreviewView];
[self bringSubviewToFront:stickerPreviewView];

/* Initialise previewCancelButton */
self.previewCancelButton = [UIButton buttonWithType:UIButtonTypeCustom];
self.previewCancelButton.frame = CGRectMake(Main_Screen_Width-30, SpaceForItems-120, 20, 20);
[self.previewCancelButton setBackgroundImage:[UIImage imageNamed:@"btn_previewcancel.png"] forState:UIControlStateNormal];
[self.previewCancelButton setBackgroundImage:[UIImage imageNamed:@"btn_previewcancel.png"] forState:UIControlStateHighlighted];

[self.previewCancelButton addTarget:self action:@selector(cancelStickerPreviewButtonPressed:) forControlEvents:UIControlEventTouchUpInside];

[self addSubview: self.previewCancelButton];

}

/* Cancel sticker preview subview */
- (void)cancelStickerPreviewButtonPressed:(id)sender {

NSLog(@"cancel sticker preview");
[self.previewCancelButton removeFromSuperview];
[stickerPreviewView removeFromSuperview];

}

现在 previewCancelButton 正确地位于 stickerPreviewView 的右上角,但无法接收到它的触摸事件。当我触摸按钮时,它在控制台中显示“111”,当我回溯时,我发现 BaseTemplateVC.m 包含 addDismissKeyboardGesture 方法,所以我相信这可能会导致问题。

任何人都可以指导我找到一些解决方案。那将不胜感激。提前致谢。 enter image description here

进展:我修改了 ChatroomVC.m 中的 gestureRecognizer 方法,因此现在它可以忽略按钮上的点击手势,但问题仍然是按钮的 Action 没有被触发。

最佳答案

试试这个,我想它会起作用。使用手势的shouldReceiveTouch委托(delegate)方法,touch.view为按钮类时返回NO。因此,当它找到按钮时,它将丢弃手势并执行按钮操作。

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {

// Block the recognition of tap gestures in the button.
if (([touch.view isKindOfClass:[UIButton class]])) {
return NO;
}

return YES;
}

这是演示实现:我已经在 Storyboard中的 View Controller 的主视图上获取了按钮。

- (void)viewDidLoad {
[super viewDidLoad];

UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapGestureClicked:)];
tapGesture.delegate = self;
[self.view addGestureRecognizer:tapGesture];
}
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {

// Disallow recognition of tap gestures in the segmented control.
if (([touch.view isKindOfClass:[UIButton class]])) {
return NO;
}
return YES;
}
- (IBAction)btnTestClicked:(UIButton *)sender {
NSLog(@"test button click");
}

- (void)tapGestureClicked:(UIGestureRecognizer *)recog
{
NSLog(@"tap gesture clicked");
}

希望对您有所帮助。快乐的编码......

关于ios - UITapGestureRecognizer 阻止 subview 中 UIButton 的触摸事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39564613/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com