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/c++ 字节对齐

    c 字节对齐 概念: 结构体里会包括各种类型的成员,比如int char long等等,它们要占用的空间不同,系统为一个结构体开辟内存空间时,会有2种选择. 第一种:节省空间的方案,以上面的列子来说的 ...

  2. 通过jquery隐藏和显示元素

    通过jquery隐藏和显示元素 调用jquery本身提供的函数 $("xxx").hide();//隐藏xxx $("xxx").show();//显示xxx ...

  3. 修改Windows默认远程端口号

    1.定位注册表,[HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Terminal Server\Wds\rdpwd\Tds\tcp],右侧修改 ...

  4. DVWA v1.9 新手指南

    DVWA简介 DVWA(Damn Vulnerable Web Application)是一个用来进行安全脆弱性鉴定的PHP/MySQL Web应用,旨在为安全专业人员测试自己的专业技能和工具提供合法 ...

  5. February 13th, 2018 Week 7th Tuesday

    You are your greatest asset. 你就是你自己最大的资本. For most of us, there are few things that we can count on ...

  6. [福大软工] Z班 第6次成绩排行榜

    作业要求 http://www.cnblogs.com/easteast/p/7668890.html 作业评分 本次作业从引言(5 ') . 用户场景(15 ').类图(10 ').界面原型(15 ...

  7. 常见C语言内存错误

    前言 C语言强大的原因之一在于几乎能掌控所有的细节,包括对内存的处理,什么时候使用内存,使用了多少内存,什么时候该释放内存,这都在程序员的掌控之中.而不像Java中,程序员是不需要花太多精力去处理垃圾 ...

  8. 删除排序链表中的重复元素的golang实现

    给定一个排序链表,删除所有重复的元素,使得每个元素只出现一次. 输入: ->-> 输出: -> 输入: ->->->-> 输出: ->-> 我们先 ...

  9. 12.scrapy框架之递归解析和post请求

    今日概要 递归爬取解析多页页面数据 scrapy核心组件工作流程 scrapy的post请求发送 今日详情 1.递归爬取解析多页页面数据 - 需求:将糗事百科所有页码的作者和段子内容数据进行爬取切持久 ...

  10. php面试题整理(三)

    判断是不是ie浏览器 1,1