首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
社区首页 >问答首页 >形状层的动画路径

形状层的动画路径
EN

Stack Overflow用户
提问于 2015-04-30 11:55:37
回答 2查看 3.4K关注 0票数 2

我被我认为是一个简单的问题所困扰。

我想绘制由线连接的视图,动画视图的位置,并使连接线也有动画。我创建视图,并在它们之间创建如下一行:

代码语言:javascript
运行
AI代码解释
复制
- (UIBezierPath *)pathFrom:(CGPoint)pointA to:(CGPoint)pointB {
    CGFloat halfY = pointA.y + 0.5*(pointB.y - pointA.y);
    UIBezierPath *linePath=[UIBezierPath bezierPath];
    [linePath moveToPoint: pointA];
    [linePath addLineToPoint:CGPointMake(pointA.x, halfY)];
    [linePath addLineToPoint:CGPointMake(pointB.x, halfY)];
    [linePath addLineToPoint:pointB];
    return linePath;
}

-(void)makeTheLine {
    CGPoint pointA = self.viewA.center;
    CGPoint pointB = self.viewB.center;

    CAShapeLayer *lineShape = [CAShapeLayer layer];
    UIBezierPath *linePath=[self pathFrom:pointA to:pointB];
    lineShape.path=linePath.CGPath;

    lineShape.fillColor = nil;
    lineShape.opacity = 1.0;
    lineShape.strokeColor = [UIColor blackColor].CGColor;
    [self.view.layer addSublayer:lineShape];
    self.lineShape = lineShape;
}

它画出了我想要的样子。我从文档中了解到,我可以在动画块中修改形状的路径,如下所示:

代码语言:javascript
运行
AI代码解释
复制
- (void)moveViewATo:(CGPoint)dest {
    UIBezierPath *destPath=[self pathFrom:dest to:self.viewB.center];
    [UIView animateWithDuration:1 animations:^{
        self.viewA.center = dest;
        self.lineShape.path = destPath.CGPath;
    }];
}

但没有骰子。视图位置按预期进行动画,但是连接到另一个视图的线会立即跳转到目标路径。

This answer意味着我所做的应该是有效的。this answer建议使用CABasic动画,这在我看来更糟,因为(a)然后我需要与对视图所做的更酷的块动画进行协调,(b)当我尝试这种方式时,线条一点也没有改变.

代码语言:javascript
运行
AI代码解释
复制
// worse way
- (void)moveViewATo:(CGPoint)dest {
    UIBezierPath *linePath=[self pathFrom:dest to:self.viewB.center];
    [UIView animateWithDuration:1 animations:^{
        self.viewA.center = dest;
        //self.lineShape.path = linePath.CGPath;
    }];

    CABasicAnimation *morph = [CABasicAnimation animationWithKeyPath:@"path"];
    morph.duration = 1;
    morph.toValue = (id)linePath.CGPath;
    [self.view.layer addAnimation:morph forKey:nil];
}

提前谢谢。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2015-05-20 08:10:50

谢谢大家的帮助。在我问这个问题之后,我发现我是在绘制错误的属性。结果,您可以在动画中替换图层的形状,如下所示:

代码语言:javascript
运行
AI代码解释
复制
CABasicAnimation *morph = [CABasicAnimation animationWithKeyPath:@"path"];
morph.duration  = 1;
morph.fromValue = (__bridge id)oldPath.path;
morph.toValue   = (__bridge id)newPath.CGPath;
[line addAnimation:morph forKey:@"change line path"];
line.path=linePath.CGPath;
票数 2
EN

Stack Overflow用户

发布于 2015-04-30 13:44:16

我想这就是你所需要的

代码语言:javascript
运行
AI代码解释
复制
    #import "ViewController.h"

    @interface ViewController ()

    //the view to animate, nothing but a simple empty UIView here.
    @property (nonatomic, strong) IBOutlet UIView *targetView;
    @property (nonatomic, strong) CAShapeLayer *shapeLayer;
    @property NSTimeInterval animationDuration;
    @end

    @implementation ViewController

    - (void)viewDidLoad {
        [super viewDidLoad];

        //the shape layer appearance
        self.shapeLayer = [[CAShapeLayer alloc]init];
        self.shapeLayer.strokeColor = [UIColor blackColor].CGColor;
        self.shapeLayer.fillColor = [UIColor clearColor].CGColor;
        self.shapeLayer.opacity = 1.0;
        self.shapeLayer.lineWidth = 2.0;
        [self.view.layer insertSublayer:self.shapeLayer below:self.targetView.layer];

        //animation config
        self.animationDuration = 2;
    }

    - (UIBezierPath *)pathFrom:(CGPoint)pointA to:(CGPoint)pointB {
        CGFloat halfY = pointA.y + 0.5*(pointB.y - pointA.y);
        UIBezierPath *linePath=[UIBezierPath bezierPath];
        [linePath moveToPoint: pointA];
        [linePath addLineToPoint:CGPointMake(pointA.x, halfY)];
        [linePath addLineToPoint:CGPointMake(pointB.x, halfY)];
        [linePath addLineToPoint:pointB];
        return linePath;
    }

    - (void) moveViewTo: (CGPoint) point {
        UIBezierPath *linePath= [self pathFrom:self.targetView.center to:point];

        self.shapeLayer.path = linePath.CGPath;

        //Use CAKeyframeAnimation to animate the view along the path
        //animate the position of targetView.layer instead of the center of targetView
        CAKeyframeAnimation *viewMovingAnimation = [CAKeyframeAnimation animationWithKeyPath:@"position"];
        viewMovingAnimation.duration = self.animationDuration;
        viewMovingAnimation.path = linePath.CGPath;
        //set the calculationMode to kCAAnimationPaced to make the movement in a constant speed
        viewMovingAnimation.calculationMode =kCAAnimationPaced;
        [self.targetView.layer addAnimation:viewMovingAnimation forKey:viewMovingAnimation.keyPath];

        //draw the path, animate the keyPath "strokeEnd"
        CABasicAnimation *lineDrawingAnimation = [CABasicAnimation animationWithKeyPath:@"strokeEnd"];
        lineDrawingAnimation.duration = self.animationDuration;
        lineDrawingAnimation.fromValue = [NSNumber numberWithDouble: 0];
        lineDrawingAnimation.toValue = [NSNumber numberWithDouble: 1];
        [self.shapeLayer addAnimation:lineDrawingAnimation forKey:lineDrawingAnimation.keyPath];


        //This part is crucial, update the values, otherwise it will back to its original state
        self.shapeLayer.strokeEnd = 1.0;
        self.targetView.center = point;
    }

    //the IBAction for a UITapGestureRecognizer
    - (IBAction) viewDidTapped:(id)sender {
        //move the view to the tapped location
        [self moveViewTo:[sender locationInView: self.view]];
    }

    @end

一些解释:

  • 对于UIViewAnimation,当动画完成时,属性值将被更改。对于CALayerAnimation,属性值永远不会改变,它只是一个动画,当动画完成后,该层将进入其原始状态(在本例中为路径)。
  • self.lineShape.path = linePath.CGPath不起作用是因为self.linePathCALayer而不是UIView,所以必须使用CALayerAnimation来动画CALayer
  • 要绘制路径,最好使用keyPath strokeEnd而不是path动画路径绘图。我不知道path为什么在最初的帖子里工作,但我觉得很奇怪。
  • CAKeyframeAnimation (而不是CABasicAnimationUIViewAnimation)用于在路径上动画视图。(我想你更喜欢这个,而不是直接从起点到终点的线性动画)。将calculationMode设置为kCAAnimationPaced将使动画具有恒定的速度,否则视图的移动将与绘制的直线不同步。
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/29976687

复制
相关文章
android flash路径动画,Flash制作沿着路径的动画
上完课后,有些同学因为课上老师讲的太快,还是不会做路径动画。今天为大家介绍Flash制作沿着路径的动画介绍,操作很简单的,一起来学习吧!
全栈程序员站长
2022/08/23
1.8K0
android flash路径动画,Flash制作沿着路径的动画
【OpenXml】Pptx的多路径形状转为WPF的Path
本文是将演示如何解析pptx文件的多路径的形状转换到WPF,绘制多个Shape的Path
ryzenWzd
2021/07/14
4410
【动画进阶】当路径动画遇到滚动驱动!
在一年前,我介绍了 CSS 中非常新奇有趣的一个新特性 -- @scroll-timeline:革命性创新,动画杀手锏 @scroll-timeline。
Sb_Coco
2023/09/15
6300
【动画进阶】当路径动画遇到滚动驱动!
CSS 路径动画工具的诞生
本文介绍了一种CSS路径动画的曲线生成方法,该方法通过将贝塞尔曲线与关键帧动画相结合,实现了在动画过程中沿路径匀速运动的效果。具体来说,该方法包括以下步骤:首先,通过贝塞尔曲线公式构建贝塞尔曲线;然后,将贝塞尔曲线与关键帧动画相结合,通过计算关键帧动画的插值函数,将贝塞尔曲线上的点映射到关键帧动画的插值点上;最后,在动画过程中,根据贝塞尔曲线上的点的位置,计算动画对象在关键帧动画中的位置,以实现沿路径匀速运动的效果。
chiron
2017/09/13
4.1K0
CSS 路径动画工具的诞生
基于图形项的复杂形状和动画--千足虫
在本篇中,会看到利用图形项来绘制复杂形状和动画,模拟“千足虫”的运动和消亡过程。程序有一个内部的定时器,在没段时间间隙中,这些千足虫都在不停的移动。如果它们的头部产生了碰撞,则其头部颜色的红色分量越来越高,直至死亡。
用户6021899
2019/08/14
1.7K0
SVG 路径动画简易指南
任何有开发经验的前端工程师都会考虑到不成体系的设备生态所带来的挑战。设备间不同的屏幕尺寸、分辨率和比例使得产品难以提供一致的体验,对于那些对产品有着像素级完美追求的人这种体验差异尤其显著! SVG(可缩放的矢量图形)完美地解决了上文中提到的部分问题。尽管 SVG 有它的局限性,但是在某些场景下是非常有用的,如果你有一个好的设计团队,你也可以基于SVG创建一些震撼的视觉体验,而不必担心给浏览器带来过重的渲染负担或阻碍页面的加载时间。
疯狂的技术宅
2019/03/27
3.7K0
SVG 路径动画简易指南
探秘神奇的运动路径动画 Motion Path
CSS 中有一个非常有意思的模块 -- CSS Motion Path Module Level 1,翻译过来也就是运动路径。本文将对 motion path 一探究竟,通过本文,你可以了解到:
Sb_Coco
2021/04/30
2K0
探秘神奇的运动路径动画 Motion Path
canvas绘制折线路径动画
要实现以上路径动画,一般可以使用svg的动画功能。或者使用canvas绘制,结合路径数学计算来实现。
用户3158888
2022/03/22
1.6K0
canvas绘制折线路径动画
VueRouter路径切换过渡动画
实现简单的页面切换淡入淡出效果 <template> <div id="app"> <transition name="fade" mode="out-in"> <router-view></router-view> </transition> </div> </template> <script> export default { name: "App" }; </script> <style> #app { margin: 0; padding: 0;
明知山
2022/05/05
9100
Unity3D--Mecanim动画系统(三)-动画层Layers
其实在新版的动画状态机中,大量的技巧已经让一般的小白晕头转向了。而且就目前来说:使用状态机的单一层动画就已经可以满足日常开发需要。那么为什么出现层的概念呢? 先来几张图,对比一下,你就会发现哪里好
孙寅
2020/06/02
9260
微信小程序弹出层动画
<button bindtap="showModal">Click Me</button> <view class="pop" wx:if="{{showPop}}"> <view class="popbg" bindtap="hideModal"></view> <view class="popup" animation="{{animationData}}">内容</view> </view> .popbg { position: fixed; top: 0; left: 0
明知山
2020/09/03
1.9K0
三维场景中常用的路径动画
在三维场景中,除了用逼近真实的模型代表现实中的设备、标识物外,通常还会使用一些动画来表示模型在现实中一些行为和作用。常见的动画比如路径动画、旋转动画、发光动画、流动动画等。本文将为大家介绍几种常用的路径动画。首先,最简单的自然是直线路径动画。
用户3158888
2021/02/22
7890
html遮罩层动画制作,flash简单制作遮罩动画效果[通俗易懂]
flash简单制作遮罩动画效果QQ空间的开机动画大家应该都有,从最初的出现的一点到后面全部出现,如此神奇的效果到底是怎么做的呢,一起来看看吧!遮罩特效: 由于百度只能上传500k以内的照片,所以效果图片质量不是很好,当然,我们一般做的特效是.swf,这里是为了方便大家观看,所以做成了gif.
全栈程序员站长
2022/08/14
3.7K0
html遮罩层动画制作,flash简单制作遮罩动画效果[通俗易懂]
Vue Router路径切换过渡动画
实现简单的页面切换淡入淡出效果 <template> <div id="app"> <transition name="fade" mode="out-in"> <router-view></router-view> </transition> </div> </template> <script> export default { name: "App" }; </script> <style> #app { margin: 0; padding: 0;
明知山
2020/09/03
1.1K0
高效学习iOS —— Stroke和路径动画
先添加需要的代码,这里需要将storyboard的ViewController换成
CC老师
2022/01/14
1.9K0
高效学习iOS —— Stroke和路径动画
支持animate.css动画的jquery弹出层特效
jquery.popup.js是一款支持animate.css动画效果的弹出层模态窗口插件。你可以在初始化插件时,配置模态窗口打开和关闭时的CSS3,使用非常炫酷和方便。
用户5997198
2019/10/25
6.4K0
支持animate.css动画的jquery弹出层特效
LeetCode动画 | 687. 最长同值路径
今天分享一个LeetCode题,题号是687,题目是最长同值路径,题目标签是树和递归,题目难度是简单。。。
我脱下短袖
2020/02/25
6530
CVPR 2019最佳论文得主专访:非视距形状重建的费马路径理论
Robin.ly 是立足硅谷的视频内容平台,服务全球工程师和研究人员,通过与知名人工智能科学家、创业者、投资人和领导者的深度对话和现场交流活动,传播行业动态和商业技能,打造人才全方位竞争力。
OpenCV学堂
2019/07/16
9400
CVPR 2019最佳论文得主专访:非视距形状重建的费马路径理论
dotnet OpenXML 让 PathLst 自定义形状转 SVG 路径格式的 Geometry 内容
在 Office 文档里面,可以使用自己定制的自绘制形状,自己绘制的内容将会存放为 pathLst 也就是 List of Shape Paths 内容到文档里面。本文将告诉大家如何将 PathLst 自定义形状转换为标准的 SVG 路径,以支持在 WPF 或 UWP 中的 Path 元素作为 Geometry 显示
林德熙
2021/02/04
1.9K1
形状匹配
相同图像的matchShape= 0.0 相似图像的matchShape= 0.19863853606386983 不相似图像的matchShape= 0.11567279132076783
裴来凡
2022/05/28
1.2K0
形状匹配

相似问题

SwiftUI -动画路径形状笔画绘制

110

SwiftUI \x动画这个路径形状

12

动画layer.mask (不是形状层的.path )

10

用于层动画的iPhone密钥路径?

20

在SwiftUI形状中设置路径动画

113
添加站长 进交流群

领取专属 10元无门槛券

AI混元助手 在线答疑

扫码加入开发者社群
关注 腾讯云开发者公众号

洞察 腾讯核心技术

剖析业界实践案例

扫码关注腾讯云开发者公众号
领券
社区富文本编辑器全新改版!诚邀体验~
全新交互,全新视觉,新增快捷键、悬浮工具栏、高亮块等功能并同时优化现有功能,全面提升创作效率和体验
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档
查看详情【社区公告】 技术创作特训营有奖征文