AudioTouchButton.swift 2.5 KB

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