本文介绍了在Swift中为UITextField/UIView摇动动画的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图弄清楚当用户将文本字段留空时如何使文本字段在按下按钮时摇动.

I am trying to figure out how to make the text Field shake on button press when the user leaves the text field blank.

我目前正在使用以下代码:

I currently have the following code working:

if self.subTotalAmountData.text == "" {

    let alertController = UIAlertController(title: "Title", message:
        "What is the Sub-Total!", preferredStyle: UIAlertControllerStyle.Alert)
    alertController.addAction(UIAlertAction(title: "Okay", style: UIAlertActionStyle.Default,handler: nil))

    self.presentViewController(alertController, animated: true, completion: nil)

} else {

}

但是我认为,仅将文本字段摇动作为警报会更具吸引力.

But i think it would be much more appealing to just have the text field shake as an alert.

我找不到任何可以使文本字段动起来的东西.

I can't find anything to animate the text field.

有什么想法吗?

谢谢!

推荐答案

您可以更改durationrepeatCount并进行调整.这就是我在代码中使用的.改变fromValuetoValue会改变摇动中移动的距离.

You can change the duration and repeatCount and tweak it. This is what I use in my code. Varying the fromValue and toValue will vary the distance moved in the shake.

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

viewToShake.layer.add(animation, forKey: "position")

这篇关于在Swift中为UITextField/UIView摇动动画的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-04 14:55