在Swift中为UITextField / UIViewanimation

我想弄清楚当用户离开文本字段为空时如何使文本字段摇button按下。

我目前有以下代码工作:

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 { } 

但是我认为只要把文本字段作为一个警报来震动就会更有吸引力。

我无法find任何animation文本字段。

有任何想法吗?

谢谢!

您可以更改durationrepeatCount并调整它。 这是我在我的代码中使用的。 改变fromValuefromValue会改变晃动中移动的距离。

 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") 

以下function用于任何视图。

 extension UIView { func shake() { let animation = CAKeyframeAnimation(keyPath: "transform.translation.x") animation.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionLinear) animation.duration = 0.6 animation.values = [-20.0, 20.0, -20.0, 20.0, -10.0, 10.0, -5.0, 5.0, 0.0 ] layer.add(animation, forKey: "shake") } } 

或者你可以使用这个,如果你想要更多的参数( 在SWIFT 3 ):

 public extension UIView { func shake(count : Float? = nil,for duration : TimeInterval? = nil,withTranslation translation : Float? = nil) { // You can change these values, so that you won't have to write a long function let defaultRepeatCount = 4 let defaultTotalDuration = 0.5 let defaultTranslation = -5 let animation : CABasicAnimation = CABasicAnimation(keyPath: "transform.translation.x") animation.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionLinear) animation.repeatCount = count ?? defaultRepeatCount animation.duration = (duration ?? defaultTotalDuration)/TimeInterval(animation.repeatCount) animation.autoreverses = true animation.byValue = translation ?? defaultTranslation layer.add(animation, forKey: "shake") } } 

你可以在任何UIView,UIButton,UILabel,UITextView等等上调用这个函数

 yourView.shake() 

或者这样,如果你想添加一些自定义参数的animation:

 yourView.shake(count: 5, for: 1.5, withTranslation: 10) 

Swift 3.0

 extension UIView { func shake(){ let animation = CABasicAnimation(keyPath: "position") animation.duration = 0.07 animation.repeatCount = 3 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") } } 
 extension CALayer { func shake(duration: NSTimeInterval = NSTimeInterval(0.5)) { let animationKey = "shake" removeAnimationForKey(animationKey) let kAnimation = CAKeyframeAnimation(keyPath: "transform.translation.x") kAnimation.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionLinear) kAnimation.duration = duration var needOffset = CGRectGetWidth(frame) * 0.15, values = [CGFloat]() let minOffset = needOffset * 0.1 repeat { values.append(-needOffset) values.append(needOffset) needOffset *= 0.5 } while needOffset > minOffset values.append(0) kAnimation.values = values addAnimation(kAnimation, forKey: animationKey) } } 

如何使用:

 [UIView, UILabel, UITextField, UIButton & etc].layer.shake(NSTimeInterval(0.7)) 

这是基于CABasicAnimation,它还包含一个audio效果:

 extension UIView{ var audioPlayer = AVAudioPlayer() func vibrate(){ let animation = CABasicAnimation(keyPath: "position") animation.duration = 0.05 animation.repeatCount = 5 animation.autoreverses = true animation.fromValue = NSValue(CGPoint: CGPointMake(self.center.x - 5.0, self.center.y)) animation.toValue = NSValue(CGPoint: CGPointMake(self.center.x + 5.0, self.center.y)) self.layer.addAnimation(animation, forKey: "position") // audio part do { audioPlayer = try AVAudioPlayer(contentsOfURL: NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource(mySoundFileName, ofType: "mp3")!)) audioPlayer.prepareToPlay() audioPlayer.play() } catch { print("∙ Error playing vibrate sound..") } } }