简体   繁体   English

在表格视图单元格突出显示时显示涟漪效应

[英]showing ripple effect when table view cell is highlighted

I wanted to add ripple effect to UITableViewCell similar to this link https://raw.githubusercontent.com/bfeher/BFPaperTableViewCell/master/BFPaperTableViewCellDemoGif.gif . 我想向UITableViewCell添加波纹效果,类似于此链接https://raw.githubusercontent.com/bfeher/BFPaperTableViewCell/master/BFPaperTableViewCellDemoGif.gif There are many pod available but i want to learn how such animation are done. 有很多可用的广告连播,但我想学习如何制作动画。 I added few line of code in didHighlightRowAt delegate to perform ripple animation but animation didn't happened. 我在didHighlightRowAt委托中添加了几行代码来执行波纹动画,但是动画没有发生。 Here is a code of what i've done so far 这是我到目前为止所做的代码

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
        }

I have not run the code but the first glaring issue is on the opacity animation. 我没有运行代码,但第一个明显的问题是不透明度动画。 The from and toValues need to be a number from 1 to 0 and I believe you are adding a path used for the shapelayer instead. from和toValues必须是从1到0的数字,我相信您正在添加用于shapelayer的路径。 Secondly instead of using all the dispatch times you can use CATransactions and completion blocks. 其次,可以使用CATransactions和完成块来代替使用所有调度时间。 For any delays use beginTime property. 对于任何延迟,请使用beginTime属性。 Hope this helps. 希望这可以帮助。 Here is a similar question so you can look at my code. 这是一个类似的问题,因此您可以看一下我的代码。 Material Ripple Effect for UICollectionView Cell . UICollectionView单元的物质波纹效应 Some other things I notice is in the first click of the video you posted it appears a CATransition of a ripple is also placed on the image to distort it as well. 我注意到的其他一些情况是,在您发布的视频的第一次单击中,它似乎在图像上也放置了CATransition波纹,也使图像变形。 Honestly when deconstructing animation I record them and scrub through them slowly. 老实说,在解构动画时,我会录制它们并慢慢地擦洗它们。 There are some great resources out there about CALayer as well. 也有一些有关CALayer的资源。 Good luck learning it is super powerful. 祝您好运,它超级强大。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM