Ios UIAlertAction中空文本字段的抖动动画
当警报中的文本字段为空时,我试图使UIAlert抖动,我使用此文本字段将项目添加到我的数组中,我使用的代码不会将任何空值添加到我的列表中,也不会停留在UIAlertAction上,因此显然抖动动画也不会显示,这是我的代码:Ios UIAlertAction中空文本字段的抖动动画,ios,swift,uialertcontroller,Ios,Swift,Uialertcontroller,当警报中的文本字段为空时,我试图使UIAlert抖动,我使用此文本字段将项目添加到我的数组中,我使用的代码不会将任何空值添加到我的列表中,也不会停留在UIAlertAction上,因此显然抖动动画也不会显示,这是我的代码: let action = UIAlertAction(title: "Add Item", style: .default) { (action) in if textField.text?.isEmpty ?? true { let anima
let action = UIAlertAction(title: "Add Item", style: .default) { (action) in
if textField.text?.isEmpty ?? true {
let animation = CABasicAnimation(keyPath: "position")
animation.duration = 0.07
animation.repeatCount = 4
animation.autoreverses = true
animation.fromValue = NSValue(cgPoint: CGPoint(x: textField.center.x - 10, y: textField.center.y))
animation.toValue = NSValue(cgPoint: CGPoint(x: textField.center.x + 10, y: textField.center.y))
textField.layer.add(animation, forKey: "position")
} else {
self.itemArray.append(textField.text!)
self.tableView.reloadData()
}
}
这里有两件事,首先是为了帮助您,我为shake功能添加了一个扩展:
extension UIView {
func shake(duration timeDuration: Double = 0.07, repeat countRepeat: Float = 3){
let animation = CABasicAnimation(keyPath: "position")
animation.duration = timeDuration
animation.repeatCount = countRepeat
animation.autoreverses = true
animation.fromValue = NSValue(cgPoint: CGPoint(x: self.center.x - 10, y: self.center.y))
animation.toValue = NSValue(cgPoint: CGPoint(x: self.center.x + 10, y: self.center.y))
self.layer.add(animation, forKey: "position")
}
}
因此,您可以很容易地使用,例如,“您的视图”。shake()更简单
第二,也是最重要的一点是,你不能使用这个,甚至不能使用你尝试的方式,因为UITextField继承自UIControl,并且需要来自UIView,所以为你调整的是,只需将UITextFiel放在UIView中,并将抖动功能添加到UIView中,它们就会抖动UIView中的所有内容:D
如果喜欢,投票:D:D