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. Linux CFS调度器之队列操作--Linux进程的管理与调度(二十七)

    1. CFS进程入队和出队 完全公平调度器CFS中有两个函数可用来增删队列的成员:enqueue_task_fair和dequeue_task_fair分别用来向CFS就绪队列中添加或者删除进程 2 ...

  2. Linux核心调度器之周期性调度器scheduler_tick--Linux进程的管理与调度(十八)

    我们前面提到linux有两种方法激活调度器:核心调度器和 周期调度器 一种是直接的, 比如进程打算睡眠或出于其他原因放弃CPU 另一种是通过周期性的机制, 以固定的频率运行, 不时的检测是否有必要 因 ...

  3. c/c++ 标准库 map multimap元素访问

    标准库 map multimap元素访问 一,map,unordered_map下标操作 下标操作种类 功能描述 c[k] 返回关键字为k的元素:如果k不在c中,添加一个关键字为k的元素,并对其初始化 ...

  4. 阿里云ECS Ubuntu16.0 安装 uwsgi 失败解决方案

    Ubuntu安装包时报错 E:Unable to locate package xxx(如:python3-pip) 一般新安装Ubuntu后需要先更新软件源: apt-get update apt- ...

  5. Python 入门:基本语法

    对于多数从其他编程语言转入Python的来说,或多或少会有些不习惯.如果沿用其他编程语言的语法来写Python代码,那么碰壁是不可避免的了. 本文是基于我看了两个小时的官方文档(Python 2.7 ...

  6. Nginx使用教程(七):使用Nginx缓存之proxy cache

    定义缓存目录 <br\>使用您喜欢的文本编辑器打开/etc/nginx/nginx.conf,并在http {区域加入: proxy_cache_path  /var/www/cache ...

  7. centos7下如何隐藏nginx的版本号

    我们在访问nginx的时候会暴露nginx的版本号,如何将这些版本号隐藏呢? 其实隐藏版本号非常简单 在nginx的配置文件中添加一个server——tokens  off:参数就可以了,下面进行操作 ...

  8. ASP.NET API Helper Page 创建并生成相关帮助文档

    创建API项目 修改原工程文件,该行为是为了避免和引入第三方API工程文件冲突 修改发布设置 引入需要生成文档的相关文件,将第三方API依赖的相关文件(XML文件非常重要,是注释显示的关键),复制到文 ...

  9. 字符串相关的hash值(一)

  10. 09 python初学 (字符串)

    # 重复输出字符串 print('hello' * 2) # >>>hellohello # 字符串切片操作,最重要的!!!! print('hello'[2:]) # >&g ...