#import "DDJelloView.h"

#define SYS_DEVICE_WIDTH    ([[UIScreen mainScreen] bounds].size.width)                  // 屏幕宽度

#define SYS_DEVICE_HEIGHT   ([[UIScreen mainScreen] bounds].size.height)                 // 屏幕长度

#define MIN_HEIGHT          50                                                          // 图形最小高度

@interface DDJelloView ()

@property (nonatomic, assign) CGFloat mHeight;

@property (nonatomic, assign) CGFloat curveX;               // r5点x坐标

@property (nonatomic, assign) CGFloat curveY;               // r5点y坐标

@property (nonatomic, strong) UIView *curveView;            // r5红点

@property (nonatomic, strong) CAShapeLayer *shapeLayer;

@property (nonatomic, strong) CADisplayLink *displayLink;

@property (nonatomic, assign) BOOL isAnimating;

@end

@implementation DDJelloView

static NSString *kX = @"curveX";

static NSString *kY = @"curveY";

- (instancetype)initWithFrame:(CGRect)frame

{

self = [super initWithFrame:frame];

if(self)

{

[self addObserver:self forKeyPath:kX options:NSKeyValueObservingOptionNew context:nil];

[self addObserver:self forKeyPath:kY options:NSKeyValueObservingOptionNew context:nil];

[self configShapeLayer];

[self configCurveView];

[self configAction];

}

return self;

}

- (void)dealloc {

[self removeObserver:self forKeyPath:kX];

[self removeObserver:self forKeyPath:kY];

}

- (void)drawRect:(CGRect)rect

{

}

-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSString *,id> *)change context:(void *)context {

if ([keyPath isEqualToString:kX] || [keyPath isEqualToString:kY]) {

[self updateShapeLayerPath];

}

}

#pragma mark -

#pragma mark - Configuration

- (void)configAction

{

_mHeight = 100;                       // 手势移动时相对高度

_isAnimating = NO;                    // 是否处于动效状态

// 手势

UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handlePanAction:)];

self.userInteractionEnabled = YES;

[self addGestureRecognizer:pan];

// CADisplayLink默认每秒运行60次calculatePath是算出在运行期间_curveView的坐标,从而确定_shapeLayer的形状

_displayLink = [CADisplayLink displayLinkWithTarget:self selector:@selector(calculatePath)];

[_displayLink addToRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];

_displayLink.paused = YES;

}

- (void)configShapeLayer

{

_shapeLayer = [CAShapeLayer layer];

//    _shapeLayer.fillColor = [UIColor colorWithRed:57/255.0 green:67/255.0 blue:89/255.0 alpha:1.0].CGColor;

_shapeLayer.fillColor = [UIColor redColor].CGColor;

[self.layer addSublayer:_shapeLayer];

}

- (void)configCurveView

{

// _curveView就是r5点

self.curveX = SYS_DEVICE_WIDTH/2.0;       // r5点x坐标

self.curveY = MIN_HEIGHT;                 // r5点y坐标

_curveView = [[UIView alloc] initWithFrame:CGRectMake(_curveX, _curveY, 3, 3)];

_curveView.backgroundColor = [UIColor redColor];

[self addSubview:_curveView];

}

#pragma mark -

#pragma mark - Action

- (void)handlePanAction:(UIPanGestureRecognizer *)pan

{

if(!_isAnimating)

{

if(pan.state == UIGestureRecognizerStateChanged)

{

// 手势移动时,_shapeLayer跟着手势向下扩大区域

CGPoint point = [pan translationInView:self];

// 这部分代码使r5红点跟着手势走

_mHeight = point.y*0.7 + MIN_HEIGHT;

self.curveX = SYS_DEVICE_WIDTH/2.0 + point.x;

self.curveY = _mHeight > MIN_HEIGHT ? _mHeight : MIN_HEIGHT;

_curveView.frame = CGRectMake(_curveX,

_curveY,

_curveView.frame.size.width,

_curveView.frame.size.height);

}

else if (pan.state == UIGestureRecognizerStateCancelled ||

pan.state == UIGestureRecognizerStateEnded ||

pan.state == UIGestureRecognizerStateFailed)

{

// 手势结束时,_shapeLayer返回原状并产生弹簧动效

_isAnimating = YES;

_displayLink.paused = NO;           //开启displaylink,会执行方法calculatePath.

// 弹簧动效

[UIView animateWithDuration:1.0

delay:0.0

usingSpringWithDamping:0.5

initialSpringVelocity:0

options:UIViewAnimationOptionCurveEaseInOut

animations:^{

// 曲线点(r5点)是一个view.所以在block中有弹簧效果.然后根据他的动效路径,在calculatePath中计算弹性图形的形状

_curveView.frame = CGRectMake(SYS_DEVICE_WIDTH/2.0, MIN_HEIGHT, 3, 3);

} completion:^(BOOL finished) {

if(finished)

{

_displayLink.paused = YES;

_isAnimating = NO;

}

}];

}

}

}

- (void)updateShapeLayerPath

{

// 更新_shapeLayer形状

UIBezierPath *tPath = [UIBezierPath bezierPath];

[tPath moveToPoint:CGPointMake(0, 0)];                              // r1点

[tPath addLineToPoint:CGPointMake(SYS_DEVICE_WIDTH, 0)];            // r2点

[tPath addLineToPoint:CGPointMake(SYS_DEVICE_WIDTH,  MIN_HEIGHT)];  // r4点

[tPath addQuadCurveToPoint:CGPointMake(0, MIN_HEIGHT)

controlPoint:CGPointMake(_curveX, _curveY)]; // r3,r4,r5确定的一个弧线

[tPath closePath];

_shapeLayer.path = tPath.CGPath;

}

- (void)calculatePath

{

// 由于手势结束时,r5执行了一个UIView的弹簧动画,把这个过程的坐标记录下来,并相应的画出_shapeLayer形状

CALayer *layer = _curveView.layer.presentationLayer;

self.curveX = layer.position.x;

self.curveY = layer.position.y;

}

@end

iOS开发 弹簧效果的更多相关文章

  1. ios开发抽屉效果的封装使用

    #import "DragerViewController.h" #define screenW [UIScreen mainScreen].bounds.size.width @ ...

  2. iOS开发——UI篇&提示效果

    提示效果 关于iOS开发提示效果是一个很常见的技术,比如我们平时点击一个按钮,实现回馈,或者发送网络请求的时候! 技术点: 一:View UIAlertView UIActionSheet 二:控制器 ...

  3. iOS开发之各种动画各种页面切面效果

    因工作原因,有段时间没发表博客了,今天就发表篇博客给大家带来一些干货,切勿错过哦.今天所介绍的主题是关于动画的,在之前的博客中也有用到动画的地方,今天就好好的总结一下iOS开发中常用的动画.说道动画其 ...

  4. iOS开发 QQ粘性动画效果

    QQ(iOS)客户端的粘性动画效果 时间 2016-02-17 16:50:00  博客园精华区 原文  http://www.cnblogs.com/ziyi--caolu/p/5195615.ht ...

  5. iOS开发笔记7:Text、UI交互细节、两个动画效果等

    Text主要总结UILabel.UITextField.UITextView.UIMenuController以及UIWebView/WKWebView相关的一些问题. UI细节主要总结界面交互开发中 ...

  6. ios开发中超简单抽屉效果(MMDrawerController)的实现

    ios开发中,展示类应用通常要用到抽屉效果,由于项目需要,本人找到一个demo,缩减掉一些不常用的功能,整理出一个较短的实例. 首先需要给工程添加第三方类库 MMDrawerController: 这 ...

  7. 【转】iOS开发之各种动画各种页面切面效果

    原文: http://www.cnblogs.com/ludashi/p/4160208.html?utm_source=tuicool 因工作原因,有段时间没发表博客了,今天就发表篇博客给大家带来一 ...

  8. iOS开发之虾米音乐频道选择切换效果分析与实现

    今天博客的内容比较简单,就是看一下虾米音乐首页中频道选择的一个动画效果的实现.之前用mask写过另外一种Tab切换的一种效果,网易云音乐里边的一种Tab切换效果,详情请移步于"视错觉:从一个 ...

  9. iOS开发探索-高斯模糊&毛玻璃效果

    iOS开发中有的时候需要将图片设置模糊,来实现特定的效果获取更好的用户体验, iOS7之后半透明模糊效果得到大范围使用的比较大,现在也可以看到很多应用局部用到了图片模糊效果,可以通过高斯模糊和毛玻璃效 ...

随机推荐

  1. javascript中的真假值、数据类型判断以及+的特殊用法

    一.javascript中的假值 jQuery中拥有一组数量奇大的假值,包括 0,NaN(非数),''(空字符串),false,null,undefined 这些值在if判断中全部等于假,但这些值彼此 ...

  2. PinPhoto On OS X

    把多张图片摆放到屏幕任何位置并保存成文件,下次打开时恢复之前的状态! 一般使用场景: 经常要看的图片,比如说软件快捷键.库的API.英文生词.常用配色等等摆好在屏幕上以便查看和记忆. 用PS等画画.设 ...

  3. ASP.NET 文件后缀名详解

    sln:解决方案文件,为解决方案资源管理器提供显示管理文件的图形接口所需的信息. .csproj:项目文件,创建应用程序所需的引用.数据连接.文件夹和文件的信息. .aspx:Web 窗体页由两部分组 ...

  4. Leetcode: Non-overlapping Intervals

    Given a collection of intervals, find the minimum number of intervals you need to remove to make the ...

  5. GIt的命令

    Git 命令 1,git init初始化当前文件夹为git仓库的根目录 2.git commit提交到本地仓库 3.git push origin master 提交到服务器 4.git log 查看 ...

  6. java权限修饰符

  7. ASP.NET中的Image和ImageButton控件

    Image 控件用来显示图形.Image 控件可以显示来自位图.图标或元文件的图形,也可以显示增强的元文件.JPEG 或 GIF文件. ImageButton 控件用于显示可点击的图像. Image ...

  8. 【001:ubuntu下搭建ESP8266开发环境--编辑 编译 下载】

    系统环境:ubuntu 16.04 TLS 64BIT 编辑器: Eclipse CDT 版本 编译器:xtensa-lx106-elf 交叉编译工具链 下载工具:esptool.py pyseria ...

  9. CoordinatorLayout 自定义Behavior并不难,由简到难手把手带你飞

    先来看看最终的效果~~ 本文同步至博主的私人博客wing的地方酒馆 嗯..一个是头像上移的 另一个是模仿UC浏览器的. (PД`q.)你不是说!有三款的吗,怎么只有两款!!!! 不要急嘛... 说了从 ...

  10. Java虚拟机学习(2):垃圾收集算法

    跟踪收集器 跟踪收集器采用的为集中式的管理方式,全局记录对象之间的引用状态,执行时从一些列GC  Roots的对象做为起点,从这些节点向下开始进行搜索所有的引用链,当一个对象到GC  Roots 没有 ...