gpt4 book ai didi

ios - 点击时查看抖动动画错误

转载 作者:行者123 更新时间:2023-11-28 10:09:20 24 4
gpt4 key购买 nike

我一直在使用以下 UIView 扩展来摇动 View :

func shake(count: Float = 4, for duration: TimeInterval = 0.5,
withTranslation translation: CGFloat = 5) {
let animation: CABasicAnimation = CABasicAnimation(keyPath: "transform.translation.x")
animation.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionLinear)
animation.repeatCount = count
animation.duration = duration / TimeInterval(animation.repeatCount)
animation.autoreverses = true
animation.fromValue = NSValue(cgPoint: CGPoint(x: -translation, y: self.center.y))
animation.toValue = NSValue(cgPoint: CGPoint(x: translation, y: self.center.y))
layer.add(animation, forKey: "shake")
}

以及用法:

view.shake(count: 3, for: 0.2, withTranslation: 8)

这对于摇动 View 非常有效(在我的例子中,我摇动了一些按钮、UIView 和一些 ImageView )。当我尝试在摇动动画期间单击其中一个 View 时出现问题。

我收到:

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSConcreteValue doubleValue]: unrecognized selector sent to instance 0x600000442640'

在按钮的情况下,如果我禁用按钮然后启动摇动动画,它似乎可以防止错误发生。不幸的是,这似乎不适用于 ImageView 和 UIView。

我还禁用了 ImageView /UIView 上的用户交互,但无济于事。

我觉得这与在为 View 设置动画时的 HitTest 检查期间将错误的坐标传递给较低层有关。

如能深入了解此错误,我们将不胜感激。

最佳答案

你可以试试这段代码。应该有用:

extension UIView {

func shakeByX() {
let animation = CABasicAnimation(keyPath: "position")
animation.duration = 0.07
animation.repeatCount = 3
animation.autoreverses = true
animation.fromValue = NSValue(cgPoint: CGPoint(x: self.center.x - 6, y: self.center.y))
animation.toValue = NSValue(cgPoint: CGPoint(x: self.center.x + 6, y: self.center.y))
self.layer.add(animation, forKey: "position")
}

func shakeByY() {
let animation = CABasicAnimation(keyPath: "position")
animation.duration = 0.07
animation.repeatCount = 3
animation.autoreverses = true
animation.fromValue = NSValue(cgPoint: CGPoint(x: self.center.x, y: self.center.y - 6))
animation.toValue = NSValue(cgPoint: CGPoint(x: self.center.x, y: self.center.y + 6))
self.layer.add(animation, forKey: "position")
}
}

关于ios - 点击时查看抖动动画错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50076965/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com