showing ripple effect when table view cell is highlighted











up vote
1
down vote

favorite












I wanted to add ripple effect to UITableViewCell similar to this link 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.
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
}









share|improve this question


























    up vote
    1
    down vote

    favorite












    I wanted to add ripple effect to UITableViewCell similar to this link 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.
    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
    }









    share|improve this question
























      up vote
      1
      down vote

      favorite









      up vote
      1
      down vote

      favorite











      I wanted to add ripple effect to UITableViewCell similar to this link 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.
      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
      }









      share|improve this question













      I wanted to add ripple effect to UITableViewCell similar to this link 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.
      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
      }






      ios swift uitableview






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 11 at 10:04









      junky

      104




      104
























          1 Answer
          1






          active

          oldest

          votes

















          up vote
          0
          down vote













          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. Secondly instead of using all the dispatch times you can use CATransactions and completion blocks. For any delays use beginTime property. Hope this helps. Here is a similar question so you can look at my code. Material Ripple Effect for UICollectionView Cell . 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. Honestly when deconstructing animation I record them and scrub through them slowly. There are some great resources out there about CALayer as well. Good luck learning it is super powerful.






          share|improve this answer





















            Your Answer






            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "1"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53247623%2fshowing-ripple-effect-when-table-view-cell-is-highlighted%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes








            up vote
            0
            down vote













            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. Secondly instead of using all the dispatch times you can use CATransactions and completion blocks. For any delays use beginTime property. Hope this helps. Here is a similar question so you can look at my code. Material Ripple Effect for UICollectionView Cell . 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. Honestly when deconstructing animation I record them and scrub through them slowly. There are some great resources out there about CALayer as well. Good luck learning it is super powerful.






            share|improve this answer

























              up vote
              0
              down vote













              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. Secondly instead of using all the dispatch times you can use CATransactions and completion blocks. For any delays use beginTime property. Hope this helps. Here is a similar question so you can look at my code. Material Ripple Effect for UICollectionView Cell . 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. Honestly when deconstructing animation I record them and scrub through them slowly. There are some great resources out there about CALayer as well. Good luck learning it is super powerful.






              share|improve this answer























                up vote
                0
                down vote










                up vote
                0
                down vote









                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. Secondly instead of using all the dispatch times you can use CATransactions and completion blocks. For any delays use beginTime property. Hope this helps. Here is a similar question so you can look at my code. Material Ripple Effect for UICollectionView Cell . 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. Honestly when deconstructing animation I record them and scrub through them slowly. There are some great resources out there about CALayer as well. Good luck learning it is super powerful.






                share|improve this answer












                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. Secondly instead of using all the dispatch times you can use CATransactions and completion blocks. For any delays use beginTime property. Hope this helps. Here is a similar question so you can look at my code. Material Ripple Effect for UICollectionView Cell . 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. Honestly when deconstructing animation I record them and scrub through them slowly. There are some great resources out there about CALayer as well. Good luck learning it is super powerful.







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 11 at 14:32









                agibson007

                2,4062714




                2,4062714






























                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.





                    Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                    Please pay close attention to the following guidance:


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53247623%2fshowing-ripple-effect-when-table-view-cell-is-highlighted%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown