如何晃动UIAlertController 中的UITextField


当textfield 单独存在时 以下代码是可以晃动 textfield的


 let animation = CAKeyframeAnimation()
animation.keyPath = "position.x"
animation.values = [0, 10, -10, 10, 0]
animation.keyTimes = [0, 1/6.0, 3/6.0, 5/6.0, 1]
animation.duration = 0.4
animation.additive = true
textField.layer.addAnimation(animation, forKey: "shake")

然而当textfield嵌在一个UIAlertController时就不起作用了,请问如何解决,如何晃动alertcontroller中的输入框或者晃动alertcontroller本身。
图片描述

ios

Kakaka 9 years, 2 months ago

可以试试这个:


 let animation = CABasicAnimation(keyPath: "position")
animation.duration = 0.07
animation.repeatCount = 4
animation.autoreverses = true
animation.fromValue = NSValue(CGPoint: CGPointMake(txtField.center.x - 10, txtField.center.y))
animation.toValue = NSValue(CGPoint: CGPointMake(txtField.center.x + 10, txtField.center.y))
txtField.layer.addAnimation(animation, forKey: "position")

我没试过,不知道行不行。 来自 stackoverflow

悲剧的柔柔姐 answered 9 years, 2 months ago

Your Answer