UITapGestureRecognizers
뷰 에 2를 추가하려고합니다 . 하나는 단일 탭이고 다른 하나는 더블 탭 이벤트입니다. 단일 탭 인식기가 예상대로 작동합니다 (자체적으로). 그러나 더블 탭 인식기가 작동하지 않는 것 같습니다.
유무과 같은 특성을 실험을 시도 : cancelsTouchesInView
, delaysTouchesBegan
그리고 delaysTouchesEnded
하지만 여전히 작동하지 않습니다.
두 번 탭하면 싱글 탭 인식기가 항상 활성화되고 더블 탭 이벤트도 슈퍼 뷰로 전송됩니다. 그러나 사용자 정의 더블 탭 인식기는 전혀 알리지 않는 것 같습니다.
문서는 위에서 언급 한 3 가지 속성을 목적으로 사용할 수 있다고 제안합니다. 그러나 어떤 값을 설정해야하며 어떤 인식기 (싱글, 더블 또는 둘 다)인지 확실하지 않습니다. 이것에 익숙한 누군가가 도움이되기를 바랍니다.
다음은 최신 업데이트 코드 블록입니다.
// ****** gesture recognizers ******
- (void)addSingleAndDoubleTapGestureRecognizersToView:(UIView *)view
{
// single tap
UITapGestureRecognizer *singleTapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget: tableViewController action: @selector(handleSingleTapOnView:)];
[singleTapRecognizer setNumberOfTouchesRequired:1];
[view addGestureRecognizer: singleTapRecognizer];
// double tap
UITapGestureRecognizer *doubleTapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget: tableViewController action: @selector (handleDoubleTapOnView:)];
[doubleTapRecognizer setNumberOfTouchesRequired:2];
[singleTapRecognizer requireGestureRecognizerToFail: doubleTapRecognizer];
[view addGestureRecognizer: doubleTapRecognizer];
}
- (void)handleSingleTapOnView:(id)sender
{
}
- (void)handleDoubleTapOnView:(id)sender
{
}
답변
UITapGestureRecognizer *singleTap = [[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(doSingleTap)] autorelease];
singleTap.numberOfTapsRequired = 1;
[self.view addGestureRecognizer:singleTap];
UITapGestureRecognizer *doubleTap = [[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(doDoubleTap)] autorelease];
doubleTap.numberOfTapsRequired = 2;
[self.view addGestureRecognizer:doubleTap];
[singleTap requireGestureRecognizerToFail:doubleTap];
참고 : 당신이 사용하는 경우 numberOfTouchesRequired
가되어야한다 .numberOfTouchesRequired = 1;
스위프트
let singleTapGesture = UITapGestureRecognizer(target: self, action: #selector(didPressPartButton))
singleTapGesture.numberOfTapsRequired = 1
view.addGestureRecognizer(singleTapGesture)
let doubleTapGesture = UITapGestureRecognizer(target: self, action: #selector(didDoubleTap))
doubleTapGesture.numberOfTapsRequired = 2
view.addGestureRecognizer(doubleTapGesture)
singleTapGesture.require(toFail: doubleTapGesture)
답변
스위프트 3 솔루션 :
let singleTap = UITapGestureRecognizer(target: self, action:#selector(self.singleTapAction(_:)))
singleTap.numberOfTapsRequired = 1
view.addGestureRecognizer(singleTap)
let doubleTap = UITapGestureRecognizer(target: self, action:#selector(self.doubleTapAction(_:)))
doubleTap.numberOfTapsRequired = 2
view.addGestureRecognizer(doubleTap)
singleTap.require(toFail: doubleTap)
코드 라인 singleTap.require (toFail : doubleTap)에서 우리는 싱글 탭을 기다리게하고 탭 이벤트가 더블 탭이 아닌지 확인합니다.
답변
requireGestureRecognizerToFail : 메소드를 사용해야합니다. 이 같은:
[singleTapRecognizer requireGestureRecognizerToFail:doubleTapRecognizer];
답변
// —- 먼저 더블 및 싱글 탭 제스처를 할당해야합니다 .——– //
UITapGestureRecognizer* doubleTap = [[UITapGestureRecognizer alloc] initWithTarget : self action : @selector (handleDoubleTap:)];
UITapGestureRecognizer* singleTap = [[UITapGestureRecognizer alloc] initWithTarget : self action : @selector (handleSingleTap:)];
[singleTap requireGestureRecognizerToFail : doubleTap];
[doubleTap setDelaysTouchesBegan : YES];
[singleTap setDelaysTouchesBegan : YES];
// ———————– 탭수 —————- //
[doubleTap setNumberOfTapsRequired : 2];
[singleTap setNumberOfTapsRequired : 1];
// ——-보기 또는 버튼에 더블 탭 및 싱글 탭 제스처 추가 ——– //
[self.view addGestureRecognizer : doubleTap];
[self.view addGestureRecognizer : singleTap];
답변
그것이 정확히 당신이 찾고있는 것인지 확실하지 않지만 제스처 인식기가없는 싱글 / 더블 탭을했습니다. UITableView에서 사용하고 있으므로 didSelectRowAtIndexPath 메서드에서 해당 코드를 사용했습니다.
tapCount++;
switch (tapCount)
{
case 1: //single tap
[self performSelector:@selector(singleTap:) withObject: indexPath afterDelay: 0.2];
break;
case 2: //double tap
[NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(singleTap:) object:indexPath];
[self performSelector:@selector(doubleTap:) withObject: indexPath];
break;
default:
break;
}
if (tapCount>2) tapCount=0;
singleTap 및 doubleTap 메소드는 NSIndexPath를 매개 변수로 사용하여 void입니다.
- (void)singleTap:(NSIndexPath *)indexPath {
//do your stuff for a single tap
}
- (void)doubleTap:(NSIndexPath *)indexPath {
//do your stuff for a double tap
}
그것이 도움이되기를 바랍니다.
답변
스위프트 2 솔루션 :
let singleTapGesture = UITapGestureRecognizer(target: self, action: #selector(handleSingleTap))
singleTapGesture.numberOfTapsRequired = 1 // Optional for single tap
view.addGestureRecognizer(singleTapGesture)
let doubleTapGesture = UITapGestureRecognizer(target: self, action: #selector(handleDoubleTap))
doubleTapGesture.numberOfTapsRequired = 2
view.addGestureRecognizer(doubleTapGesture)
singleTapGesture.requireGestureRecognizerToFail(doubleTapGesture)
답변
singleTap과 doubleTap을 모두 감지하기 위해 UIGestureRecognizerDelegate 메소드를 구현했습니다.
그냥하세요.
UITapGestureRecognizer *doubleTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(handleDoubleTapGesture:)];
[doubleTap setDelegate:self];
doubleTap.numberOfTapsRequired = 2;
[self.headerView addGestureRecognizer:doubleTap];
UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(handleSingleTapGesture:)];
singleTap.numberOfTapsRequired = 1;
[singleTap setDelegate:self];
[doubleTap setDelaysTouchesBegan:YES];
[singleTap setDelaysTouchesBegan:YES];
[singleTap requireGestureRecognizerToFail:doubleTap];
[self.headerView addGestureRecognizer:singleTap];
그런 다음 이러한 대리자 메서드를 구현하십시오.
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch{
return YES;
}
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer{
return YES;
}