UILongPressGestureRecognizer如何工作(how UILongPressGestureRecognizer works)

编程入门 行业动态 更新时间:2024-10-18 14:18:47
UILongPressGestureRecognizer如何工作(how UILongPressGestureRecognizer works)

我试图了解手势识别器的工作原理,并尝试让手势识别器告诉我何时长时间触摸BEGINS以及何时即使触摸不动也会结束。

在viewDidLoad ,我添加了一个名为game的子视图。 在游戏中,我已经实现了两种识别触摸的方法。 一个有效,但没有做我想要的。 另一个不起作用。

Method1我刚刚添加了两个方法:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { NSLog(@"Began"); } - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *) event { NSLog(@"Ended"); }

这有效,无需实例化手势识别器。 有人可以告诉我为什么吗?

这个问题是- (void)touchedEnded:只有在触摸移动时才会被调用,而如果触摸在它开始的同一位置结束则不会被调用。 因此,如果我触摸并移动并放开,两个函数都会被调用。 如果我触摸并按住并放开(不移动),只会调用- (void)touchesBegan 。

方法2我实例化了一个手势识别器:

@property (nonatomic, strong) UILongPressGestureRecognizer *lprg;

然后在我的设置中:

self.lprg = [UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(handleLongPress:)];

然后:

- (void)handleLongPress:(UILongPressGestureRecognizer *)recognizer { NSLog(@"Handling"); switch (recognizer.state) { case UIGestureRecognizerStateBegan: // ... } }

但是这个我以编程方式实例化识别器的方法并不起作用。 我从未在控制台中获得任何NSLog输出。

I am trying to understand how the gesture recognizer works and trying to get a gesture recognizer to tell me when a long touch BEGINS and when it ENDS even if the touch does not move.

In viewDidLoad, I add a subview called game. In game, I have implemented two ways to recognize touches. One works but does not do what I want. The other does not work.

Method1 I just added two methods:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { NSLog(@"Began"); } - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *) event { NSLog(@"Ended"); }

This worked, without instantiating a gesture recognizer. Can someone tell me why?

The issue with this is that - (void)touchedEnded: only gets called if the touch moved and not if the touch ended at the same location it started. So if I touched and moved and let go, both functions get called. If I touch and hold and let go (without moving), only the - (void)touchesBegan gets called.

METHOD 2 I instantiated a gesture recognizer:

@property (nonatomic, strong) UILongPressGestureRecognizer *lprg;

then in my setup:

self.lprg = [UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(handleLongPress:)];

then:

- (void)handleLongPress:(UILongPressGestureRecognizer *)recognizer { NSLog(@"Handling"); switch (recognizer.state) { case UIGestureRecognizerStateBegan: // ... } }

But this one, where I programmatically instantiated the recognizer, did not work. I never got any NSLog output in the console.

最满意答案

你必须使用这个代码来初始化手势

UILongPressGestureRecognizer *gesture1 = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(celllongpressed:)]; [gesture1 setDelegate:self]; [gesture1 setMinimumPressDuration:1]; [self addGestureRecognizer:gesture1];

和目标方法使用这个

-(void)celllongpressed:(UIGestureRecognizer *)longPress { }

you have to use this code to intialized gesture

UILongPressGestureRecognizer *gesture1 = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(celllongpressed:)]; [gesture1 setDelegate:self]; [gesture1 setMinimumPressDuration:1]; [self addGestureRecognizer:gesture1];

and to target method use this

-(void)celllongpressed:(UIGestureRecognizer *)longPress { }

更多推荐

本文发布于:2023-08-05 20:59:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1437987.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:工作   UILongPressGestureRecognizer   works

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!