简体   繁体   English

动画bezier曲线点

[英]Animating a bezier curve point

I'm trying to animate a bezier curve I made with Paintcode (great app, btw) and am drawing in a custom UIView in the "drawRect" method. 我正在尝试使用Paintcode(很棒的应用程序,顺便说一句)制作bezier曲线的动画,并在“drawRect”方法中绘制自定义UIView。

The drawing works fine but I want to animate a single point in the bezier curve. 绘图工作正常,但我想在贝塞尔曲线中为单个点设置动画。

Here's my non-working method: 这是我的非工作方法:

-(void)animateFlame{
    NSLog(@"Animating");
    // Create the starting path. Your curved line.
    //UIBezierPath * startPath;
    // Create the end path. Your straight line.
    //UIBezierPath * endPath = [self generateFlame];
    //[endPath moveToPoint: CGPointMake(167.47, 214)];

    int displacementX = (((int)arc4random()%50))-25;
    int displacementY = (((int)arc4random()%30))-15;
    NSLog(@"%i %i",displacementX,displacementY);

    UIBezierPath* theBezierPath = [UIBezierPath bezierPath];
    [theBezierPath moveToPoint: CGPointMake(167.47, 214)];
    [theBezierPath addCurveToPoint: CGPointMake(181+displacementX, 100+displacementY) controlPoint1: CGPointMake(89.74, 214) controlPoint2: CGPointMake(192.78+displacementX, 76.52+displacementY)];
    [theBezierPath addCurveToPoint: CGPointMake(167.47, 214) controlPoint1: CGPointMake(169.22+displacementX, 123.48+displacementY) controlPoint2: CGPointMake(245.2, 214)];
    [theBezierPath closePath];
    // Create the shape layer to display and animate the line.
    CAShapeLayer * myLineShapeLayer = [[CAShapeLayer alloc] init];

    CABasicAnimation * pathAnimation = [CABasicAnimation animationWithKeyPath:@"path"];
    pathAnimation.fromValue = (__bridge id)[bezierPath CGPath];
    pathAnimation.toValue = (__bridge id)[theBezierPath CGPath];
    pathAnimation.duration = 0.39f;
    [myLineShapeLayer addAnimation:pathAnimation forKey:@"pathAnimation"];

    bezierPath = theBezierPath;
}

Using this, nothing moves on the screen at all. 使用它,屏幕上根本没有任何动作。 The random displacements generated are good and the bezierPath variable is a UIBezierPath that's declared with a class scope. 生成的随机位移是好的,bezierPath变量是使用类范围声明的UIBezierPath。

Am I missing something? 我错过了什么吗? (The goal is to do a sort of candle-like animation) (目标是做一种类似蜡烛的动画)

Quick Edit Just seen your layer code. 快速编辑刚看到您的图层代码。 You are mixing up several different concepts. 您正在混合几个不同的概念。 Like drawRect, CAShapeLayer, old animation code etc... 像drawRect,CAShapeLayer,旧动画代码等......

By doing the method below you should be able to get this working. 通过以下方法,你应该能够使这个工作。

You can't do this :( you can't animate the contents of drawRect (ie you can't get it to draw multiple times over the course of the animation). 你不能这样做:(你不能动画drawRect的内容(即你不能让它在动画过程中多次绘制)。

You may be able to use a timer or something and create your own animation type code. 您可以使用计时器或其他东西,并创建自己的动画类型代码。 (ie create a timer that fires 30 times a second and runs a function that calculates where you are in the animation and updates the values you want to change and calls [self setNeedsDisplay]; to trigger the draw rect method. (即创建一个每秒触发30次的计时器并运行一个函数来计算你在动画中的位置并更新你想要改变的值并调用[self setNeedsDisplay];来触发draw rect方法。

Other than that there isn't much you can do. 除此之外,您无能为力。

ANOTHER EDIT 另一个编辑

Just adding another option here. 在这里添加另一个选项。 Doing this with a CAShapeLayer might be very poor performance. 使用CAShapeLayer执行此操作可能会导致性能非常差。 You might be best using a UIImageView with a series of UIImages. 您可能最好使用带有一系列UIImages的UIImageView。

There are built in properties, animationImages, animationDuration, etc... on UIImageView. 在UIImageView上有内置属性,animationImages,animationDuration等。

POSSIBLE SOLUTION 可能的解决方案

The path property of CAShapeLayer is animatable and so you could possible use this. CAShapeLayerpath属性是可动画的,因此您可以使用它。

Something like... 就像是...

// set up properties for path
@property (nonatomic, assign) CGPath startPath;
@property (nonatomic, assign) CGPath endPath;
@property (nonatomic, strong) CAShapeLayer *pathLayer;

// create the startPath
UIBezierPath *path = [UIBezierPath //create your path using the paint code code
self.startPath = path.CGPath;

// create the end path
path = [UIBezierPath //create your path using the paint code code
self.endPath = path.CGPath;

// create the shapee layer
self.pathLayer = [CAShapeLayer layer];
self.pathLayer.path = self.startPath;
//also set line width, colour, shadows, etc...

[self.view.layer addSubLayer:self.pathLayer];

Then you should be able to animate the path like this... 然后你应该能够像这样动画这条路径......

- (void)animatePath
{
    [UIView animateWithDuration:2.0
        animations^() {
            self.pathlayer.path = self.endPath;
    }];
}

There are lots of notes in the docs about CAShapeLayer and animating the path property. 有关CAShapeLayer和动画路径属性的文档中有很多注释。

This should work though. 这应该工作。

Also, get rid of the old animation code. 另外,摆脱旧的动画代码。 It has been gone since iOS4.3 you should be using the updated block animations. 自iOS4.3以来,你应该使用更新的块动画。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM