UITapGestureRecognizer-터치 업이 아닌 터치 다운에서 작동하도록 하시겠습니까?
탭 이벤트를 사용하는 것은 매우 시간에 민감하므로 사용자가 터치 업을 요구하는 대신 단순히 터치 다운 할 때 UITapGestureRecognizer를 활성화 할 수 있는지 궁금합니다.
사용자 지정 TouchDownGestureRecognizer 하위 클래스를 만들고 touchesBegan에서 제스처를 구현합니다.
TouchDownGestureRecognizer.h
#import <UIKit/UIKit.h>
@interface TouchDownGestureRecognizer : UIGestureRecognizer
@end
TouchDownGestureRecognizer.m
#import "TouchDownGestureRecognizer.h"
#import <UIKit/UIGestureRecognizerSubclass.h>
@implementation TouchDownGestureRecognizer
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
if (self.state == UIGestureRecognizerStatePossible) {
self.state = UIGestureRecognizerStateRecognized;
}
}
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
self.state = UIGestureRecognizerStateFailed;
}
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
self.state = UIGestureRecognizerStateFailed;
}
@end
이행:
#import "TouchDownGestureRecognizer.h"
TouchDownGestureRecognizer *touchDown = [[TouchDownGestureRecognizer alloc] initWithTarget:self action:@selector(handleTouchDown:)];
[yourView addGestureRecognizer:touchDown];
-(void)handleTouchDown:(TouchDownGestureRecognizer *)touchDown{
NSLog(@"Down");
}
신속한 구현 :
import UIKit
import UIKit.UIGestureRecognizerSubclass
class TouchDownGestureRecognizer: UIGestureRecognizer
{
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent)
{
if self.state == .Possible
{
self.state = .Recognized
}
}
override func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent)
{
self.state = .Failed
}
override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent)
{
self.state = .Failed
}
}
다음은 2017 년에 붙여 넣을 Swift 구문입니다.
import UIKit.UIGestureRecognizerSubclass
class SingleTouchDownGestureRecognizer: UIGestureRecognizer {
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent) {
if self.state == .possible {
self.state = .recognized
}
}
override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent) {
self.state = .failed
}
override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent) {
self.state = .failed
}
}
이는 UITap
. 그래서 코드에서 ...
func add(tap v:UIView, _ action:Selector) {
let t = UITapGestureRecognizer(target: self, action: action)
v.addGestureRecognizer(t)
}
안전하게 교체 할 수 있습니다 ....
func add(hairtriggerTap v:UIView, _ action:Selector) {
let t = SingleTouchDownGestureRecognizer(target: self, action: action)
v.addGestureRecognizer(t)
}
테스트 결과 두 번 이상 호출되지 않습니다. 드롭 인 교체로 작동합니다. 두 통화 사이를 전환 할 수 있습니다.
Use a UILongPressGestureRecognizer and set its minimumPressDuration
to 0. It will act like a touch down during the UIGestureRecognizerStateBegan
state.
For Swift 4
func setupTap() {
let touchDown = UILongPressGestureRecognizer(target:self, action: #selector(didTouchDown))
touchDown.minimumPressDuration = 0
view.addGestureRecognizer(touchDown)
}
@objc func didTouchDown(gesture: UILongPressGestureRecognizer) {
if gesture.state == .began {
doSomething()
}
}
For Objective-C
Example:
-(void)setupLongPress
{
self.longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(didLongPress:)];
self.longPress.minimumPressDuration = 0;
[self.view addGestureRecognizer:self.longPress];
}
-(void)didLongPress:(UILongPressGestureRecognizer *)gesture
{
if (gesture.state == UIGestureRecognizerStateBegan){
[self doSomething];
}
}
Swift (without subclassing)
Here is a Swift version similar to Rob Caraway's Objective-C answer.
The idea is to use a long press gesture recognizer with the minimumPressDuration
set to zero rather than using a tap gesture recognizer. This is because the long press gesture recognizer reports touch began events while the tap gesture does not.
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var myView: UIView!
override func viewDidLoad() {
super.viewDidLoad()
// Add "long" press gesture recognizer
let tap = UILongPressGestureRecognizer(target: self, action: #selector(tapHandler))
tap.minimumPressDuration = 0
myView.addGestureRecognizer(tap)
}
// called by gesture recognizer
@objc func tapHandler(gesture: UITapGestureRecognizer) {
// handle touch down and touch up events separately
if gesture.state == .began {
// do something...
print("tap down")
} else if gesture.state == .ended { // optional for touch up event catching
// do something else...
print("tap up")
}
}
}
This is another solution. Create subclass of UIControl. You can use it like UIView even in Storyboard because UIControl is subclass of UIView.
class TouchHandlingView: UIControl {
}
And addTarget to it:
@IBOutlet weak var mainView: TouchHandlingView!
...
mainView.addTarget(self, action: "startAction:", forControlEvents: .TouchDown)
...
Then the designated action will be called like UIButton:
func startAction(sender: AnyObject) {
print("start")
}
'Programing' 카테고리의 다른 글
iPhone-UIScrollView가 상단 또는 하단에 도달했는지 확인 (0) | 2020.10.11 |
---|---|
ADB-Android-현재 활동의 이름 가져 오기 (0) | 2020.10.11 |
코드 변경없이 "컴파일러 필수 멤버 누락"오류가 여러 번 발생 함 (0) | 2020.10.11 |
정수 나누기를 반올림하고 Java에서 int 결과를 얻는 방법은 무엇입니까? (0) | 2020.10.11 |
Android에서 버튼과 같은 이미지보기 클릭 효과를 제공하려면 어떻게해야합니까? (0) | 2020.10.11 |