【发布时间】:2023-04-01 21:25:02
【问题描述】:
我正在为 iOS 开发键盘扩展。但是,我遇到了一些奇怪的问题,动画/图层没有立即出现在屏幕的最左侧。当用户按键时,我使用图层/动画来显示“工具提示”。对于除 A 和 Q 之外的所有键,工具提示都会立即显示,但是对于这两个键,在图层和动画出现之前似乎有一点延迟。这只会在触地时发生,如果我滑入 Q 或 A 命中区域,工具提示会立即呈现。我的调试显示代码对所有键的执行完全相同,但是对于这两个键,它没有立即生效。
关于屏幕左边缘是否有任何特殊情况可能导致此行为的任何想法?还是我在做一些愚蠢的事情,这可能是造成这种情况的原因?
这是触发工具提示渲染的触摸处理代码的一部分:
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")
}
【问题讨论】:
标签: swift core-animation ios-app-extension