gpt4 book ai didi

iphone - 手势识别器和按钮操作

转载 作者:行者123 更新时间:2023-12-03 18:08:27 25 4
gpt4 key购买 nike

我有一个看起来像这样的 View 层次结构:

UIView (A)
UIView > UIImageView
UIView > UIView (B)
UIView > UIView (B) > Rounded Rect Button
UIView > UIView (B) > UIImageView
UIView > UIView (B) > UILabel

我已将手势识别器附加到我的 UIView (B)。我面临的问题是,我没有对 UIView (B) 内的圆角矩形按钮执行任何操作。 singleTap 手势识别器捕获/覆盖按钮的 Touch Up Inside 事件。

我怎样才能让它发挥作用?我认为响应者链层次结构将确保按钮触摸事件被优先考虑,并且它将被触发!我错过了什么?

这是一些相关代码:

#pragma mark -
#pragma mark View lifecycle (Gesture recognizer setup)

- (void)viewDidLoad {
[super viewDidLoad];

// double tap gesture recognizer
UITapGestureRecognizer *dtapGestureRecognize = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(doubleTapGestureRecognizer:)];
dtapGestureRecognize.delegate = self;
dtapGestureRecognize.numberOfTapsRequired = 2;
[self.viewB addGestureRecognizer:dtapGestureRecognize];

// single tap gesture recognizer
UITapGestureRecognizer *tapGestureRecognize = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(singleTapGestureRecognizer:)];
tapGestureRecognize.delegate = self;
tapGestureRecognize.numberOfTapsRequired = 1;
[tapGestureRecognize requireGestureRecognizerToFail:dtapGestureRecognize];
[self.viewB addGestureRecognizer:tapGestureRecognize];

// add gesture recodgnizer to the grid view to start the edit mode
UILongPressGestureRecognizer *pahGestureRecognizer = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressGestureRecognizerStateChanged:)];
pahGestureRecognizer.delegate = self;
pahGestureRecognizer.minimumPressDuration = 0.5;
[self.viewB addGestureRecognizer:pahGestureRecognizer];

[dtapGestureRecognize release];
[tapGestureRecognize release];
[pahGestureRecognizer release];
}

#pragma mark -
#pragma mark Button actions

- (IBAction)buttonTouchUpInside:(id)sender {
NSLog(@"%s, %@", __FUNCTION__, sender);
}

#pragma mark -
#pragma mark Gesture recognizer actions


- (void)singleTapGestureRecognizer:(UIGestureRecognizer *)gestureRecognizer {
NSLog(@"%s", __FUNCTION__);
}

- (void)doubleTapGestureRecognizer:(UIGestureRecognizer *)gestureRecognizer {
NSLog(@"%s", __FUNCTION__);
}

- (void)longPressGestureRecognizerStateChanged:(UIGestureRecognizer *)gestureRecognizer {

switch (gestureRecognizer.state) {

case UIGestureRecognizerStateEnded: {
NSLog(@"%s", __FUNCTION__);

break;
}
default:
break;
}
}

最佳答案

在“shouldReceiveTouch”方法中,您应该添加一个条件,如果触摸位于按钮中,则返回 NO。

这是来自苹果SimpleGestureRecognizers例子。

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

// Disallow recognition of tap gestures in the segmented control.
if ((touch.view == yourButton)) {//change it to your condition
return NO;
}
return YES;
}

希望对你有帮助

编辑

正如 Daniel 指出的,您必须遵守 UIGestureRecognizerDelegate 才能正常工作。

沙尼

关于iphone - 手势识别器和按钮操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4825199/

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