O2DemoAlertView.swift 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. //
  2. // O2DemoAlertView.swift
  3. // O2Platform
  4. //
  5. // Created by FancyLou on 2019/1/22.
  6. // Copyright © 2019 zoneland. All rights reserved.
  7. //
  8. import UIKit
  9. import CocoaLumberjack
  10. class O2DemoAlertView: UIView {
  11. let backFrame = CGRect.init(x: 0, y: 0, width: SCREEN_WIDTH, height: SCREEN_HEIGHT)
  12. let backColor = UIColor(r: 0, g: 0, b: 0, a: 0.6)
  13. var boardView = UIView()
  14. var closeBtn = UIButton.init(type: UIButton.ButtonType.custom)
  15. func initView() -> UIView {
  16. self.frame = backFrame
  17. self.isHidden = true
  18. self.backgroundColor = backColor
  19. return self
  20. }
  21. private func addBoardView() {
  22. // 公告内容放到屏幕外面
  23. let x = (SCREEN_WIDTH - 315 ) / 2
  24. self.boardView.frame = CGRect.init(x: x, y: -(SCREEN_HEIGHT), width: 315, height: 485)
  25. self.boardView.backgroundColor = UIColor.clear
  26. let boardBackImage = UIImageView.init(frame: CGRect.init(x: 0, y: 0, width: 315, height: 485))
  27. boardBackImage.layer.masksToBounds = true
  28. boardBackImage.contentMode = UIView.ContentMode.scaleAspectFill
  29. boardBackImage.image = UIImage(named: "pic_czsm")
  30. self.boardView.addSubview(boardBackImage)//添加公告
  31. let closeX = CGFloat(315 - 5 - 22)
  32. self.closeBtn.frame = CGRect.init(x: closeX, y: 5, width: 22, height: 22) // 关闭按钮在公告的右上角 right:5 top:5
  33. self.closeBtn.setImage(UIImage(named: "icon_off_white2"), for: UIControl.State.normal)
  34. self.closeBtn.addTarget(self, action: #selector(closeAlertView), for: UIControl.Event.touchUpInside)
  35. self.boardView.addSubview(self.closeBtn)
  36. self.closeBtn.isHidden = true //关闭按钮隐藏
  37. }
  38. func showFallDown() {
  39. UIApplication.shared.keyWindow?.addSubview(initView())
  40. self.isHidden = false //显示背景
  41. addBoardView() //添加公告
  42. self.addSubview(self.boardView)
  43. //执行动画 从上往下掉落 回弹一下
  44. let firstY = SCREEN_HEIGHT - 485
  45. let secondY = CGFloat(0.0)
  46. let lastY = (SCREEN_HEIGHT - 485) / 2
  47. UIView.animate(withDuration: 0.5, animations: {
  48. self.boardView.frame.origin.y = firstY
  49. }) { (_) in
  50. UIView.animate(withDuration: 0.2, animations: {
  51. self.boardView.frame.origin.y = secondY
  52. }, completion: { (_) in
  53. UIView.animate(withDuration: 0.2, animations: {
  54. self.boardView.frame.origin.y = lastY
  55. }, completion: { (_) in
  56. self.closeBtn.isHidden = false
  57. })
  58. })
  59. }
  60. }
  61. @objc func closeAlertView() {
  62. self.closeBtn.isHidden = true
  63. UIView.animate(withDuration: 0.5, animations: {
  64. self.boardView.frame.origin.y = -(SCREEN_HEIGHT)
  65. }) { (_) in
  66. self.removeFromSuperview()
  67. }
  68. }
  69. }