다음과 같은 뷰 계층 구조가 있습니다.
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를 반환하는 조건을 추가해야합니다.
이것은 apple 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
작동 하려면 준수해야합니다 .
샤니
답변
나는 또한 같은 문제가 있었고, 나는 시도했다.
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch{
if ([touch.view isKindOfClass:[UIButton class]]) { //change it to your condition
return NO;
}
return YES;
}
이제 완벽하게 작동합니다 ………
답변
일반적으로 모든 종류의 UIControl에서 터치를 피하기 위해 아래 delegate 메서드를 사용합니다.
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {
if (([touch.view isKindOfClass:[UIControl class]])) {
return NO;
}
return YES;
}
참고 : gestureRecognizerShouldBegin을 확인하지 마십시오 (recogniser.view 클래스 유형 확인). 작동하지 않습니다.
답변
다음은 Swift 3.0 버전입니다.
extension UIViewController: UIGestureRecognizerDelegate {
public func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldReceive touch: UITouch) -> Bool {
if touch.view is UIButton {
return false
}
return true
}
잊지 마세요 :
Make your tapper object delegate to self (e.g: tapper.delegate = self)
답변
다음은 Swift 버전입니다.
func gestureRecognizer(gestureRecognizer: UIGestureRecognizer, shouldReceiveTouch touch: UITouch) -> Bool {
if (touch.view!.isKindOfClass(UIButton)) {
return false
}
return true
}
잊지 마세요 :
- 수업을 준수하도록
UIGestureRecognizerDelegate
- (예 :자가에 캐처 객체의 대리자를 확인
tapper.delegate = self
)
답변
가장 좋은 해결책은 아래 코드 스 니펫을 사용하는 것입니다.
-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {
CGPoint touchLocation = [touch locationInView:self.view];
return !CGRectContainsPoint(self.menuButton.frame, touchLocation);
}