繁体   English   中英

动画bezier曲线点

[英]Animating a bezier curve point

我正在尝试使用Paintcode(很棒的应用程序,顺便说一句)制作bezier曲线的动画,并在“drawRect”方法中绘制自定义UIView。

绘图工作正常,但我想在贝塞尔曲线中为单个点设置动画。

这是我的非工作方法:

-(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;
}

使用它,屏幕上根本没有任何动作。 生成的随机位移是好的,bezierPath变量是使用类范围声明的UIBezierPath。

我错过了什么吗? (目标是做一种类似蜡烛的动画)

快速编辑刚看到您的图层代码。 您正在混合几个不同的概念。 像drawRect,CAShapeLayer,旧动画代码等......

通过以下方法,你应该能够使这个工作。

你不能这样做:(你不能动画drawRect的内容(即你不能让它在动画过程中多次绘制)。

您可以使用计时器或其他东西,并创建自己的动画类型代码。 (即创建一个每秒触发30次的计时器并运行一个函数来计算你在动画中的位置并更新你想要改变的值并调用[self setNeedsDisplay];来触发draw rect方法。

除此之外,您无能为力。

另一个编辑

在这里添加另一个选项。 使用CAShapeLayer执行此操作可能会导致性能非常差。 您可能最好使用带有一系列UIImages的UIImageView。

在UIImageView上有内置属性,animationImages,animationDuration等。

可能的解决方案

CAShapeLayerpath属性是可动画的,因此您可以使用它。

就像是...

// 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];

然后你应该能够像这样动画这条路径......

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

有关CAShapeLayer和动画路径属性的文档中有很多注释。

这应该工作。

另外,摆脱旧的动画代码。 自iOS4.3以来,你应该使用更新的块动画。

暂无
暂无

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

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