I am trying to animate a UIBeizerPath drawing (in my example, a triangle) in a subclass of UIView. However, the entire subview is animated instead of the form.
Is there something that I am missing in the animation?
- (void)drawRect:(CGRect)rect { CAShapeLayer *drawLayer = [CAShapeLayer layer]; drawLayer.frame = CGRectMake(0, 0, 100, 100); drawLayer.strokeColor = [UIColor greenColor].CGColor; drawLayer.lineWidth = 4.0; [self.layer addSublayer:drawLayer]; UIBezierPath *path = [UIBezierPath bezierPath]; [path moveToPoint:CGPointMake(0,0)]; [path addLineToPoint:CGPointMake(50,100)]; [path addLineToPoint:CGPointMake(100,0)]; [path closePath]; CGPoint center = [self convertPoint:self.center fromView:nil]; [path applyTransform:CGAffineTransformMakeTranslation(center.x, center.y)]; [[UIColor redColor] set]; [path fill]; [[UIColor blueColor] setStroke]; path.lineWidth = 3.0f; [path stroke]; CAKeyframeAnimation *pathAnimation = [CAKeyframeAnimation animationWithKeyPath:@"position"]; pathAnimation.duration = 4.0f; pathAnimation.path = path.CGPath; pathAnimation.calculationMode = kCAAnimationLinear; [drawLayer addAnimation:pathAnimation forKey:@"position"]; }
Cory D. wiles
source share