iOS键盘扩展中的屏幕左侧的动画延迟

问题描述:

我正在研究iOS的键盘扩展。不过,我在动画/图层中出现了一些奇怪的问题,它们并未立即出现在屏幕的最左侧。当用户按下某个键时,我使用图层/动画显示“工具提示”。对于除A和Q以外的所有按键,都会立即显示工具提示,但对于这两个按键,在出现图层和动画之前似乎会有一些延迟。这只会在接触时发生,如果我滑入Q或A击中区域,工具提示会立即呈现。我的调试显示代码对所有的键执行完全一样,但对于这两个键,它不会立即生效。iOS键盘扩展中的屏幕左侧的动画延迟

任何有关是否有可能导致此行为的屏幕左边缘有什么特别的想法?还是我在做一些愚蠢的事情呢?

这是我的触摸处理代码部触发刀尖渲染:

override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) { 
    if(!shouldIgnoreTouches()) { 
     for touch in touches { 
      let location = (touch).locationInView(self.inputView) 

      // pass coordinates to offset service to find candidate keys 
      let keyArray = keyOffsetService.getKeys(_keyboardLayout!, location: location) 
      let primaryKey = keyArray[0] 

      if primaryKey.alphaNumericKey != nil { 
       let layers = findLayers(touch) 

       if layers.keyLayer != nil { 
        graphicsService.animateKeyDown(layers.keyLayer as! CATextLayer, shieldLayer: layers.shieldLayer) 
        _shieldsUp.append((textLayer:layers.keyLayer, shieldLayer:layers.shieldLayer)) 
       } 
      } 
     } 
    } 
} 

动画代码:

func animateKeyDown(layer:CATextLayer, shieldLayer:CALayer?) { 
    if let sLayer = shieldLayer { 
     keyDownShields(layer, shieldLayer: sLayer) 
     CATransaction.begin() 
     CATransaction.setDisableActions(true) 

     let fontSizeAnim = CABasicAnimation(keyPath: "fontSize") 
     fontSizeAnim.removedOnCompletion = true 
     fontSizeAnim.fromValue = layer.fontSize 
     fontSizeAnim.toValue = layer.fontSize * 0.9 
     layer.fontSize = layer.fontSize * 0.9 

     let animation = CABasicAnimation(keyPath: "opacity") 
     animation.removedOnCompletion = true 
     animation.fromValue = layer.opacity 
     animation.toValue = 0.3 
     layer.opacity = 0.3 

     let animGroup = CAAnimationGroup() 
     animGroup.animations = [fontSizeAnim, animation] 
     animGroup.duration = 0.01 
     layer.addAnimation(animGroup, forKey: "down") 

     CATransaction.commit() 
    } 
} 

取消隐藏工具提示层:

private func keyDownShields(layer:CATextLayer, shieldLayer:CALayer) { 
    shieldLayer.hidden = false 
    shieldLayer.setValue(true, forKey: "isUp") 
    shieldLayer.zPosition = 1 
    shieldLayer.removeAllAnimations() 
    layer.setValue(true, forKey: "isUp") 
} 

Image of the keyboard with tooltip

这是由iOS 9中的一项功能引起的,该功能允许用户在向右滑动时强制按屏幕左边缘切换应用程序。

您可以通过禁用3D touch来关闭此功能,但这不是一个解决方案。

我不知道有任何API可以让您覆盖此行为。

+0

谢谢,这是有道理的。我猜想iOS10的行为发生了变化,因为问题已经消失了。 – Malakim

+0

我的iPhone 6s iOS 11版本仍然存在这个问题任何想法如何禁用我的应用程序内的3D触摸? – Netero

+0

@Netero,据我所知 - 你不能。至少我们从来没有做过任何事情。 – Malakim