PlayButtonView.swift 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. /*
  2. MIT License
  3. Copyright (c) 2017-2018 MessageKit
  4. Permission is hereby granted, free of charge, to any person obtaining a copy
  5. of this software and associated documentation files (the "Software"), to deal
  6. in the Software without restriction, including without limitation the rights
  7. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. copies of the Software, and to permit persons to whom the Software is
  9. furnished to do so, subject to the following conditions:
  10. The above copyright notice and this permission notice shall be included in all
  11. copies or substantial portions of the Software.
  12. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  13. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  14. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  15. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  16. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  17. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  18. SOFTWARE.
  19. */
  20. import UIKit
  21. open class PlayButtonView: UIView {
  22. // MARK: - Properties
  23. public let triangleView = UIView()
  24. private var triangleCenterXConstraint: NSLayoutConstraint?
  25. private var cacheFrame: CGRect = .zero
  26. // MARK: - Initializers
  27. public override init(frame: CGRect) {
  28. super.init(frame: frame)
  29. setupSubviews()
  30. setupConstraints()
  31. triangleView.clipsToBounds = true
  32. triangleView.backgroundColor = .black
  33. backgroundColor = .playButtonLightGray
  34. }
  35. required public init?(coder aDecoder: NSCoder) {
  36. fatalError("init(coder:) has not been implemented")
  37. }
  38. // MARK: - Methods
  39. open override func layoutSubviews() {
  40. super.layoutSubviews()
  41. guard !cacheFrame.equalTo(frame) else { return }
  42. cacheFrame = frame
  43. updateTriangleConstraints()
  44. applyCornerRadius()
  45. applyTriangleMask()
  46. }
  47. private func setupSubviews() {
  48. addSubview(triangleView)
  49. }
  50. private func setupConstraints() {
  51. triangleView.translatesAutoresizingMaskIntoConstraints = false
  52. let centerX = triangleView.centerXAnchor.constraint(equalTo: centerXAnchor)
  53. let centerY = triangleView.centerYAnchor.constraint(equalTo: centerYAnchor)
  54. let width = triangleView.widthAnchor.constraint(equalTo: widthAnchor, multiplier: 0.5)
  55. let height = triangleView.heightAnchor.constraint(equalTo: heightAnchor, multiplier: 0.5)
  56. triangleCenterXConstraint = centerX
  57. NSLayoutConstraint.activate([centerX, centerY, width, height])
  58. }
  59. private func triangleMask(for frame: CGRect) -> CAShapeLayer {
  60. let shapeLayer = CAShapeLayer()
  61. let trianglePath = UIBezierPath()
  62. let point1 = CGPoint(x: frame.minX, y: frame.minY)
  63. let point2 = CGPoint(x: frame.maxX, y: frame.maxY/2)
  64. let point3 = CGPoint(x: frame.minX, y: frame.maxY)
  65. trianglePath .move(to: point1)
  66. trianglePath .addLine(to: point2)
  67. trianglePath .addLine(to: point3)
  68. trianglePath .close()
  69. shapeLayer.path = trianglePath.cgPath
  70. return shapeLayer
  71. }
  72. private func updateTriangleConstraints() {
  73. triangleCenterXConstraint?.constant = frame.width/8
  74. }
  75. private func applyTriangleMask() {
  76. let rect = CGRect(origin: .zero, size: triangleView.bounds.size)
  77. triangleView.layer.mask = triangleMask(for: rect)
  78. }
  79. private func applyCornerRadius() {
  80. layer.cornerRadius = frame.width / 2
  81. }
  82. }