AudioTouchButton.swift 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. //
  2. // AudioTouchButton.swift
  3. // O2Platform
  4. //
  5. // Created by FancyLou on 2020/7/7.
  6. // Copyright © 2020 zoneland. All rights reserved.
  7. //
  8. import UIKit
  9. import SnapKit
  10. class AudioTouchButton: UIView {
  11. var areaY: CGFloat = 40
  12. var clickTime = 0.5
  13. var touchBegan: (()->Void)? = nil
  14. var upglide: (()->Void)? = nil
  15. var down: (()->Void)? = nil
  16. var touchEnd: (()->Void)? = nil
  17. var voiceButton: UIButton?
  18. private var isBegan: Bool = false
  19. private var timer: Timer?
  20. override init(frame: CGRect) {
  21. super.init(frame: frame)
  22. self.areaY = -40
  23. self.clickTime = 0.5
  24. self.isBegan = false
  25. self.voiceButton = UIButton(type: .custom)
  26. self.addSubview(self.voiceButton!)
  27. self.voiceButton?.titleLabel?.font = .systemFont(ofSize: 14)
  28. self.voiceButton?.isUserInteractionEnabled = false
  29. self.voiceButton?.snp_makeConstraints({ (make) in
  30. make.edges.equalTo(self)
  31. })
  32. }
  33. required init?(coder: NSCoder) {
  34. fatalError("init(coder:) has not been implemented")
  35. }
  36. override func point(inside point: CGPoint, with event: UIEvent?) -> Bool {
  37. if self.voiceButton?.isSelected == true {
  38. return true
  39. }else {
  40. return super.point(inside: point, with: event)
  41. }
  42. }
  43. override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
  44. let timer = Timer.scheduledTimer(timeInterval: self.clickTime, target: self, selector: #selector(timeAction), userInfo: nil, repeats: false)
  45. print("++++++++++++++++++开始")
  46. self.timer = timer
  47. }
  48. override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
  49. if self.voiceButton?.isSelected == true {
  50. let anchTouch = touches.first
  51. let point = anchTouch?.location(in: self)
  52. if point?.y ?? 0 > self.areaY {
  53. self.down?()
  54. }else {
  55. self.upglide?()
  56. }
  57. }
  58. }
  59. override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
  60. if self.voiceButton?.isSelected == true {
  61. self.touchEnd?()
  62. }
  63. self.voiceButton?.isSelected = false
  64. self.timer?.invalidate()
  65. self.timer = nil
  66. print("+++++++++++++++++取消")
  67. }
  68. @objc func timeAction() {
  69. self.touchBegan?()
  70. print("++++++++++++执行");
  71. self.voiceButton?.isSelected = true
  72. self.timer?.invalidate()
  73. }
  74. }