iOS上手指点击波纹效果的实现
https://www.jianshu.com/p/35e6f53ca0fe
闲暇时间做了一个反馈手指点击屏幕的效果,用到了CAShapeLayer和基本的动画知识,模拟器上效果如下:

- 这种效果使用在某些页面上肯定会给用户更有趣的体验,特别是面向儿童的app中
具体的实现代码如下
- 首先监听控制器view的Tap事件
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(onTap:)];
[self.view addGestureRecognizer:tap];
- (void)onTap:(UITapGestureRecognizer*)sender {
CGPoint center = [sender locationInView:sender.view];
[FingerWaveView showInView:self.view center:center];
}
- FingerWaveView.h
#import <UIKit/UIKit.h>
@interface FingerWaveView : UIView
+ (instancetype)showInView:(UIView *)view center:(CGPoint)center;
@end
- FingerWaveView.m
#import "FingerWaveView.h"
@interface FingerWaveView () <CAAnimationDelegate>
{
CGSize waveSize;
NSTimeInterval duration;
}
@end
@implementation FingerWaveView
- (instancetype)initWithFrame:(CGRect)frame{
self=[super initWithFrame:frame];
if (self) {
waveSize = CGSizeMake(150, 150);
duration = 1.0;
}
return self;
}
+ (instancetype)showInView:(UIView *)view center:(CGPoint)center {
FingerWaveView *waveView = [FingerWaveView new];
[waveView setframeWithCenter:center];
[view addSubview:waveView];
return waveView;
}
- (void)didMoveToSuperview{
CAShapeLayer *waveLayer = [CAShapeLayer new];
waveLayer.backgroundColor = [UIColor clearColor].CGColor;
waveLayer.opacity = 0.6;
waveLayer.fillColor = [UIColor whiteColor].CGColor;
[self.layer addSublayer:waveLayer];
[self startAnimationInLayer:waveLayer];
}
- (void)startAnimationInLayer:(CALayer *)layer{
UIBezierPath *beginPath = [UIBezierPath bezierPathWithArcCenter:[self pathCenter] radius:[self animationBeginRadius] startAngle:0 endAngle:M_PI*2 clockwise:YES];
UIBezierPath *endPath = [UIBezierPath bezierPathWithArcCenter:[self pathCenter] radius:[self animationEndRadius] startAngle:0 endAngle:M_PI*2 clockwise:YES];
CABasicAnimation *rippleAnimation = [CABasicAnimation animationWithKeyPath:@"path"];
rippleAnimation.delegate = self;
rippleAnimation.fromValue = (__bridge id _Nullable)(beginPath.CGPath);
rippleAnimation.toValue = (__bridge id _Nullable)(endPath.CGPath);
rippleAnimation.duration = duration;
CABasicAnimation *opacityAnimation = [CABasicAnimation animationWithKeyPath:@"opacity"];
opacityAnimation.delegate = self;
opacityAnimation.fromValue = [NSNumber numberWithFloat:0.6];
opacityAnimation.toValue = [NSNumber numberWithFloat:0.0];
opacityAnimation.duration = duration;
[layer addAnimation:rippleAnimation forKey:@"rippleAnimation"];
[layer addAnimation:opacityAnimation forKey:@"opacityAnimation"];
}
- (void)setframeWithCenter:(CGPoint)center{
CGRect frame = CGRectMake(center.x-waveSize.width*0.5, center.y-waveSize.height*0.5, waveSize.width, waveSize.height);
self.frame = frame;;
}
- (CGFloat)animationBeginRadius{
return waveSize.width*0.5*0.2;
}
- (CGFloat)animationEndRadius{
return waveSize.width*0.5;
}
- (CGPoint)pathCenter{
return CGPointMake(waveSize.width*0.5, waveSize.height*0.5);
}
#pragma mark - CAAnimationDelegate
- (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag{
if (flag) {
[self removeFromSuperview];
}
}
@end
- 大家也可以DIY我的代码,做出很多其他的效果,比如改成其他的波纹颜色。
作者:星星Y灬
链接:https://www.jianshu.com/p/35e6f53ca0fe
來源:简书
简书著作权归作者所有,任何形式的转载都请联系作者获得授权并注明出处。
iOS上手指点击波纹效果的实现的更多相关文章
- Android Material适配 为控件设置指定背景色和点击波纹效果
Android Material适配 为控件设置指定背景色和点击波纹效果,有需要的朋友可以参考下. 大部分时候,我们都需要为控件设置指定背景色和点击效果 4.x以下可以使用selector,5.0以上 ...
- iOS tabbar点击动画效果实现
正常情况下,我们点击tabbar都只有一个变色效果,但有时候,如果我们想给它添加一个点击动画,该如何做呢? 先上几个效果图: 1.先放大,再缩小 2.Z轴旋转 3.Y轴位移 ...
- ios点击产生波纹效果
ios点击产生波纹效果 by 伍雪颖 - (void)viewDidLoad { [super viewDidLoad]; RippleView = [[UIView alloc] initWithF ...
- Android特效专辑(十)——点击水波纹效果实现,逻辑清晰实现简单
Android特效专辑(十)--点击水波纹效果实现,逻辑清晰实现简单 这次做的东西呢,和上篇有点类似,就是用比较简单的逻辑思路去实现一些比较好玩的特效,最近也是比较忙,所以博客更新的速度还得看时间去推 ...
- Android点击Button水波纹效果
先上图,看看接下来我要向大家介绍的是个什么东西,例如以下图: 接下来要介绍的就是怎样实现上述图中的波纹效果.这样的效果假设大家没有体验过的话,能够看看百度手机卫士或者360手机卫士,里面的按钮点击效果 ...
- iOS UIButton加在window上点击无效果问题
UIButton加在window上,点击没有效果,找了很久,原来是没有加上这名:[self.window makeKeyAndVisible]; self.window = [[UIWindow al ...
- android 点击水波纹效果
这里是重点,<ripple>是API21才有的新Tag,正是实现水波纹效果的; 其中<ripple android:color="#FF21272B" .... ...
- iOS动画-扩散波纹效果
最终效果 实现思路 动画的表现形式是颜色以及大小的变化,整体效果可以看做多个单独的波纹效果的叠加.因此我们可以创建多个CALayer,分别赋予CABasicAnimation动画,组成最终的动画效果. ...
- jquery ripples水波纹效果( 涟漪效果)
这个效果是我从bootstrap-material-design上面分离下来的,bootstrap-material-design的一些组件样式我不太不喜欢,但是非常喜欢这个水波纹效果,所以就有了这篇 ...
随机推荐
- lambda表达式2
1.lambda表达式 Java8最值得学习的特性就是Lambda表达式和Stream API,如果有python或者javascript的语言基础,对理解Lambda表达式有很大帮助,因为Java正 ...
- HDU ACM 1690 Bus System (SPFA)
Bus System Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total ...
- Python 3 iter函数用法简述
Python 3中关于iter(object[, sentinel)]方法有两个参数. 使用iter(object)这种形式比较常见. iter(object, sentinel)这种形式一般较少使用 ...
- 第10章 嵌入式Linux 的调试技术
10.1 打印内核调试信息:printk printk位函数运行在内核空间, printf函数运行在用户空间.也就是说,像Linux 驱动这样的Linux内核程序只能使用printk函数输出调试信息 ...
- memcache缓存雪崩、缓存无底洞、缓存穿透、永久数据被踢现象
一.缓存雪崩现象 缓存雪崩一般是由某个缓存节点失效,导致其他节点的缓存命中率下降, 缓存中缺失的数据去数据库查询,短时间内造成数据库服务器崩溃, 重启DB短期又被压跨,但新数据的缓存也更新一些,DB反 ...
- LeetCode算法题-Excel Sheet Column Title(Java实现)
这是悦乐书的第180次更新,第182篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第39题(顺位题号是168).给定正整数,返回Excel工作表中显示的相应列标题.例如: ...
- IntelliJ IDEA 导入Spring源码
第一步: 使用git 拉取代码 git 命令: git init //创建git仓库 git clone https://github.com/spring-projects/spring-f ...
- Glyphicons 字体图标
- Spring缓存注解@Cacheable、@CacheEvict、@CachePut使用
从3.1开始,Spring引入了对Cache的支持.其使用方法和原理都类似于Spring对事务管理的支持.Spring Cache是作用在方法上的,其核心思想是这样的:当我们在调用一个缓存方法时会把该 ...
- 05.Python网络爬虫之三种数据解析方式
引入 回顾requests实现数据爬取的流程 指定url 基于requests模块发起请求 获取响应对象中的数据 进行持久化存储 其实,在上述流程中还需要较为重要的一步,就是在持久化存储之前需要进行指 ...