animation贝塞尔曲线点
我正在试图使用Paintcode(伟大的应用程序,顺便说一句)制作一个贝塞尔曲线,并在“drawRect”方法中绘制自定义的UIView。
绘图工作正常,但我想要在贝塞尔曲线单一点的animation。
这是我的非工作方法:
-(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; }
使用这个,屏幕上根本没有任何东西移动。 生成的随机位移是很好的,bezierPathvariables是一个用类作用域声明的UIBezierpath。
我错过了什么吗? (目标是做一种蜡烛般的animation)
快速编辑刚刚看到你的图层代码。 你正在混合几个不同的概念。 像drawRect,CAShapeLayer,旧的animation代码等…
通过下面的方法,你应该能够得到这个工作。
你不能这样做:(你不能animationdrawRect的内容(即你不能让它在animation的过程中多次绘制)。
您可能能够使用计时器或其他东西,并创build您自己的animationtypes代码。 (即创build一个定时器,每秒触发30次,并运行一个函数,计算你在animation中的位置,并更新你想改变的值,并调用[self setNeedsDisplay];
来触发绘制矩形方法。
除此之外,你可以做的不多。
另一个编辑
只需在此添加另一个选项。 用CAShapeLayer做这个可能是非常糟糕的performance。 你可能最好使用UIImageView和一系列UIImage。
在UIImageView中内置了属性,animationImages,animationDuration等。
可能的解决scheme
CAShapeLayer
的path
属性是animation的,所以你可以使用这个。
就像是…
// 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];
那么你应该能够像这样的pathanimation…
- (void)animatePath { [UIView animateWithDuration:2.0 animations^() { self.pathlayer.path = self.endPath; }]; }
在关于CAShapeLayer的文档中有很多注释和animationpath属性。
这应该工作。
另外,摆脱旧的animation代码。 从iOS4.3开始,你应该使用更新的块animation。