Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/97.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios UIAlertAction中空文本字段的抖动动画_Ios_Swift_Uialertcontroller - Fatal编程技术网

Ios 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

当警报中的文本字段为空时,我试图使UIAlert抖动,我使用此文本字段将项目添加到我的数组中,我使用的代码不会将任何空值添加到我的列表中,也不会停留在UIAlertAction上,因此显然抖动动画也不会显示,这是我的代码:

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