UFO ET IT

UIGestureRecognizer에 기존 터치를 취소하도록 어떻게 알릴 수 있습니까?

ufoet 2020. 12. 2. 22:18
반응형

UIGestureRecognizer에 기존 터치를 취소하도록 어떻게 알릴 수 있습니까?


나는이 UIPanGestureRecognizer나는 물체 (추적하기 위해 사용하고 UIImageView사용자의 손가락 아래 참조). 저는 X 축의 모션에만 관심이 있으며 터치가 Y 축에서 개체의 프레임 위나 아래에서 벗어나면 터치를 종료하고 싶습니다.

터치가 개체의 Y 경계 내에 있는지 확인하는 데 필요한 모든 것이 있지만 터치 이벤트를 취소하는 방법을 모르겠습니다. 인식기의 cancelsTouchesInView속성을 뒤집어도 원하는대로 작동하지 않는 것 같습니다.

감사!


이 작은 트릭은 저에게 효과적입니다.

@implementation UIGestureRecognizer (Cancel)

- (void)cancel {
    self.enabled = NO;
    self.enabled = YES;
}

@end

로부터 UIGestureRecognizer @enabled문서 :

터치를 수신하지 않도록 제스처 인식기를 비활성화합니다. 기본값은 YES입니다. 제스처 인식기가 현재 제스처를 인식하는 동안이 속성을 NO로 변경하면 제스처 인식기가 취소 된 상태로 전환됩니다.


Swift에서 @matej의 답변.

extension UIGestureRecognizer {
  func cancel() {
    isEnabled = false
    isEnabled = true
  }
}

Obj-C :

recognizer.enabled = NO;
recognizer.enabled = YES;

스위프트 3 :

recognizer.isEnabled = false
recognizer.isEnabled = true

이 방법에 대해로부터 사과 문서 :

@property(nonatomic, getter=isEnabled) BOOL enabled

터치를 수신하지 않도록 제스처 인식기를 비활성화합니다. 기본값은 YES입니다. 제스처 인식기가 현재 제스처를 인식하는 동안이 속성을 NO로 변경하면 제스처 인식기가 취소 된 상태로 전환됩니다.


설명서에 따르면 제스처 인식기를 하위 클래스로 만들 수 있습니다.

YourPanGestureRecognizer.m에서 :

#import "YourPanGestureRecognizer.h"

@implementation YourPanGestureRecognizer

- (void) cancelGesture {
    self.state=UIGestureRecognizerStateCancelled;
}

@end

YourPanGestureRecognizer.h에서 :

#import <UIKit/UIKit.h>
#import <UIKit/UIGestureRecognizerSubclass.h>

@interface NPPanGestureRecognizer: UIPanGestureRecognizer

- (void) cancelGesture;

@end

이제 어디서든 전화를 걸 수 있습니다.

YourPanGestureRecognizer *panRecognizer = [[YourPanGestureRecognizer alloc] initWithTarget:self action:@selector(panMoved:)];
[self.view addGestureRecognizer:panRecognizer];
[...]
-(void) panMoved:(YourPanGestureRecognizer*)sender {
    [sender cancelGesture]; // This will be called twice
}

참고 : https://developer.apple.com/documentation/uikit/uigesturerecognizer?language=objc


이를 처리하는 몇 가지 방법이 있습니다.

  1. If you were writing a custom pan gesture recognizer subclass, you could easily do this by calling -ignoreTouch:withEvent: from inside the recognizer when you notice it straying from the area you care about.

  2. Since you're using the standard Pan recognizer, and the touch starts off OK (so you don't want to prevent it with the delegate functions), you really can only make your distinction when you receive the recognizer's target actions. Check the Y value of the translationInView: or locationInView: return values, and clamp it appropriately.

참고URL : https://stackoverflow.com/questions/3937831/how-can-i-tell-a-uigesturerecognizer-to-cancel-an-existing-touch

반응형