https://www.jianshu.com/p/35e6f53ca0fe

2016.10.19 22:00* 字数 135 阅读 2468评论 2喜欢 7

闲暇时间做了一个反馈手指点击屏幕的效果,用到了CAShapeLayer和基本的动画知识,模拟器上效果如下:

 
fingerWave.gif
  • 这种效果使用在某些页面上肯定会给用户更有趣的体验,特别是面向儿童的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上手指点击波纹效果的实现的更多相关文章

  1. Android Material适配 为控件设置指定背景色和点击波纹效果

    Android Material适配 为控件设置指定背景色和点击波纹效果,有需要的朋友可以参考下. 大部分时候,我们都需要为控件设置指定背景色和点击效果 4.x以下可以使用selector,5.0以上 ...

  2. iOS tabbar点击动画效果实现

    正常情况下,我们点击tabbar都只有一个变色效果,但有时候,如果我们想给它添加一个点击动画,该如何做呢? 先上几个效果图: 1.先放大,再缩小 2.Z轴旋转               3.Y轴位移 ...

  3. ios点击产生波纹效果

    ios点击产生波纹效果 by 伍雪颖 - (void)viewDidLoad { [super viewDidLoad]; RippleView = [[UIView alloc] initWithF ...

  4. Android特效专辑(十)——点击水波纹效果实现,逻辑清晰实现简单

    Android特效专辑(十)--点击水波纹效果实现,逻辑清晰实现简单 这次做的东西呢,和上篇有点类似,就是用比较简单的逻辑思路去实现一些比较好玩的特效,最近也是比较忙,所以博客更新的速度还得看时间去推 ...

  5. Android点击Button水波纹效果

    先上图,看看接下来我要向大家介绍的是个什么东西,例如以下图: 接下来要介绍的就是怎样实现上述图中的波纹效果.这样的效果假设大家没有体验过的话,能够看看百度手机卫士或者360手机卫士,里面的按钮点击效果 ...

  6. iOS UIButton加在window上点击无效果问题

    UIButton加在window上,点击没有效果,找了很久,原来是没有加上这名:[self.window makeKeyAndVisible]; self.window = [[UIWindow al ...

  7. android 点击水波纹效果

    这里是重点,<ripple>是API21才有的新Tag,正是实现水波纹效果的; 其中<ripple android:color="#FF21272B" .... ...

  8. iOS动画-扩散波纹效果

    最终效果 实现思路 动画的表现形式是颜色以及大小的变化,整体效果可以看做多个单独的波纹效果的叠加.因此我们可以创建多个CALayer,分别赋予CABasicAnimation动画,组成最终的动画效果. ...

  9. jquery ripples水波纹效果( 涟漪效果)

    这个效果是我从bootstrap-material-design上面分离下来的,bootstrap-material-design的一些组件样式我不太不喜欢,但是非常喜欢这个水波纹效果,所以就有了这篇 ...

随机推荐

  1. C# Modbus协议中读取浮点数的操作方法

    输入参数P1,P2代表PLC中浮点数储存的两个寄存器获取的数据 public static float GetFloat(ushort P1, ushort P2) { int intSign, in ...

  2. Android长时间定时任务实现

    在服务的onStartCommand方法里面使用AlarmManager 定时唤醒发送广播,在广播里面启动服务 每次执行startService方法启动服务都会执行onStartCommand 1.服 ...

  3. js在前端json字符串和对象互相转化

    js在前端json字符串和对象互相转化 //对象转json串 注意:参数必须是对象,数组不起作用,对象格式{'0'=>'a'} JSON.stringify( arr ); //json字符串转 ...

  4. python + MySql 基本操作

    python + mysql数据库的链接 1.安装mysql pip install PySQLdb 2.连接数据库 # -*- coding: UTF- -*- import MySQLdb # 打 ...

  5. C# -- 随机数产生的字母金字塔

    C# -- 随机数产生的字母金字塔 1. 代码实现: static void Main(string[] args) { showNpoint(); Console.ReadKey(); } priv ...

  6. 06.Python网络爬虫之requests模块(2)

    今日内容 session处理cookie proxies参数设置请求代理ip 基于线程池的数据爬取 知识点回顾 xpath的解析流程 bs4的解析流程 常用xpath表达式 常用bs4解析方法 引入 ...

  7. ES5-ES6-ES7_Symbol数据类型

    Symbol数据类型简介 ES6 引入了一种新的原始数据类型Symbol,表示独一无二的值.它是 JavaScript 语言的第七种数据类型,前六种是:undefined.null.布尔值(Boole ...

  8. 在Intellij IDEA中使用Maven的方式将项目导出为jar包

    前言:由于项目使用maven管理方式,所以在未发布版本的时候,就需要将项目打成jar包,供本地调试使用.注意在使用本地jar包的时候,需要将pom文件中相关jar包的依赖屏蔽,再将jar包加入项目中. ...

  9. 设计模式のBridgePattern(桥接模式)----结构模式

    一.产生背景 这里以电视遥控器的一个例子来引出桥接模式解决的问题,首先,我们每个牌子的电视机都有一个遥控器,此时我们能想到的一个设计是——把遥控器做为一个抽象类,抽象类中提供遥控器的所有实现,其他具体 ...

  10. centos7+nginx负载均衡Tomcat服务

    接着上一篇:www.cnblogs.com/lkun/p/8252815.html 我们在上一篇在一台centos7服务器上部署了两个nginx,接下来我们使用一个nginx实现tomcat的负载均衡 ...