如何在Swift 2中检测所有触摸

火龙M子

我正在尝试为使用Swift 2开发的应用程序创建超时功能,但是在swift 2中,您可以将此代码放入应用程序委托中,并且可以正常工作,但是它无法检测到任何键盘按下,按钮按下,文本框按下的情况, 等等:

override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
    super.touchesBegan(touches, withEvent: event);
    let allTouches = event!.allTouches();

    if(allTouches?.count > 0) {
        let phase = (allTouches!.first as UITouch!).phase;
        if(phase == UITouchPhase.Began || phase == UITouchPhase.Ended) {
            //Stuff
            timeoutModel.actionPerformed();
        }
    }
}

在Swift 2之前,我能够拥有AppDelegate子类UIApplication并覆盖sendEvent:

-(void)sendEvent:(UIEvent *)event
{
    [super sendEvent:event];

    // Only want to reset the timer on a Began touch or an Ended touch, to reduce the number of timer resets.
    NSSet *allTouches = [event allTouches];
    if ([allTouches count] > 0) {
        // allTouches count only ever seems to be 1, so anyObject works here.
        UITouchPhase phase = ((UITouch *)[allTouches anyObject]).phase;
        if (phase == UITouchPhaseBegan || phase == UITouchPhaseEnded)
            [[InactivityModel instance] actionPerformed];
    }
}

上面的代码可用于每次触摸,但是仅当UIWindow的层次结构上方不存在视图时,快速等效项才有效吗?

有谁知道一种检测应用程序中每一次触摸的方法?

红发女郎

由于我的应用程序中有类似内容,因此我尝试对其进行修复:

  • 覆盖sendEventUIWindow-不工作
  • sendEvent在委托中覆盖-不起作用

因此,唯一的方法是提供自定义UIApplication子类。到目前为止,我的代码(可在iOS 9上运行)是:

@objc(MyApplication) class MyApplication: UIApplication {

  override func sendEvent(event: UIEvent) {
    //
    // Ignore .Motion and .RemoteControl event
    // simply everything else then .Touches
    //
    if event.type != .Touches {
      super.sendEvent(event)
      return
    }

    //
    // .Touches only
    //
    var restartTimer = true

    if let touches = event.allTouches() {
      //
      // At least one touch in progress?
      // Do not restart auto lock timer, just invalidate it
      //
      for touch in touches.enumerate() {
        if touch.element.phase != .Cancelled && touch.element.phase != .Ended {
          restartTimer = false
          break
        }
      }
    }

    if restartTimer {
      // Touches ended || cancelled, restart auto lock timer
      print("Restart auto lock timer")
    } else {
      // Touch in progress - !ended, !cancelled, just invalidate it
      print("Invalidate auto lock timer")
    }

    super.sendEvent(event)
  }

}

为什么有@objc(MyApplication)那是因为Swift用一种不同于Objective-C的方式来命名名称,它只是说-我在Objective-C中的类名称是MyApplication

要使其正常工作,请打开info.plist并添加具有Principal类键和MyApplication值的行(MyApplication是内部内容@objc(...),而不是Swift类名称)。原始密钥为NSPrincipalClass

在此处输入图片说明

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

如何在Android中禁用布局的所有触摸

来自分类Dev

如何在UIScrollView中检测触摸点

来自分类Dev

如何在SpriteKit中检测触摸位置?

来自分类Dev

如何在Android中检测触摸速度

来自分类Dev

如何在Swift中识别连续触摸?

来自分类Dev

如何检测屏幕 JavaScript 中没有触摸?

来自分类Dev

如何在Cocos2D V3上的CCScene中检测触摸?

来自分类Dev

如何在Unity Game Engine(2D)中检测触摸

来自分类Dev

如何在Cocos2D V3上的CCScene中检测触摸?

来自分类Dev

如何在cocos2d中对子画面进行分组以进行触摸检测?

来自分类Dev

如何在Swift中遍历NSMapTable中的所有项目

来自分类Dev

如何在Swift中获取所有静态属性?

来自分类Dev

如何在Swift中列出所有符合协议的类?

来自分类Dev

如何在Swift中从PDF获取所有文本?

来自分类Dev

如何在Swift中获取所有静态属性?

来自分类Dev

如何在 Swift String 中写入所有 ASCII 字符

来自分类Dev

如何在iOS的子ViewController中检测触摸事件

来自分类Dev

如何在TableView中检测双击/触摸事件

来自分类Dev

如何在Sprite Kit tvOS中检测触摸

来自分类Dev

CAShapeLayer在动画Swift中检测触摸

来自分类Dev

如何在 Swift 中识别 SVG 对象内部的触摸?

来自分类Dev

如何在没有scene2d的情况下检测是否已触摸纹理libgdx

来自分类Dev

如何在没有scene2d的情况下检测是否已触摸纹理libgdx

来自分类Dev

如何在Swift 2中模糊场景

来自分类Dev

如何在Swift 2中解析JSON?

来自分类Dev

如何在UITableView中检测单元格选择-Swift

来自分类Dev

如何在Swift中检测缩放对地图的影响

来自分类Dev

如何在Swift中检测URL的重定向?

来自分类Dev

如何在Swift / Xcode中检测NofiticationCenter的关闭?

Related 相关文章

热门标签

归档