How to make UIButton shake on tap?

LampPost

I'm learning about UIView animations using keyframes and spring animations and I'm trying to make a button shake after tapping it. The issue is that I drag and dropped the button from the library and pinned the trailing edge to a UILabel above it and nothing else. In the various examples I see a header constraint but my button has no header. This is the code I have so far

@IBAction func noButtonPressed(_ sender: UIButton) {
    UIView.animate(withDuration: 1, delay: 1, usingSpringWithDamping: 0.5, initialSpringVelocity: 15, options: [], animations: {
        self.noButtonTrailing.constant = 16
        self.view.layoutIfNeeded()
    })
}

Am I suppose to make a header constraint somewhere? Thanks

Krunal

Here is simple media timing animation for linear movement & UIView damping animation.

enter image description here

Note: Swift 4

extension UIView {


    // Using CAMediaTimingFunction
    func shake(duration: TimeInterval = 0.5, values: [CGFloat]) {
        let animation = CAKeyframeAnimation(keyPath: "transform.translation.x")

        // Swift 4.2 and above
        animation.timingFunction = CAMediaTimingFunction(name: CAMediaTimingFunctionName.linear)

        // Swift 4.1 and below
        animation.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionLinear)


        animation.duration = duration // You can set fix duration
        animation.values = values  // You can set fix values here also
        self.layer.add(animation, forKey: "shake")
    }


    // Using SpringWithDamping
    func shake(duration: TimeInterval = 0.5, xValue: CGFloat = 12, yValue: CGFloat = 0) {
        self.transform = CGAffineTransform(translationX: xValue, y: yValue)
        UIView.animate(withDuration: duration, delay: 0, usingSpringWithDamping: 0.4, initialSpringVelocity: 1.0, options: .curveEaseInOut, animations: {
            self.transform = CGAffineTransform.identity
        }, completion: nil)

    }


    // Using CABasicAnimation
    func shake(duration: TimeInterval = 0.05, shakeCount: Float = 6, xValue: CGFloat = 12, yValue: CGFloat = 0){
        let animation = CABasicAnimation(keyPath: "position")
        animation.duration = duration
        animation.repeatCount = shakeCount
        animation.autoreverses = true
        animation.fromValue = NSValue(cgPoint: CGPoint(x: self.center.x - xValue, y: self.center.y - yValue))
        animation.toValue = NSValue(cgPoint: CGPoint(x: self.center.x + xValue, y: self.center.y - yValue))
        self.layer.add(animation, forKey: "shake")
    }

}

Button Action

@IBAction func noButtonPressed(button: UIButton) {

  // for spring damping animation
  //button.shake()  


  // for CAMediaTimingFunction
  button.shake(duration: 0.5, values: [-12.0, 12.0, -12.0, 12.0, -6.0, 6.0, -3.0, 3.0, 0.0])

 // for CABasicAnimation
 //button.shake(shakeCount: 10)  

}

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

How to make tableViewCell handle both tap and longPress?

分類Dev

How to make a netctl profile for a TAP device?

分類Dev

Is there a way to detect closest UIButton to tap location?

分類Dev

How to achieve shake animation programmatically?

分類Dev

UIButton to make UIScrollView bounce

分類Dev

how to make a background scroll according to user tap to right/left in live wallpaper app?

分類Dev

What's the simplest way to receive tap events on a disabled UIButton?

分類Dev

Get Signal from tap on UIButton in Reactive Cocoa 4

分類Dev

How to locate a tap on a SCNPlane?

分類Dev

How to shake a widget in Flutter on invalid input?

分類Dev

How to define a Shake rule that depends on some modification of an input

分類Dev

Lubuntu 18.10 How to activate tap to click

分類Dev

How to catch the tap down event on React Native?

分類Dev

Swift - How to add tap gesture to array of UIViews?

分類Dev

How to hide AppBar dynamically on Double Tap?

分類Dev

How to select a SKRoute on a single tap in iphone sdk

分類Dev

How to present UIImagePickerController from UIImageView with Tap Gesture

分類Dev

How to adjust an UIButton's imageSize?

分類Dev

How to create event for UIButton in ViewController?

分類Dev

How to check UIButton has target?

分類Dev

How to trigger UIButton action programmatically

分類Dev

How to addTarget to UIButton without sender

分類Dev

UICollectionViewCell Shake

分類Dev

WPF ScrollViewer: tap=click, tap-and-hold/drag=scroll. How to achieve this?

分類Dev

How to set the title of UIButton as left alignment?

分類Dev

How to enable tap in leftView/rightView when UITextField disabled?

分類Dev

How to create youtube's double-tap gesture on Android?

分類Dev

How to get the single tap point in iOS voice over mode

分類Dev

How to run tap operator when any given observables emits value?

Related 関連記事

  1. 1

    How to make tableViewCell handle both tap and longPress?

  2. 2

    How to make a netctl profile for a TAP device?

  3. 3

    Is there a way to detect closest UIButton to tap location?

  4. 4

    How to achieve shake animation programmatically?

  5. 5

    UIButton to make UIScrollView bounce

  6. 6

    how to make a background scroll according to user tap to right/left in live wallpaper app?

  7. 7

    What's the simplest way to receive tap events on a disabled UIButton?

  8. 8

    Get Signal from tap on UIButton in Reactive Cocoa 4

  9. 9

    How to locate a tap on a SCNPlane?

  10. 10

    How to shake a widget in Flutter on invalid input?

  11. 11

    How to define a Shake rule that depends on some modification of an input

  12. 12

    Lubuntu 18.10 How to activate tap to click

  13. 13

    How to catch the tap down event on React Native?

  14. 14

    Swift - How to add tap gesture to array of UIViews?

  15. 15

    How to hide AppBar dynamically on Double Tap?

  16. 16

    How to select a SKRoute on a single tap in iphone sdk

  17. 17

    How to present UIImagePickerController from UIImageView with Tap Gesture

  18. 18

    How to adjust an UIButton's imageSize?

  19. 19

    How to create event for UIButton in ViewController?

  20. 20

    How to check UIButton has target?

  21. 21

    How to trigger UIButton action programmatically

  22. 22

    How to addTarget to UIButton without sender

  23. 23

    UICollectionViewCell Shake

  24. 24

    WPF ScrollViewer: tap=click, tap-and-hold/drag=scroll. How to achieve this?

  25. 25

    How to set the title of UIButton as left alignment?

  26. 26

    How to enable tap in leftView/rightView when UITextField disabled?

  27. 27

    How to create youtube's double-tap gesture on Android?

  28. 28

    How to get the single tap point in iOS voice over mode

  29. 29

    How to run tap operator when any given observables emits value?

ホットタグ

アーカイブ