Here is a small implementation based on CAShapeLayer
, that basically clones the behavior described in the link.
class PlayPauseButton: UIControl {
// public function can be called from out interface
func setPlaying(_ playing: Bool) {
self.playing = playing
animateLayer()
}
private (set) var playing: Bool = false
private let leftLayer: CAShapeLayer
private let rightLayer: CAShapeLayer
override init(frame: CGRect) {
leftLayer = CAShapeLayer()
rightLayer = CAShapeLayer()
super.init(frame: frame)
backgroundColor = UIColor.white
setupLayers()
}
required init?(coder aDecoder: NSCoder) {
fatalError("Not implemented")
}
private func setupLayers() {
layer.addSublayer(leftLayer)
layer.addSublayer(rightLayer)
leftLayer.fillColor = UIColor.black.cgColor
rightLayer.fillColor = UIColor.black.cgColor
addTarget(self,
action: #selector(pressed),
for: .touchUpInside)
}
@objc private func pressed() {
setPlaying(!playing)
}
private func animateLayer() {
let fromLeftPath = leftLayer.path
let toLeftPath = leftPath()
leftLayer.path = toLeftPath
let fromRightPath = rightLayer.path
let toRightPath = rightPath()
rightLayer.path = toRightPath
let leftPathAnimation = pathAnimation(fromPath: fromLeftPath,
toPath: toLeftPath)
let rightPathAnimation = pathAnimation(fromPath: fromRightPath,
toPath: toRightPath)
leftLayer.add(leftPathAnimation,
forKey: nil)
rightLayer.add(rightPathAnimation,
forKey: nil)
}
private func pathAnimation(fromPath: CGPath?,
toPath: CGPath) -> CAAnimation {
let animation = CABasicAnimation(keyPath: "path")
animation.duration = 0.33
animation.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseIn)
animation.fromValue = fromPath
animation.toValue = toPath
return animation
}
override func layoutSubviews() {
leftLayer.frame = leftLayerFrame
rightLayer.frame = rightLayerFrame
leftLayer.path = leftPath()
rightLayer.path = rightPath()
}
private let pauseButtonLineSpacing: CGFloat = 10
private var leftLayerFrame: CGRect {
return CGRect(x: 0,
y: 0,
width: bounds.width * 0.5,
height: bounds.height)
}
private var rightLayerFrame: CGRect {
return leftLayerFrame.offsetBy(dx: bounds.width * 0.5,
dy: 0)
}
private func leftPath() -> CGPath {
if playing {
let bound = leftLayer.bounds
.insetBy(dx: pauseButtonLineSpacing,
dy: 0)
.offsetBy(dx: -pauseButtonLineSpacing,
dy: 0)
return UIBezierPath(rect: bound).cgPath
}
return leftLayerPausedPath()
}
private func rightPath() -> CGPath {
if playing {
let bound = rightLayer.bounds
.insetBy(dx: pauseButtonLineSpacing,
dy: 0)
.offsetBy(dx: pauseButtonLineSpacing,
dy: 0)
return UIBezierPath(rect: bound).cgPath
}
return rightLayerPausedPath()
}
private func leftLayerPausedPath() -> CGPath {
let y1 = leftLayerFrame.width * 0.5
let y2 = leftLayerFrame.height - leftLayerFrame.width * 0.5
let path = UIBezierPath()
path.move(to:CGPoint(x: 0, y: 0))
path.addLine(to: CGPoint(x: leftLayerFrame.width,
y: y1))
path.addLine(to: CGPoint(x: leftLayerFrame.width,
y: y2))
path.addLine(to: CGPoint(x: 0,
y: leftLayerFrame.height))
path.close()
return path.cgPath
}
private func rightLayerPausedPath() -> CGPath {
let y1 = rightLayerFrame.width * 0.5
let y2 = rightLayerFrame.height - leftLayerFrame.width * 0.5
let path = UIBezierPath()
path.move(to:CGPoint(x: 0, y: y1))
path.addLine(to: CGPoint(x: rightLayerFrame.width,
y: rightLayerFrame.height * 0.5))
path.addLine(to: CGPoint(x: rightLayerFrame.width,
y: rightLayerFrame.height * 0.5))
path.addLine(to: CGPoint(x: 0,
y: y2))
path.close()
return path.cgPath
}
}
The code should be pretty straight forward. Here is how you could use it,
let playButton = PlayPauseButton(frame: CGRect(x: 0,
y: 0,
width: 200,
height: 200))
view.addSubview(playButton)
You dont need to handle anything from outside. The button itself handles animation. You could use target/action for changing detecting the button click. Also, you could add animate the status from outside using public method,
playButton.setPlaying(true) // animate to playing
playButton.setPlaying(false) // animate to paused state
And here is how it looks,
