Effect:

UIView, UIImageView, UIButton

Let mView = mView () Mview. rippleEnable = true by default mview. setRippleStyle(.unbounded) mview. onClick {// Click events}Copy the code

UIView

Import MaterialComponents class MView :UIView {private var rippleView: MDCRippleView? Import MaterialComponents class MView :UIView {private var rippleView: MDCRippleView? Var onClick :(()->())? // @objc func click(){ // onClick? // set the click event func onClick(todo: @escaping () ->()){self.onClick = todo // Use this method to set click events to intercept touch events, Unable to display water ripple effect / / self. The isUserInteractionEnabled = true / / let tapGesture = UITapGestureRecognizer (target: the self, the action: #selector(self.click)) // tapGesture.numberOfTapsRequired = 1 // self.addGestureRecognizer(tapGesture) if rippleEnable { // Add rippleView if rippleView == nil {rippleView = MDCRippleView(frame: self.bounds)} addSubview(rippleView!) If a line between them is bounded by a line between them and a line between them. If a line between them is bounded by a line between them and a line between them and a line between them  nil { rippleView = MDCRippleView(frame: self.bounds) } rippleView? .view = view; view = view; view = view; view = view; view = view { super.touchesBegan(touches, with: // Touches as NSSet. AnyObject () as anyObject. Location (in:self) // Set rippleView rounded = parent view rounded rippleView rounded? .layer.cornerRadius = self.layer.cornerradius //. Unbounded radius when bounded by rippleView? .maximumradius = SQRT (self.bounds.width*self.bounds.width + self.bounds.height*self.bounds.height)/2 // Start the diffusion animation rippleView? .beginrippleTouchDown (at: point, animated: true, completion: nil)}} /** Touches ended (_ touches: Set<UITouch>, with event: UIEvent?) {super.touches ended (touches, with: event) if rippleEnable {// Touches? .beginRippleTouchUp(animated: true, completion: nil) } onClick? ()} /** touches cancelled (_ touches: Set< uittouch >, with event: UIEvent?) {super.touchesCancelled(touches, with: event) if rippleEnable { .beginRippleTouchUp(animated: true, completion: nil) } } }Copy the code

UIImageView

class MImageView :UIImageView {... With the UIView}Copy the code

UIButton

import MaterialComponents class MButton: UIButton { private var rippleView : MDCRippleView? var rippleEnable = true private var onClick :(()->())? @objc func click(){ onClick? ()} func onClick(todo: @escaping () ->()){self.onclick = todo #selector(self.click), for: . TouchUpInside) / / cancel click image highlight adjustsImageWhenHighlighted = false if rippleEnable {if rippleView = = nil {rippleView = MDCRippleView(frame: self.bounds) } addSubview(rippleView!) } } func setRippleStyle(_ style:MDCRippleStyle){ if rippleView == nil { rippleView = MDCRippleView(frame: self.bounds) } rippleView? .rippleStyle = style } override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) { super.touchesBegan(touches, with: event) if rippleEnable { let point = ((touches as NSSet).anyObject() as AnyObject).location(in:self) rippleView? .layer.cornerRadius = self.layer.cornerRadius rippleView? .maximumRadius = sqrt(self.bounds.width*self.bounds.width + self.bounds.height*self.bounds.height)/2 rippleView? .beginRippleTouchDown(at: point, animated: true, completion: nil) } } override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) { super.touchesEnded(touches, with: event) if rippleEnable { rippleView? .beginRippleTouchUp(animated: true, completion: nil) } } override func touchesCancelled(_ touches: Set<UITouch>, with event: UIEvent?) { super.touchesCancelled(touches, with: event) if rippleEnable { rippleView? .beginRippleTouchUp(animated: true, completion: nil) } } }Copy the code

Water ripple Effect Open source library click here

Similarly, the UICollectionViewCell can also be used. Take UICollectionViewCell as an example. The code is as follows:

UICollectionViewCell

import MaterialComponents class MCollectionViewCell: UICollectionViewCell { private var rippleView : MDCRippleView! Var rippleEnable = true Override init(frame: CGRect) {super.init(frame: Frame) // Add view rippleView = MDCRippleView(frame: bounds) addSubview(rippleView)} required init? (coder: NSCoder) {fatalError("init(coder:) has not been implemented")} Func setRippleView(_ view:UIView){rippleview.frame = view.bounds view.addSubView (rippleView)} // Touch events override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) { super.touchesBegan(touches, with: event) if rippleEnable { let point = ((touches as NSSet).anyObject() as AnyObject).location(in:self) rippleView?.beginRippleTouchDown(at: point, animated: true, completion: nil) } } override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) { super.touchesEnded(touches, with: event) if rippleEnable { rippleView?.beginRippleTouchUp(animated: true, completion: nil) } } override func touchesCancelled(_ touches: Set<UITouch>, with event: UIEvent?) { super.touchesCancelled(touches, with: event) if rippleEnable { rippleView?.beginRippleTouchUp(animated: true, completion: nil) } } }Copy the code