在Swift中为UITextField/UIView摇动动画

Joe*_*ens 59 uitextfield ios swift

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

我目前有以下代码工作:

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 {

}
Run Code Online (Sandbox Code Playgroud)

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

我找不到任何动画文本字段的动画.

有任何想法吗?

谢谢!

rak*_*hbs 138

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

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")
Run Code Online (Sandbox Code Playgroud)


小智 131

以下功能在任何视图中使用.

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")
    }
}
Run Code Online (Sandbox Code Playgroud)

  • 如果你想继续摇动,添加animation.repeatCount = 1000! (4认同)

Rom*_*One 38

或者,如果你想要更多参数,你可以使用它(在swift 3和swift 4中):

public extension UIView {

    func shake(count : Float = 4,for duration : TimeInterval = 0.5,withTranslation translation : Float = 5) {

        let animation = CAKeyframeAnimation(keyPath: "transform.translation.x")
        animation.timingFunction = CAMediaTimingFunction(name: CAMediaTimingFunctionName.linear)
        animation.repeatCount = count
        animation.duration = duration/TimeInterval(animation.repeatCount)
        animation.autoreverses = true
        animation.values = [translation, -translation]
        layer.add(animation, forKey: "shake")
    }  
}
Run Code Online (Sandbox Code Playgroud)

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

yourView.shake()
Run Code Online (Sandbox Code Playgroud)

或者这样,如果要为动画添加一些自定义参数:

yourView.shake(count: 5, for: 1.5, withTranslation: 10)
Run Code Online (Sandbox Code Playgroud)

  • 此解决方案不正确.摇动动画只能动画到一边.即[-5至0].正确的解决方案是[-2.5到+2.5].这种不正确的解决方案意味着震动不会从视图的中心发生,只是在视觉上看起来很近. (2认同)
  • 如果您尝试点击动画的uitextfield,这实际上会崩溃。 (2认同)

Cor*_*ett 22

我认为所有这些都很危险.

如果您的摇动动画基于用户操作,并且在动画制作时触发了用户操作.

CRAAAAAASH

这是我在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()
}
Run Code Online (Sandbox Code Playgroud)

也许不是最干净的,但这种方法可以反复触发,很容易理解

编辑:

我是使用UIViewPropertyAnimator的巨大支持者.这么多很酷的功能允许您对基本动画进行动态修改.

这是另一个在视图抖动时添加红色边框的示例,然后在抖动完成时将其删除.

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()
}
Run Code Online (Sandbox Code Playgroud)


Har*_*kar 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")
    }
}
Run Code Online (Sandbox Code Playgroud)

使用

self.vwOffer.shake()
Run Code Online (Sandbox Code Playgroud)


Vah*_*hid 8

雨燕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()
    }
}
Run Code Online (Sandbox Code Playgroud)


小智 6

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)
    }
}
Run Code Online (Sandbox Code Playgroud)

如何使用:

[UIView, UILabel, UITextField, UIButton & etc].layer.shake(NSTimeInterval(0.7))
Run Code Online (Sandbox Code Playgroud)


ric*_*ira 5

我尝试了一些可用的解决方案,但没有一个能够处理完整的摇动动画:从左到右移动并返回到原始位置。

因此,经过一番调查,我找到了正确的解决方案,我认为使用UIViewPropertyAnimator.

func shake(completion: (() -> Void)? = nil) {
    let speed = 0.75
    let time = 1.0 * speed - 0.15
    let timeFactor = CGFloat(time / 4)
    let animationDelays = [timeFactor, timeFactor * 2, timeFactor * 3]

    let shakeAnimator = UIViewPropertyAnimator(duration: time, dampingRatio: 0.3)
    // left, right, left, center
    shakeAnimator.addAnimations({
        self.transform = CGAffineTransform(translationX: 20, y: 0)
    })
    shakeAnimator.addAnimations({
        self.transform = CGAffineTransform(translationX: -20, y: 0)
    }, delayFactor: animationDelays[0])
    shakeAnimator.addAnimations({
        self.transform = CGAffineTransform(translationX: 20, y: 0)
    }, delayFactor: animationDelays[1])
    shakeAnimator.addAnimations({
        self.transform = CGAffineTransform(translationX: 0, y: 0)
    }, delayFactor: animationDelays[2])
    shakeAnimator.startAnimation()

    shakeAnimator.addCompletion { _ in
        completion?()
    }

    shakeAnimator.startAnimation()
}
Run Code Online (Sandbox Code Playgroud)

最后结果:

摇动动画结果