【问题标题】:Capturing and recording touch events on UIView在 UIView 上捕获和记录触摸事件
【发布时间】:2018-04-17 12:39:43
【问题描述】:

我正在尝试继承 UIView 并设计一个透明视图。此视图将位于许多其他视图之上,它的唯一任务是捕获和记录用户触摸(点击和平移)。我尝试了许多不同的方法,并在其他用户提出的不同问题中进行了解释,但没有运气。这是我迄今为止在我的实现文件中所做的:

#import "touchLayer.h"

@implementation touchLayer

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) [self commonInit];
    return self;
}

- (id)initWithCoder:(NSCoder *)aDecoder
{
    self = [super initWithCoder:aDecoder];
    if (self) [self commonInit];
    return self;
}

- (void)commonInit
{
    self.userInteractionEnabled = YES;
    self.alpha = 0.0;
}

- (id)hitTest:(CGPoint)point withEvent:(UIEvent *)event
{
    id hitView = [super hitTest:point withEvent:event];
    if (hitView == self)
    {
        UITouch *touch = [[event allTouches] anyObject];

        if (touch.phase == UITouchPhaseBegan) NSLog(@"touchesBegan");
        else if (touch.phase == UITouchPhaseMoved) NSLog(@"touchesMoved");
        else if (touch.phase == UITouchPhaseEnded) NSLog(@"touchesEnded");

        return nil;
    }
    else
        return hitView;
}

@end

现在这段代码工作得很好,我看到了较低层的触摸,但我无法区分 touchBegan、touchMoved 和 touchEnded。 [[event allTouches] anyObject] 返回 nil。您知道如何在不阻止触摸的情况下捕获 UIView 上的点击和平移吗?非常感谢。

【问题讨论】:

  • 所以现在,你的问题是这个视图阻止了下面视图中的触摸。对吗?
  • 没有实际的触摸被传递到下视图。但是,我需要知道这个 UIView 捕获了什么样的触摸(开始、移动、结束),但是 event.allTouches.anyObject 返回 nil。
  • 据我所知,我确信透明视图不会收到触摸。尝试给它一个颜色,你会看到不同
  • 我也尝试过使用颜色和 alpha 通道。它确实收到了触摸,但就像我说的我无法获得 UITouchPhase。是零。
  • 尝试使用func touchesBegan(touches: NSSet, withEvent event: UIEvent) func touchesMoved(touches: NSSet, withEvent event: UIEvent)touchesEnd这3种方法

标签: ios uiview uigesturerecognizer uitouch uievent


【解决方案1】:

经过调查,实际上我找不到使用hitTest 方法和touchLayer 检测触摸的解决方案。但是你的问题是关于捕捉和记录用户的触摸,所以我有另一个问题。

我的解决办法是

  • 子类UIWindow
  • UIAppDelegate 中的window 替换为使用您的窗口类创建的新窗口。
  • 覆盖UIWindowsendEvent方法,在该方法中捕捉并记录用户触摸。

这是我的UIWindow 子类,用于检测触摸。我试过了,效果很好。

@implementation DetectTouchWindow

- (void)sendEvent:(UIEvent *)event {
  UITouch *touch = [[event allTouches] anyObject];

  switch ([touch phase]) {
    case UITouchPhaseBegan:
      NSLog(@"Touch Began");
      break;
    case UITouchPhaseMoved:
      NSLog(@"Touch Move");
      break;
    case UITouchPhaseEnded:
      NSLog(@"Touch End");
      break;
    case UITouchPhaseCancelled:
      NSLog(@"Touch Cancelled");
      break;
    default:
      break;
  }

  [super sendEvent:event];
}

@end

为了更详细和更简单,我创建了一个演示 repo 来检查它。你可以看看这个链接https://github.com/trungducc/stackoverflow/tree/recording-touch-events

希望这会有所帮助;)

【讨论】:

  • 这太好了,非常感谢您分享这个。像魅力一样工作。
  • 欢迎@BabakSamareh ;)
  • 另外,我还必须在不同的视图控制器中记录触摸。您建议的这种方法从应用程序打开到关闭的那一刻开始捕获触摸。如何区分为不同视图控制器捕获的触摸?
  • 您可以使用UIApplication.sharedApplication.delegate.window.rootViewController 来检查当前控制器是什么;)
  • @BabakSamareh 或DetectTouchWindow,只需使用self.rootViewCont‌​roller
猜你喜欢
  • 2011-08-12
  • 2020-05-27
  • 2014-08-06
  • 1970-01-01
  • 1970-01-01
  • 2011-05-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多