Skip to content

Commit

Permalink
Clean up PlayButtonView code
Browse files Browse the repository at this point in the history
  • Loading branch information
SD10 committed Sep 18, 2017
1 parent e15f9f4 commit dfad049
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 34 deletions.
23 changes: 16 additions & 7 deletions Sources/MediaMessageCell.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,22 +26,31 @@ import UIKit

open class MediaMessageCell: MessageCollectionViewCell<UIImageView> {

// MARK: - Properties

open lazy var playButtonView: PlayButtonView = {
let playButtonView = PlayButtonView()
playButtonView.triangleView.backgroundColor = .black
playButtonView.backgroundColor = .playButtonLightGray
playButtonView.frame.size = CGSize(width: 35, height: 35)
return playButtonView
}()

// MARK: - Methods

private func setupConstraints() {
playButtonView.translatesAutoresizingMaskIntoConstraints = false

let centerX = playButtonView.centerXAnchor.constraint(equalTo: centerXAnchor)
let centerY = playButtonView.centerYAnchor.constraint(equalTo: centerYAnchor)
let width = playButtonView.widthAnchor.constraint(equalToConstant: playButtonView.bounds.width)
let height = playButtonView.heightAnchor.constraint(equalToConstant: playButtonView.bounds.height)

NSLayoutConstraint.activate([centerX, centerY, width, height])
}

override func setupSubviews() {
super.setupSubviews()
messageContentView.addSubview(playButtonView)
playButtonView.translatesAutoresizingMaskIntoConstraints = false
playButtonView.centerXAnchor.constraint(equalTo: centerXAnchor).isActive = true
playButtonView.centerYAnchor.constraint(equalTo: centerYAnchor).isActive = true
playButtonView.widthAnchor.constraint(equalToConstant: 35).isActive = true
playButtonView.heightAnchor.constraint(equalToConstant: 35).isActive = true
setupConstraints()
}

override open func configure(with message: MessageType, at indexPath: IndexPath, and messagesCollectionView: MessagesCollectionView) {
Expand Down
95 changes: 68 additions & 27 deletions Sources/PlayButtonView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,64 +26,105 @@ import UIKit

open class PlayButtonView: UIView {

// MARK: - Properties

open let triangleView = UIView()

private var triangleHeightConstraint: NSLayoutConstraint?
private var triangleWidthConstraint: NSLayoutConstraint?
private var triangleCenterXConstraint: NSLayoutConstraint?

private var triangleViewSize: CGSize {
return CGSize(width: frame.width/2, height: frame.height/2)
}

override open var frame: CGRect {
didSet {
commonSetup()
updateTriangleConstraints()
applyCornerRadius()
applyTriangleMask()
}
}

override open var bounds: CGRect {
didSet {
commonSetup()
updateTriangleConstraints()
applyCornerRadius()
applyTriangleMask()
}
}

open let triangleView: UIView = {
let triangleView = UIView()
triangleView.layer.masksToBounds = true
// MARK: - Initializers

override public init(frame: CGRect) {
super.init(frame: frame)

setupSubviews()
setupConstraints()
applyCornerRadius()
applyTriangleMask()

triangleView.clipsToBounds = true
return triangleView
}()
triangleView.backgroundColor = .black
backgroundColor = .playButtonLightGray
}

required public init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}

// MARK: - Methods

private func setupSubviews() {
addSubview(triangleView)
}

private func setupConstraints() {
triangleView.translatesAutoresizingMaskIntoConstraints = false

let centerX = triangleView.centerXAnchor.constraint(equalTo: centerXAnchor, constant: triangleViewSize.width/8)
let centerY = triangleView.centerYAnchor.constraint(equalTo: centerYAnchor)
let width = triangleView.widthAnchor.constraint(equalToConstant: triangleViewSize.width)
let height = triangleView.heightAnchor.constraint(equalToConstant: triangleViewSize.height)

triangleWidthConstraint = width
triangleHeightConstraint = height
triangleCenterXConstraint = centerX

NSLayoutConstraint.activate([centerX, centerY, width, height])
}

private func triangleMask(for frame: CGRect) -> CAShapeLayer {
let shapeLayer = CAShapeLayer()
let trianglePath = UIBezierPath()

let point1 = CGPoint(x: frame.minX, y: frame.minY)
let point2 = CGPoint(x: frame.maxX, y: frame.maxY/2)
let point3 = CGPoint(x: frame.minX, y: frame.maxY)

trianglePath .move(to: point1)
trianglePath .addLine(to: point2)
trianglePath .addLine(to: point3)
trianglePath .close()

shapeLayer.path = trianglePath.cgPath
return shapeLayer
}

override public init(frame: CGRect) {
super.init(frame: frame)
setupSubviews()
commonSetup()
return shapeLayer
}

required public init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
private func updateTriangleConstraints() {
triangleWidthConstraint?.constant = triangleViewSize.width
triangleHeightConstraint?.constant = triangleViewSize.height
triangleCenterXConstraint?.constant = triangleViewSize.width/8
}

private func setupSubviews() {
addSubview(triangleView)
private func applyTriangleMask() {
let rect = CGRect(origin: .zero, size: triangleViewSize)
triangleView.layer.mask = triangleMask(for: rect)
}

private func commonSetup() {

let size = CGSize(width: frame.width/2, height: frame.height/2)
triangleView.bounds.size = size
triangleView.layer.mask = triangleMask(for: triangleView.bounds)

let centerOffset = triangleView.bounds.width/8
triangleView.center = CGPoint(x: center.x + centerOffset, y: center.y)

private func applyCornerRadius() {
layer.cornerRadius = frame.width / 2
layer.masksToBounds = true
}

}

0 comments on commit dfad049

Please sign in to comment.