Я хотел добавить волновой эффект к UITableViewCell аналогично этой ссылке https://raw.githubusercontent.com/bfeher/BFPaperTableViewCell/master/BFPaperTableViewCellDemoGif.gif. Есть много доступных модулей, но я хочу узнать, как делается такая анимация. Я добавил несколько строк кода в делегат didHighlightRowAt для выполнения анимации пульсации, но анимация не произошла.
Вот код того, что я сделал до сих пор
func tableView(_ tableView: UITableView, didHighlightRowAt indexPath: IndexPath) {
let cell = tableView.cellForRow(at: indexPath)!
cell.contentView.subviews.forEach { (view) in
view.layer.masksToBounds = true
print(view.layer.cornerRadius)
let startAnimation = UIBezierPath(rect: CGRect(x: view.bounds.origin.x, y: view.bounds.origin.y, width: view.bounds.width, height: view.bounds.height)).cgPath
let endAnimation = UIBezierPath(rect: CGRect(x: view.bounds.width, y: view.bounds.origin.y, width: view.bounds.width, height: view.bounds.height)).cgPath
let shapeLayer = CAShapeLayer()
let compinedLayer = CGMutablePath()
shapeLayer.fillColor = UIColor.blue.cgColor
shapeLayer.cornerRadius = 10.0
shapeLayer.opacity = 0.5
let animation = CABasicAnimation(keyPath: "ripple")
animation.fromValue = startAnimation
animation.toValue = endAnimation
animation.duration = 3
animation.fillMode = CAMediaTimingFillMode.both
animation.timingFunction = CAMediaTimingFunction(name: CAMediaTimingFunctionName.easeOut)
animation.isRemovedOnCompletion = false
compinedLayer.addPath(startAnimation)
compinedLayer.addPath(endAnimation)
shapeLayer.path = compinedLayer
view.layer.insertSublayer(shapeLayer, at: 0)
shapeLayer.add(animation, forKey: "ripple")
let deatline = DispatchTime(uptimeNanoseconds: UInt64((3 * 0.75)) * NSEC_PER_SEC)
DispatchQueue.main.asyncAfter(deadline: deatline, execute: {
let opacityAnimation = CABasicAnimation(keyPath: "opacity")
opacityAnimation.fillMode = CAMediaTimingFillMode.both
opacityAnimation.duration = 3 - (3 * 0.75)
opacityAnimation.fromValue = endAnimation
opacityAnimation.toValue = startAnimation
opacityAnimation.isRemovedOnCompletion = false
shapeLayer.add(opacityAnimation, forKey: "opacity")
})
let deadline = DispatchTime(uptimeNanoseconds: UInt64(3 - (3 * 0.75)))
DispatchQueue.main.asyncAfter(deadline: deadline, execute: {
shapeLayer.removeAllAnimations()
shapeLayer.removeFromSuperlayer()
})
}
}
func tableView(_ tableView: UITableView, shouldHighlightRowAt indexPath: IndexPath) -> Bool {
return true
}