在UIAlertAction中为空文本字段摇动动画

时间:2019-01-09 13:50:34

标签: ios swift uialertcontroller

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

class tag_info:
    def __init__(self):
        self.id = ""
        self.rssi = 0

class tag_list:
    def __init__(self):
        self.tag = [tag_info() for _ in range(20)]

class data_packet():
    def __init__(self):
        self.timestamp=0
        self.tags = tag_list()
        self.heading=0
        self.airt=0

1 个答案:

答案 0 :(得分:0)

这里有两件事,首先是为了帮助您在这里,我添加了扩展功能以摇动功能:

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添加摇动功能,它们将摇动:D

如果喜欢投票:D:D