效果图:

image

使用方式

UIView,UIImageView,UIButton 一样

let mView = MView()

//是否开启水波纹效果,默认开启

mView.rippleEnable = true

//设置水波纹可以扩散至view外

mView.setRippleStyle(.unbounded)

mView.onClick {

//点击事件

}

UIView

import MaterialComponents

class MView :UIView {

//水波纹效果view

private var rippleView : MDCRippleView?

//是否开启水波纹效果

var rippleEnable = true

var onClick :(()->())?

// @objc func click(){

// onClick?()

// }

//设置点击事件

func onClick(todo : @escaping () ->()){

self.onClick = todo

self.isUserInteractionEnabled = true

//使用此方法设置点击事件会拦截触摸事件,无法显示水波纹效果

// let tapGesture = UITapGestureRecognizer(target: self, action: #selector(self.click))

// tapGesture.numberOfTapsRequired = 1

// self.addGestureRecognizer(tapGesture)

if rippleEnable {

//加入rippleview

if rippleView == nil {

rippleView = MDCRippleView(frame: self.bounds)

}

addSubview(rippleView!)

}

}

/**

扩散效果

.bounded - 扩散在view内

.unbounded - 可扩散至view外

*/

func setRippleStyle(_ style:MDCRippleStyle){

if rippleView == nil {

rippleView = MDCRippleView(frame: self.bounds)

}

rippleView?.rippleStyle = style

}

/**

触摸事件 - 开始触摸

*/

override func touchesBegan(_ touches: Set, with event: UIEvent?) {

super.touchesBegan(touches, with: event)

if rippleEnable {

//触摸位置

let point = ((touches as NSSet).anyObject() as AnyObject).location(in:self)

//设置rippleview的圆角 = 父view的圆角

rippleView?.layer.cornerRadius = self.layer.cornerRadius

//.unbounded 时,扩散的半径

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, with event: UIEvent?) {

super.touchesEnded(touches, with: event)

if rippleEnable {

//收回扩散

rippleView?.beginRippleTouchUp(animated: true, completion: nil)

}

onClick?()

}

/**

触摸取消

*/

override func touchesCancelled(_ touches: Set, with event: UIEvent?) {

super.touchesCancelled(touches, with: event)

if rippleEnable {

//收回扩散

rippleView?.beginRippleTouchUp(animated: true, completion: nil)

}

}

}

UIImageView

import MaterialComponents

class MImageView :UIImageView {

//同UIView

}

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

//添加点击事件

self.addTarget(self, action: #selector(self.click), for: .touchUpInside)

//取消点击图片高亮

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, 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, with event: UIEvent?) {

super.touchesEnded(touches, with: event)

if rippleEnable {

rippleView?.beginRippleTouchUp(animated: true, completion: nil)

}

}

override func touchesCancelled(_ touches: Set, with event: UIEvent?) {

super.touchesCancelled(touches, with: event)

if rippleEnable {

rippleView?.beginRippleTouchUp(animated: true, completion: nil)

}

}

}

水波纹效果开源库 点击这里

同理,cell中同样可以使用,以UICollectionViewCell为例,代码如下:

UICollectionViewCell

import MaterialComponents

class MCollectionViewCell: UICollectionViewCell {

private var rippleView : MDCRippleView!

//是否开启水波纹效果

var rippleEnable = true

override init(frame: CGRect) {

super.init(frame: frame)

//加入view

rippleView = MDCRippleView(frame: bounds)

addSubview(rippleView)

}

required init?(coder: NSCoder) {

fatalError("init(coder:) has not been implemented")

}

//设置要显示水波纹效果的view,不设置的话默认扩散至整个cell

func setRippleView(_ view:UIView){

rippleView.frame = view.bounds

view.addSubview(rippleView)

}

//触摸事件

override func touchesBegan(_ touches: Set, 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, with event: UIEvent?) {

super.touchesEnded(touches, with: event)

if rippleEnable {

rippleView?.beginRippleTouchUp(animated: true, completion: nil)

}

}

override func touchesCancelled(_ touches: Set, with event: UIEvent?) {

super.touchesCancelled(touches, with: event)

if rippleEnable {

rippleView?.beginRippleTouchUp(animated: true, completion: nil)

}

}

}

android水波纹加光圈扩散效果,swift UIView优雅的添加点击事件 android Material Design 水波纹扩散效果...相关推荐

  1. 高德地图, 点聚合效果,以及给点添加点击事件

    这个试用于高德地图, 首先要调用方法 'AMap.MarkerCluster', if (clusters) { clusters.setMap(null) } clusters = new VueA ...

  2. android html图片点击事件,Android TextView加载HTMl图文之添加点击事件和查看图片

    前言 用TextView显示Html图文,每一个需求都是需要探索的,不再是简单的添加点击事件就可以了. 1.如何添加点击事件 这里要使用上在Html.forHtml()方法中的第四个参数:Html.T ...

  3. android imageview 点击事件,Android ImageView点击效果

    ImageView设置点击效果需要注意两点,第一个设置android:clickable="true",第二个 属性必须放到最后才能生效 android:id="@+id ...

  4. Android群英传笔记——第十二章:Android5.X 新特性详解,Material Design UI的新体验

    Android群英传笔记--第十二章:Android5.X 新特性详解,Material Design UI的新体验 第十一章为什么不写,因为我很早之前就已经写过了,有需要的可以去看 Android高 ...

  5. android 部分区域点击,Android编程实现ListView中item部分区域添加点击事件功能

    本文实例讲述了Android编程实现ListView中item部分区域添加点击事件功能.分享给大家供大家参考,具体如下: 需求如题目:Android listview中item部分区域添加点击事件,在 ...

  6. [安卓开发]弹幕滚幕效果自定义View之BarrageView|支持点击事件|隐藏不滞留|颜色随机|大小速度范围随机

    安卓弹幕滚幕效果自定义View之BarrageView|支持点击事件|隐藏不滞留|颜色随机|大小速度范围随机 1.简介 项目地址: https://github.com/tpnet/BarrageVi ...

  7. Android开发之Notification(实现消息弹窗、提示音以及点击事件)

    文章目录 通知管理器 通知渠道 通知 发送通知 更多效果 添加点击事件 取消消息 通知管理器 通知管理器(NotificationManager)类是一个通知管理器,这个对象是由系统维护的服务,是以单 ...

  8. 百度地图调用加载显示Marker,并添加点击事件

    百度地图调用加载显示Marker,并添加点击事件 注册百度开发者账号,申请应用AK 百度地图开发平台官网 点击右上角控制台,选择创建应用 创建应用,勾选浏览器端,白名单填写* 注:如上线更改为公网IP ...

  9. textview点击事件 android,Android给TextView添加点击事件的实现方法

    首先设定TextView的clickable属性为true. 可以在布局文件中进行设定,比如: android:id="@+id/phone" android:clickable= ...

最新文章

  1. 快速排序java代码_8 种排序算法与 Java 代码实现!
  2. AppBarLayout、CollapsingToolbarLayout以及Toolbar
  3. Istio调用链埋点原理剖析—是否真的“零修改”?
  4. 大致了解写java聊天器所需要的技术
  5. 框架:SpringMVC的工作原理
  6. WeihanLi.Npoi 1.7.0 更新介绍
  7. SpringBoot集成thymeleaf增删改查示例
  8. R语言基础入门(4)之数据类型与相应运算2
  9. 用户故事与敏捷方法笔记 --- 用户故事
  10. 数据库左连接、右连接、内连接、全连接笔记
  11. 《Using OpenRefine》翻译~8
  12. 为什么现在社交电商这么火?
  13. 柳传志二十年驭人成就柳氏联想
  14. 随机位置生成小方块案例
  15. ZooKeeper Commands: The Four Letter Words
  16. dubbo使用带有密码的redis注册中心完整配置及遇到问题解决、RestTemplate配置【持续更新】
  17. SQL Server数据库表的基本操作(批量插入、删除、查询数据,删除表中重复数据方法)
  18. Database:MySQL
  19. Zookeeper--简介
  20. 基于目标检测的狗品种识别及图像检索

热门文章

  1. 关于大数据系统及Hadoop系统中的概念
  2. ef mysql 配置字符串_连接字符串-EF Core | Microsoft Docs
  3. Java_Day2 变量、基本数据类型、运算符
  4. 文本框内容可以删除,就是不想让用户手工写入
  5. 第一天赔了4万,之后一个月倒赚了200万?只能默默说一句“牛x”
  6. CAD制图初学入门绘图
  7. 糟糕的横向发展 可穿戴厂商的迷途
  8. python原码反码补码
  9. “类的使用”编程规范(一)覆写(重写)、重载、隐藏、遮蔽、遮掩
  10. 女孩顶球-第10届蓝桥杯Scratch省赛真题第1题