Skip to content

Instantly share code, notes, and snippets.

@mohsenasm
Last active May 20, 2019 11:41
Show Gist options
  • Save mohsenasm/6a35eec0d79e54eff89a4927b35819eb to your computer and use it in GitHub Desktop.
Save mohsenasm/6a35eec0d79e54eff89a4927b35819eb to your computer and use it in GitHub Desktop.
iOS Swift Bounce Animation
extension UIView {
func bounceAnimationX(angle: Double, speed: Double = 1) {
var inTransform = CATransform3DIdentity;
inTransform.m34 = 1.0 / 1000.0;
inTransform = CATransform3DRotate(inTransform, CGFloat(angle * Double.pi / 180), 0, 1, 0)
var outTransform = CATransform3DIdentity;
outTransform.m34 = 1.0 / 500.0;
outTransform = CATransform3DRotate(outTransform, 0, 0, 1, 0)
UIView.transition(with: self, duration: 0.25*speed, options: .curveEaseInOut, animations: {
self.layer.transform = inTransform
}) { (_) in
UIView.transition(with: self, duration: 0.35*speed, options: .curveEaseInOut, animations: {
self.layer.transform = outTransform
}, completion: nil)
}
}
func bounceAnimationY(angle: Double, speed: Double = 1) {
var inTransform = CATransform3DIdentity;
inTransform.m34 = 1.0 / 1000.0;
inTransform = CATransform3DRotate(inTransform, CGFloat(angle * Double.pi / 180), 1, 0, 0)
var outTransform = CATransform3DIdentity;
outTransform.m34 = 1.0 / 500.0;
outTransform = CATransform3DRotate(outTransform, 0, 0, 1, 0)
UIView.transition(with: self, duration: 0.25*speed, options: .curveEaseInOut, animations: {
self.layer.transform = inTransform
}) { (_) in
UIView.transition(with: self, duration: 0.35*speed, options: .curveEaseInOut, animations: {
self.layer.transform = outTransform
}, completion: nil)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment