沿着path路径做动画

简介:

沿着path路径做动画

 

路径

 

效果

 

源码




//
//  ViewController.m
//  PathAnimation
//
//  Created by YouXianMing on 16/1/26.
//  Copyright © 2016年 YouXianMing. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()

@property (nonatomic) CGPoint startPoint;
@property (nonatomic) CGPoint endPoint;

@end

@implementation ViewController

- (void)viewDidLoad {
    
    [super viewDidLoad];
    
    // 起始点 结束点
    self.startPoint = CGPointMake(0, 150);
    self.endPoint   = CGPointMake(160, 150);

    // 初始化view
    UIButton *redButton       = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 10, 10)];
    redButton.center          = self.startPoint;
    redButton.backgroundColor = [UIColor redColor];
    [redButton addTarget:self action:@selector(buttonEvent) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:redButton];
    
    // CAKeyframeAnimation
    CAKeyframeAnimation *animation = [CAKeyframeAnimation animationWithKeyPath:@"position"];
    animation.path                 = [self path].CGPath;
    animation.duration             = 2.f;
    animation.timingFunction       = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
    
    redButton.center               = self.endPoint;
    [redButton.layer addAnimation:animation forKey:nil];
}

- (UIBezierPath *)path {

    UIBezierPath* bezierPath = [UIBezierPath bezierPath];
    
    [bezierPath moveToPoint:    self.startPoint];
    [bezierPath addLineToPoint: CGPointMake(68, 150)];
    [bezierPath addLineToPoint: CGPointMake(83, 107)];
    [bezierPath addLineToPoint: CGPointMake(96, 206)];
    [bezierPath addLineToPoint: CGPointMake(102, 150)];
    [bezierPath addLineToPoint: CGPointMake(116, 150)];
    [bezierPath addLineToPoint: CGPointMake(127, 82)];
    [bezierPath addLineToPoint: CGPointMake(143, 213)];
    [bezierPath addLineToPoint: self.endPoint];

    return bezierPath;
}

- (void)buttonEvent {
    
    // todo
}

@end

细节

 

效果

 

源码


 

//
//  ViewController.m
//  PathAnimation
//
//  Created by YouXianMing on 16/1/26.
//  Copyright © 2016年 YouXianMing. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()

@property (nonatomic) CGPoint startPoint;
@property (nonatomic) CGPoint endPoint;

@end

@implementation ViewController

- (void)viewDidLoad {
    
    [super viewDidLoad];
    
    // 起始点 结束点
    self.startPoint = CGPointMake(0, 150);
    self.endPoint   = CGPointMake(160, 150);

    // 初始化view
    UIButton *redButton       = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 10, 10)];
    redButton.center          = self.startPoint;
    redButton.backgroundColor = [UIColor redColor];
    [redButton addTarget:self action:@selector(buttonEvent) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:redButton];
    
    // CAKeyframeAnimation
    CAKeyframeAnimation *animation = [CAKeyframeAnimation animationWithKeyPath:@"position"];
    animation.path                 = [self path].CGPath;
    animation.duration             = 2.f;
    animation.autoreverses         = true;
    animation.repeatCount          = CGFLOAT_MAX;
    animation.timingFunction       = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
    
    [redButton.layer addAnimation:animation forKey:nil];
}

- (UIBezierPath *)path {

    UIBezierPath* bezierPath = [UIBezierPath bezierPath];
    
    [bezierPath moveToPoint:    self.startPoint];
    [bezierPath addLineToPoint: CGPointMake(68, 150)];
    [bezierPath addLineToPoint: CGPointMake(83, 107)];
    [bezierPath addLineToPoint: CGPointMake(96, 206)];
    [bezierPath addLineToPoint: CGPointMake(102, 150)];
    [bezierPath addLineToPoint: CGPointMake(116, 150)];
    [bezierPath addLineToPoint: CGPointMake(127, 82)];
    [bezierPath addLineToPoint: CGPointMake(143, 213)];
    [bezierPath addLineToPoint: self.endPoint];

    return bezierPath;
}

- (void)buttonEvent {
    
    // todo
}

@end

细节



目录
相关文章
|
3月前
|
前端开发
css动画 —— 自定义不规则的动画路径 offset-path (含不规则的动画路径参数获取方法)
css动画 —— 自定义不规则的动画路径 offset-path (含不规则的动画路径参数获取方法)
87 1
|
3月前
Flutter 滚动距离来设置TabBar的位置,点击TabBar滚动的到指定的位置
Flutter 滚动距离来设置TabBar的位置,点击TabBar滚动的到指定的位置
|
存储 图形学
|
人工智能 前端开发 JavaScript
canvas 中如何实现自定义路径动画
前言 大家好!!又到周末了,最近项目忙完了,有时间写文章了。之前有粉丝问我, fly哥怎么实现自定义路径动画, 当时给他说的就是路径无非不就是直线 或者曲线。也就这两种, 直线的话 可以用直线方程, 曲线的话稍微复杂点 ,需要用贝塞尔曲线去做lerp。也就是动画的每一幁的算出路径的对应的坐标就可以了。但是这套方案学习成本太高了, 有没有一种更加简单的方式呢?本篇文章大概花费你5分钟, 你可以学到什么呢 svg 的 两个无敌api 后面介绍 封装了一个自定义路径动画函数 创建Path 制作动画前,先要拿到动画的路径,对此我们可以直接使用svg的path定义规则,比如我们定义了一条较为复杂的路径
canvas 中如何实现自定义路径动画
|
JavaScript
js 实现上下拖动改变父 div 的高度,左右上下拖动动态分割孩子的宽高
js 实现上下拖动改变父 div 的高度,左右上下拖动动态分割孩子的宽高
542 0
js 实现上下拖动改变父 div 的高度,左右上下拖动动态分割孩子的宽高
|
监控 数据可视化
三维场景中常用的路径动画
三维场景中常用的路径动画
三维场景中常用的路径动画
|
前端开发 C# 算法
WPF路径动画(动态逆向动画)
原文:WPF路径动画(动态逆向动画) WPF 中的Path.Data 不再多介绍,M开始坐标点 C弧度坐标点 L 直线坐标点   个人写了关于Path.Data数据反向,意思就是把Path的数据逆转,但是图形是没有变化的 Xaml代码如下: ...
1978 0
|
关系型数据库
使用path制作各类型动画路径
原文:使用path制作各类型动画路径 ...
826 0
|
前端开发 人工智能 JavaScript
SVG路径动画解密
原文:SVG路径动画解密 原文链接:http://www.gbtags.com/gb/share/5581.
981 0
|
C# 前端开发
[原]Wpf应用Path路径绘制圆弧
原文:[原]Wpf应用Path路径绘制圆弧 1. 移动指令:Move Command(M):M 起始点  或者:m 起始点比如:M 100,240或m 100,240使用大写M时,表示绝对值; 使用小写m时; 表示相对于前一点的值,如果前一点没有指定,则使用(0,0)。
3000 0