UIBezierPath先来对付它

4.gif
1.gif

2.gif

3.gif

4

先上图,这就是所谓的贝尔曲线,公式我不会,我也不懂,感觉大学白学了
果冻参考链接

先来一个简单一点的果冻效果:

4.gif

代码没有什么难度,所以注释比较少,来直接上代码

//
//  PullCircleView.m
//  MyTest
//
//  Created by 丁祥 on 2017/3/24.
//  Copyright © 2017年 wonders. All rights reserved.
//

#import "PullCircleView.h"
#define boundSize 150
@interface CircleView : UIView

@end

@interface PullCircleView()
@property (nonatomic,strong)CAShapeLayer *shapeLayer;
@property (nonatomic,assign)double r1;
@property (nonatomic,assign)double r2;
@property (nonatomic,assign)double x1;
@property (nonatomic,assign)double x2;
@property (nonatomic,assign)double y1;
@property (nonatomic,assign)double y2;
@property (nonatomic,assign)double d;
@property (nonatomic,assign)CGPoint pointA;
@property (nonatomic,assign)CGPoint pointB;
@property (nonatomic,assign)CGPoint pointC;
@property (nonatomic,assign)CGPoint pointD;
@property (nonatomic,assign)CGPoint pointM;
@property (nonatomic,assign)CGPoint pointN;

@property (nonatomic,strong)CADisplayLink *displayLink;
@property (nonatomic,strong)CircleView *circleViewOne;//为了做出弹簧效果
@property (nonatomic,strong)CircleView *circleViewTwo;//为了做出弹簧效果
@property (nonatomic,assign)BOOL state;//点断以后

@end
@implementation PullCircleView

- (instancetype)initWithFrame:(CGRect)frame
{
    
    self =[super initWithFrame:frame];
    if (self) {
        
        _shapeLayer =[CAShapeLayer layer];
        _shapeLayer.fillColor=[UIColor redColor].CGColor;
        [self.layer addSublayer:_shapeLayer];
        [self loadData];
        [self addGesture];
        
        _circleViewOne =[[CircleView alloc] initWithFrame:CGRectMake(0, 0, 2*_r1, 2*_r1)];
        
        _circleViewOne.center =CGPointMake(_x1, _y1);
        
        _circleViewTwo =[[CircleView alloc] initWithFrame:CGRectMake(0, 0, 2*_r2, 2*_r2)];
        
        _circleViewTwo.center =CGPointMake(_x2, _y2);
        
        _state =YES;
        
        [self addSubview:_circleViewOne];
        [self addSubview:_circleViewTwo];
        
    }
    
    return self;

}

- (void)addGesture
{
    _displayLink =[CADisplayLink displayLinkWithTarget:self selector:@selector(calcauLayer)];
    [_displayLink addToRunLoop:[NSRunLoop mainRunLoop] forMode:NSDefaultRunLoopMode];
    _displayLink.paused =YES;
    
    UIPanGestureRecognizer *pan =[[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(pullLayer:)];
    [self addGestureRecognizer:pan];


}

- (void)calcauLayer
{
    CALayer *layer =_circleViewTwo.layer.presentationLayer;
    _x2 =layer.position.x;
    _y2 =layer.position.y;
    
    CGFloat scale = (1 - [self getDistance]/boundSize);
    _circleViewOne.transform = CGAffineTransformMakeScale(scale, scale);
    [self updateLayer];
}

- (void)updateLayer
{
    
    [self setNeedsDisplay];

}

- (void)pullLayer:(UIPanGestureRecognizer *)pangesture
{
    if (pangesture.state == UIGestureRecognizerStateChanged){
        
        CGPoint point =[pangesture locationInView:self];
        _x2 =point.x;
        _y2 =point.y;
        _circleViewTwo.center =CGPointMake(_x2, _y2);
        
        if ([self getDistance]<boundSize) {
            
        CGFloat scale = (1 - [self getDistance]/boundSize);
        scale = MAX(0.25, scale);
        _circleViewOne.transform = CGAffineTransformMakeScale(scale, scale);
        [self updateLayer];

        }else{
            
            self.shapeLayer.path =nil;
            [UIView animateWithDuration:0.5 delay:0 usingSpringWithDamping:0.3 initialSpringVelocity:0 options:UIViewAnimationOptionCurveEaseInOut animations:^{
                
                _circleViewOne.transform =CGAffineTransformMakeScale(1, 1);
                
            } completion:^(BOOL finished) {
                
                _state =NO;
                
                
            }];
        
        }
        
    
    }else if (pangesture.state == UIGestureRecognizerStateEnded){
        
        if (_state) {
            
            _displayLink.paused =NO;

        }
        [UIView animateWithDuration:2 delay:0 usingSpringWithDamping:0.5 initialSpringVelocity:0 options:UIViewAnimationOptionCurveEaseInOut animations:^{
            
            _x2 =_x1;
            _y2 =_y1;
            _circleViewTwo.center =CGPointMake(_x2, _y2);
            
        } completion:^(BOOL finished) {
            
            if (finished) {
                
                _displayLink.paused =YES;
                
                _state =YES;
            }
            
        }];
    }
    


}

- (void)drawRect:(CGRect)rect
{
    
    double d =[self getDistance];
    double sin =[self getSin];
    double cos =[self getCos];
    _r1 =_circleViewOne.frame.size.width/2;
    _r2 =_circleViewTwo.frame.size.width/2;
    
    _pointA =CGPointMake(_x1-_r1*cos, _y1 +_r1*sin);
    _pointB =CGPointMake(_x1+_r1*cos, _y1 -_r1*sin);
    _pointC =CGPointMake(_x2+_r2*cos, _y2 -_r2*sin);
    _pointD =CGPointMake(_x2-_r2*cos, _y2 +_r2*sin);
    _pointM = CGPointMake(_pointA.x+(d / 2) * sin, _pointA.y + (d / 2) * cos);
    _pointN = CGPointMake(_pointB.x + (d / 2) * sin, _pointB.y+ (d / 2) * cos);
    
    UIBezierPath *path =[UIBezierPath bezierPath];

    [path moveToPoint:_pointA];
    [path addLineToPoint:_pointB];
    [path addQuadCurveToPoint:_pointC controlPoint:_pointN];
    [path addLineToPoint:_pointD];
    [path addQuadCurveToPoint:_pointA controlPoint:_pointM];
    self.shapeLayer.path =path.CGPath;

}

- (void)loadData
{
    
    _x1 =200;
    _y1 =200;
    _x2 =200;
    _y2 =200;
    _r1 =15;
    _r2 =20;

}

- (double)getDistance
{
    
    double d  =(_x2 -_x1)*(_x2 -_x1) +(_y2 -_y1)*(_y2 -_y1);
    
    _d =sqrtf(d);

    return _d;

}

- (double)getSin
{
    
    double d =[self getDistance];
    if (d ==0) {
        return 0;
    }
    
    return (_x2 -_x1)/d;
}

- (double)getCos
{
    
    double d =[self getDistance];
    
    if (d==0) {
        return 0;
    }
    
    return (_y2 -_y1)/d;
    
}

@end

@implementation CircleView

- (instancetype)initWithFrame:(CGRect)frame
{
    
    self =[super initWithFrame:frame];
    if (self) {
        
        self.clipsToBounds =YES;
        self.layer.cornerRadius =MIN(self.bounds.size.width, self.bounds.size.height)/2.0;
        self.backgroundColor =[UIColor redColor];
        
    }
    
    return self;

}


@end

1.中间用到了CADisplayLink 结合UIBezierPath来去更新drawRect

2.用到了阻尼系数的动画,也就是spring函数

3.对于动画,可以选择spring函数,也可以用keysAnimal这个动画,不过要设置多个关键帧,所以直接选择spring函数比较方便

4.关键就是UIBezierPath的各个点的计算问题,可以根据前三个图,来认识一下UIBezierPath的特性,然后去计算就比较容易

在写的过程我遇到的问题:
1. 断点的计算,开始的圆我都是用path画的后来发现,还有断点问题,又重新写了一下,
2.曲线的曲度,我开始是不改变原点的大小,去改变曲线cotrolPoint的位置,但是形变无法控制,折腾一下午没成功,还是去改变原点的scale比较靠谱

弹出菜单链接

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 194,761评论 5 460
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 81,953评论 2 371
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 141,998评论 0 320
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 52,248评论 1 263
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 61,130评论 4 356
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 46,145评论 1 272
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 36,550评论 3 381
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 35,236评论 0 253
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 39,510评论 1 291
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 34,601评论 2 310
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 36,376评论 1 326
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,247评论 3 313
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 37,613评论 3 299
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 28,911评论 0 17
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,191评论 1 250
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 41,532评论 2 342
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 40,739评论 2 335

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 171,078评论 25 707
  • WebSocket-Swift Starscream的使用 WebSocket 是 HTML5 一种新的协议。它实...
    香橙柚子阅读 23,557评论 8 183
  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 11,947评论 4 60
  • 我们整天忙的焦头烂额,却没什么成绩,是为什么呢,就是把一些事情放在了“紧急但不重要上了”每天被一些无关紧要的八卦事...
    朱村阅读 125评论 0 1
  • “到老学得乌龟爬,能缩头来且缩头……”这句话时常会想起,都这么多年了,没有刻意去记忆,却可以深深地烙在心坎里。 爷...
    澄默时节阅读 208评论 0 0