No Description

FFAlertController.swift 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. //
  2. // FFAlertController.swift
  3. // PaiAi
  4. //
  5. // Created by zhengjianfei on 2017/3/9.
  6. // Copyright © 2017年 FFIB. All rights reserved.
  7. //
  8. import UIKit
  9. enum FFAlertStyle {
  10. case bottom
  11. case middle
  12. }
  13. protocol AlertAnimation: CAAnimationDelegate, UIGestureRecognizerDelegate {
  14. var animationStyle: FFAlertStyle {set get}
  15. func showShiftAnimatied(animationView : UIView)
  16. func showAnimated(animationView : UIView)
  17. func tapAction(tap: UIGestureRecognizer)
  18. func showBottomAnimated(animationView : UIView)
  19. func dismissBottomAnimated(animationView : UIView)
  20. func dismissAnimated(animationView : UIView)
  21. }
  22. var animationStyleKey = "animationStyleKey"
  23. extension UIViewController: AlertAnimation {
  24. var animationStyle: FFAlertStyle {
  25. get {
  26. return associatedObject(key: &animationStyleKey, initialiser: { () -> FFAlertStyle in
  27. return FFAlertStyle.middle
  28. })
  29. }
  30. set {
  31. associateObject(key: &animationStyleKey, value: newValue)
  32. }
  33. }
  34. func showShiftAnimatied(animationView : UIView) {
  35. let animation = CATransition()
  36. animation.duration = 0.2
  37. animation.subtype = kCATransitionFromTop
  38. animation.type = kCATransitionMoveIn
  39. let tap = UITapGestureRecognizer(target: self, action: #selector(tapAction(tap:)))
  40. tap.delegate = self
  41. view.addGestureRecognizer(tap)
  42. animationView.layer.add(animation, forKey: nil)
  43. }
  44. func showAnimated(animationView : UIView) {
  45. animationStyle = .middle
  46. let animation = CAKeyframeAnimation(keyPath: "transform")
  47. animation.duration = 0.2
  48. let array = [NSValue(caTransform3D: CATransform3DMakeScale(0.1, 0.1, 1.0)), NSValue(caTransform3D: CATransform3DMakeScale(1.0, 1.0, 1.0))]
  49. animation.values = array as [AnyObject]
  50. let tap = UITapGestureRecognizer(target: self, action: #selector(tapAction(tap:)))
  51. tap.delegate = self
  52. view.addGestureRecognizer(tap)
  53. animationView.layer.add(animation, forKey: nil)
  54. }
  55. @objc func tapAction(tap: UIGestureRecognizer) {
  56. switch animationStyle {
  57. case .middle:
  58. dismissAnimated(animationView: tap.view?.subviews.first ?? UIView())
  59. default:
  60. dismissBottomAnimated(animationView: tap.view?.subviews.first ?? UIView())
  61. }
  62. }
  63. func showBottomAnimated(animationView : UIView) {
  64. animationStyle = .bottom
  65. showShiftAnimatied(animationView: animationView)
  66. }
  67. func dismissBottomAnimated(animationView : UIView) {
  68. let position = animationView.layer.position
  69. let positions = CGPoint(x: kScreenWidth / 2, y: kScreenHeight + view.height)
  70. let basicPosition = CABasicAnimation(keyPath: "position")
  71. basicPosition.delegate = self
  72. basicPosition.fromValue = NSValue(cgPoint: position)
  73. basicPosition.toValue = NSValue(cgPoint: positions)
  74. basicPosition.duration = 0.2
  75. basicPosition.fillMode = kCAFillModeForwards
  76. basicPosition.isRemovedOnCompletion = false
  77. animationView.layer.add(basicPosition, forKey: nil)
  78. }
  79. func dismissAnimated(animationView : UIView) {
  80. let animation = CAKeyframeAnimation(keyPath: "transform")
  81. animation.duration = 0.4
  82. 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))]
  83. animation.values = array as [AnyObject]
  84. animation.delegate = self
  85. animation.isRemovedOnCompletion = false
  86. animation.fillMode = kCAFillModeForwards
  87. animationView.layer.add(animation, forKey: nil)
  88. }
  89. public func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldReceive touch: UITouch) -> Bool {
  90. if (touch.view?.superview?.isKind(of: UITableViewCell.classForCoder()))! {
  91. return false
  92. }
  93. return true
  94. }
  95. public func animationDidStop(_ anim: CAAnimation, finished flag: Bool) {
  96. self.removeControllerAndViewFromParent()
  97. }
  98. }