Last active
May 20, 2019 11:41
-
-
Save mohsenasm/6a35eec0d79e54eff89a4927b35819eb to your computer and use it in GitHub Desktop.
iOS Swift Bounce Animation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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