如何使步进和长压共存? [英] How to get stepper and longpress to coexist?

查看:80
本文介绍了如何使步进和长压共存?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试通过长按手势和配置为连续更新的步进器来设置视图.使用长按时,不会出现步进器的连续功能.目前,我已经禁用了longpress.我想我不需要它.但是为了将来参考,我如何允许两者共存?

I tried setting up a view with a longpress gesture and a stepper configured for continuous updates. With the longpress, the continuous feature of the stepper does not occur. For now, I've disabled the longpress. I guess I don't need it. But for future reference, how would I allow for both to coexist?

请清楚,这是我尝试此操作时设置屏幕的方式.

Just to be clear, here is the way the screen was set up when I tried this.

  • 使用简单的视图控制器设置了应用程序.
  • 在该视图中添加了一个子视图(可以是一个控制器,但我只是将其设为UIView).
  • 几个标签和步进器已添加到此子视图中.
  • 将踏步机连接成插座和动作.
  • 在IB的主视图中添加了一个longpress识别器.
  • 为完整起见,还向IB中的主视图添加了轻击手势.

主视图上的敲击按预期运行.步进器上的拍子按预期功能运行.在主视图上长按可以正常运行.长按步进器不会.

Taps on the main view function as expected. Taps on the steppers function as expected. Longpress on the main view functions as expected. Longpress on the stepper does not.

我修改了longpress调用的代码,以检查子视图的框架,并且如果触摸位置在该矩形内则不执行操作,但这没什么区别.在这种情况下,我没有尝试过让长按失败者失败,但我想接下来我会尝试.好的,也许不是.似乎没有用于此的API.但是,有此冲突,我不是要尝试.

I modified the code called by the longpress to check for the frame of the subview and not act if the touch location was within that rectangle, but that didn't make a difference. I did not try getting the longpress to fail in that situation, but I suppose I'll try that next. OK, maybe not. There doesn't seem to be an API for that. However, there is this kludge, that I'm not going to try.

附加的是探查器的屏幕快照,带有倒置的调用树,因此您可以查看每个项目正在被调用的内容.

Attached is a screen shot from profiler with an inverted call tree so you can see what each item is being called by.

darkStepped:是步进器调用的IBAction.如果步进器是由手势识别器触发的,我是否不希望在调用树中看到该手势识别器?

darkStepped: is the IBAction that is called by the stepper. If the stepper were triggered by a gesture recognizer, wouldn't I expect to see the gesture recognizer in the call tree?

推荐答案

再次仔细查看Apple的文档后,我找到了解决方案.我将视图控制器作为委托添加到了longpress手势识别器

After carefully reviewing Apple's docs again, I've found the solution. I added the view controller as the delegate to the longpress gesture recognizer

self.longPress.delegate = self;

(当然,还要在接口中添加<UIGestureRecognizerDelegate>,然后将此方法添加到视图控制器中:

(and, of course, adding <UIGestureRecognizerDelegate> to the interface, and then added this method to the view controller:

-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {
   // Determine if the touch is inside the custom subview
   if (gestureRecognizer == self.longPress) {
      CGPoint touchLocation = [touch locationInView:self.view];
      if (CGRectContainsPoint(self.antControl.frame, touchLocation)) {
         return NO;
      }
   }
   return YES;
}

这样,当长按发生在问题中提到的子视图self.antControl的帧内时,手势识别器甚至不会被调用.

This way the gesture recognizer doesn't even get called when the longpress occurs within the frame of self.antControl, which is the subview mentioned in the question.

这篇关于如何使步进和长压共存?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆