123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114 |
- import UIKit
- enum FFAlertStyle {
- case bottom
- case middle
- }
- protocol AlertAnimation: CAAnimationDelegate, UIGestureRecognizerDelegate {
- var animationStyle: FFAlertStyle {set get}
- func showShiftAnimatied(animationView : UIView)
- func showAnimated(animationView : UIView)
- func tapAction(tap: UIGestureRecognizer)
- func showBottomAnimated(animationView : UIView)
- func dismissBottomAnimated(animationView : UIView)
- func dismissAnimated(animationView : UIView)
- }
- var animationStyleKey = "animationStyleKey"
- extension UIViewController: AlertAnimation {
- var animationStyle: FFAlertStyle {
- get {
- return associatedObject(key: &animationStyleKey, initialiser: { () -> FFAlertStyle in
- return FFAlertStyle.middle
- })
- }
-
- set {
- associateObject(key: &animationStyleKey, value: newValue)
- }
- }
-
- func showShiftAnimatied(animationView : UIView) {
- let animation = CATransition()
- animation.duration = 0.2
- animation.subtype = kCATransitionFromTop
- animation.type = kCATransitionMoveIn
- let tap = UITapGestureRecognizer(target: self, action: #selector(tapAction(tap:)))
- tap.delegate = self
- view.addGestureRecognizer(tap)
- animationView.layer.add(animation, forKey: nil)
- }
-
- func showAnimated(animationView : UIView) {
- animationStyle = .middle
- let animation = CAKeyframeAnimation(keyPath: "transform")
- animation.duration = 0.2
- let array = [NSValue(caTransform3D: CATransform3DMakeScale(0.1, 0.1, 1.0)), NSValue(caTransform3D: CATransform3DMakeScale(1.0, 1.0, 1.0))]
- animation.values = array as [AnyObject]
- let tap = UITapGestureRecognizer(target: self, action: #selector(tapAction(tap:)))
- tap.delegate = self
- view.addGestureRecognizer(tap)
- animationView.layer.add(animation, forKey: nil)
- }
-
- @objc func tapAction(tap: UIGestureRecognizer) {
- switch animationStyle {
- case .middle:
- dismissAnimated(animationView: tap.view?.subviews.first ?? UIView())
- default:
- dismissBottomAnimated(animationView: tap.view?.subviews.first ?? UIView())
- }
- }
-
- func showBottomAnimated(animationView : UIView) {
- animationStyle = .bottom
- showShiftAnimatied(animationView: animationView)
- }
-
- func dismissBottomAnimated(animationView : UIView) {
- let position = animationView.layer.position
- let positions = CGPoint(x: kScreenWidth / 2, y: kScreenHeight + view.height)
- let basicPosition = CABasicAnimation(keyPath: "position")
- basicPosition.delegate = self
- basicPosition.fromValue = NSValue(cgPoint: position)
- basicPosition.toValue = NSValue(cgPoint: positions)
- basicPosition.duration = 0.2
- basicPosition.fillMode = kCAFillModeForwards
- basicPosition.isRemovedOnCompletion = false
- animationView.layer.add(basicPosition, forKey: nil)
- }
-
- func dismissAnimated(animationView : UIView) {
- let animation = CAKeyframeAnimation(keyPath: "transform")
- animation.duration = 0.4
- let array = [NSValue(caTransform3D: CATransform3DMakeScale(1.0, 1.0, 1.0)), NSValue(caTransform3D: CATransform3DMakeScale(1.2, 1.2, 1.0)), NSValue(caTransform3D: CATransform3DMakeScale(0.1, 0.1, 1.0))]
- animation.values = array as [AnyObject]
- animation.delegate = self
- animation.isRemovedOnCompletion = false
- animation.fillMode = kCAFillModeForwards
- animationView.layer.add(animation, forKey: nil)
- }
-
- public func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldReceive touch: UITouch) -> Bool {
- if (touch.view?.superview?.isKind(of: UITableViewCell.classForCoder()))! {
- return false
- }
- return true
- }
-
- public func animationDidStop(_ anim: CAAnimation, finished flag: Bool) {
- self.removeControllerAndViewFromParent()
- }
- }
|