Ios Swift中UITextField/UIView的抖动动画
我试图弄清楚当用户将文本字段留空时,如何在按下按钮时使文本字段抖动 我目前有以下代码正在工作:Ios Swift中UITextField/UIView的抖动动画,ios,swift,uitextfield,Ios,Swift,Uitextfield,我试图弄清楚当用户将文本字段留空时,如何在按下按钮时使文本字段抖动 我目前有以下代码正在工作: if self.subTotalAmountData.text == "" { let alertController = UIAlertController(title: "Title", message: "What is the Sub-Total!", preferredStyle: UIAlertControllerStyle.Alert) alertCon
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 {
}
但我认为,将文本字段作为一个警告进行抖动会更有吸引力
我找不到任何东西来设置文本字段的动画
有什么想法吗
谢谢 您可以更改
持续时间
和重复计数
并对其进行调整。这就是我在代码中使用的内容。改变fromValue
和toValue
将改变震动中移动的距离
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")
您可以更改
持续时间
和重复计数
并对其进行调整。这就是我在代码中使用的内容。改变fromValue
和toValue
将改变震动中移动的距离
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")
以下函数用于任何视图
extension UIView {
func shake() {
let animation = CAKeyframeAnimation(keyPath: "transform.translation.x")
animation.timingFunction = CAMediaTimingFunction(name: CAMediaTimingFunctionName.linear)
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")
}
}
以下函数用于任何视图
extension UIView {
func shake() {
let animation = CAKeyframeAnimation(keyPath: "transform.translation.x")
animation.timingFunction = CAMediaTimingFunction(name: CAMediaTimingFunctionName.linear)
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")
}
}
如何使用:
[UIView, UILabel, UITextField, UIButton & etc].layer.shake(NSTimeInterval(0.7))
如何使用:
[UIView, UILabel, UITextField, UIButton & etc].layer.shake(NSTimeInterval(0.7))
这是基于Cabasicanization,它还包含音频效果:
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..")
}
}
}
这是基于Cabasicanization,它还包含音频效果:
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..")
}
}
}
编辑:如果连续两次触发动画,则使用
cabasicanition
会导致应用程序崩溃。因此,请务必使用CAKeyframeAnimation
。错误已修复,感谢评论:)
或者,如果需要更多参数(swift 5中的),也可以使用此选项: 您可以通过这种方式在任何UIView、UIButton、UILabel、UIExtView等上调用此函数
yourView.shake()
或者,如果要向动画中添加一些自定义参数,请使用这种方式:
yourView.shake(count: 5, for: 1.5, withTranslation: 10)
编辑:如果连续两次触发动画,则使用
cabasicanition
会导致应用程序崩溃。因此,请务必使用CAKeyframeAnimation
。错误已修复,感谢评论:)
或者,如果需要更多参数(swift 5中的),也可以使用此选项: 您可以通过这种方式在任何UIView、UIButton、UILabel、UIExtView等上调用此函数
yourView.shake()
或者,如果要向动画中添加一些自定义参数,请使用这种方式:
yourView.shake(count: 5, for: 1.5, withTranslation: 10)
Swift 5.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")
}
}
使用
self.vwOffer.shake()
Swift 5.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")
}
}
使用
self.vwOffer.shake()
我认为所有这些都是危险的 如果抖动动画基于用户动作,并且该用户动作在设置动画时被触发 craaaaash 这是我在Swift 4中的方法:
static func shake(view: UIView, for duration: TimeInterval = 0.5, withTranslation translation: CGFloat = 10) {
let propertyAnimator = UIViewPropertyAnimator(duration: duration, dampingRatio: 0.3) {
view.transform = CGAffineTransform(translationX: translation, y: 0)
}
propertyAnimator.addAnimations({
view.transform = CGAffineTransform(translationX: 0, y: 0)
}, delayFactor: 0.2)
propertyAnimator.startAnimation()
}
也许不是最干净的,但这种方法可以反复触发,并且容易理解
编辑:
我是使用UIViewPropertyImator的坚定支持者。有很多很酷的功能,可以让你对基本动画进行动态修改
下面是另一个示例,用于在视图抖动时添加红色边框,然后在抖动结束时将其删除
static func shake(view: UIView, for duration: TimeInterval = 0.5, withTranslation translation: CGFloat = 10) {
let propertyAnimator = UIViewPropertyAnimator(duration: duration, dampingRatio: 0.3) {
view.layer.borderColor = UIColor.red.cgColor
view.layer.borderWidth = 1
view.transform = CGAffineTransform(translationX: translation, y: 0)
}
propertyAnimator.addAnimations({
view.transform = CGAffineTransform(translationX: 0, y: 0)
}, delayFactor: 0.2)
propertyAnimator.addCompletion { (_) in
view.layer.borderWidth = 0
}
propertyAnimator.startAnimation()
}
我认为所有这些都是危险的 如果抖动动画基于用户动作,并且该用户动作在设置动画时被触发 craaaaash 这是我在Swift 4中的方法:
static func shake(view: UIView, for duration: TimeInterval = 0.5, withTranslation translation: CGFloat = 10) {
let propertyAnimator = UIViewPropertyAnimator(duration: duration, dampingRatio: 0.3) {
view.transform = CGAffineTransform(translationX: translation, y: 0)
}
propertyAnimator.addAnimations({
view.transform = CGAffineTransform(translationX: 0, y: 0)
}, delayFactor: 0.2)
propertyAnimator.startAnimation()
}
也许不是最干净的,但这种方法可以反复触发,并且容易理解
编辑:
我是使用UIViewPropertyImator的坚定支持者。有很多很酷的功能,可以让你对基本动画进行动态修改
下面是另一个示例,用于在视图抖动时添加红色边框,然后在抖动结束时将其删除
static func shake(view: UIView, for duration: TimeInterval = 0.5, withTranslation translation: CGFloat = 10) {
let propertyAnimator = UIViewPropertyAnimator(duration: duration, dampingRatio: 0.3) {
view.layer.borderColor = UIColor.red.cgColor
view.layer.borderWidth = 1
view.transform = CGAffineTransform(translationX: translation, y: 0)
}
propertyAnimator.addAnimations({
view.transform = CGAffineTransform(translationX: 0, y: 0)
}, delayFactor: 0.2)
propertyAnimator.addCompletion { (_) in
view.layer.borderWidth = 0
}
propertyAnimator.startAnimation()
}
//在基类或任何视图控制器中编写并使用它
//在基类或任何视图控制器中编写并使用它 适用于Corey Pett的安全(非碰撞)震动分机回答:
extension UIView {
func shake(for duration: TimeInterval = 0.5, withTranslation translation: CGFloat = 10) {
let propertyAnimator = UIViewPropertyAnimator(duration: duration, dampingRatio: 0.3) {
self.transform = CGAffineTransform(translationX: translation, y: 0)
}
propertyAnimator.addAnimations({
self.transform = CGAffineTransform(translationX: 0, y: 0)
}, delayFactor: 0.2)
propertyAnimator.startAnimation()
}
}
Swift 5 适用于Corey Pett的安全(非碰撞)震动分机回答:
extension UIView {
func shake(for duration: TimeInterval = 0.5, withTranslation translation: CGFloat = 10) {
let propertyAnimator = UIViewPropertyAnimator(duration: duration, dampingRatio: 0.3) {
self.transform = CGAffineTransform(translationX: translation, y: 0)
}
propertyAnimator.addAnimations({
self.transform = CGAffineTransform(translationX: 0, y: 0)
}, delayFactor: 0.2)
propertyAnimator.startAnimation()
}
}
我尝试了一些可用的解决方案,但没有一个能够处理全抖动动画:从左向右移动并返回原始位置 因此,经过一些调查,我找到了一个正确的解决方案,我认为这是一个成功的抖动。 最终结果:
我尝试了一些可用的解决方案,但没有一个解决方案能够处理全抖动动画:从左向右移动并返回原始位置 因此,经过一些调查,我找到了一个正确的解决方案,我认为这是一个成功的抖动。 最终结果:
你做了什么来尝试设置文本字段的动画?我不知道从哪里开始。没有什么对我有意义,我也找不到任何教程显示哪怕是我可以采用的最微小的变化。我已经尝试过使用animateWithDuration UIViewAnimationOptions.Transition制作动画,但代码总是错误,我无法理解它的含义。谷歌iPhone动画教程。Ray Wanderlich的通常都很好,可读性也很好。谢谢,我会尽我所能去阅读和理解的!你做了什么来尝试设置文本字段的动画?我不知道从哪里开始。没有什么对我有意义,我也找不到任何教程显示哪怕是我可以采用的最微小的变化。我已经尝试过使用animateWithDuration UIViewAnimationOptions.Transition制作动画,但代码总是错误,我无法理解它的含义。谷歌iPhone动画教程。Ray Wanderlich的通常都很好,可读性也很好。谢谢,我会尽我所能去阅读和理解的!不知道为什么,但我收到了此异常:
2015-05-17 19:43:00.744 PDFDDistributor[19908:2303862]-[PDFDDistributor.ViewController mLoginBT:]:未识别的选择器发送到实例0x7fca90d17e00 2015-05-17 19:43:00.749 PDFDDistributor[19908:2303862]***由于未捕获的异常“NSInvalidArgumentException”,终止应用程序,原因:'-[pdfdistributor.ViewController mLoginBT::]:发送到实例0x7fca90d17e00'***的无法识别的选择器第一次抛出调用堆栈:
Swift 3版本:让动画=CABASICIATION(关键路径: