View 如何旋转已随核心动画移动的视图?
我使用核心动画(CAKeyframeAnimation)使用给定路径移动视图,同时显示路径(虚线)。 在沿给定路径设置视图动画时,我希望它朝向它将要到达的方向。 换句话说,我希望视图在移动时逐渐旋转 经过一些研究,我发现使用关键时间和值进行CAKeyFrameAnimation可能是可行的,但没有成功。我不知道如何实施它 由于我还是IOS编程新手,我不知道是否有其他(更好的?)方法来制作此类动画 以下是我使用的代码:View 如何旋转已随核心动画移动的视图?,view,path,rotation,while-loop,View,Path,Rotation,While Loop,我使用核心动画(CAKeyframeAnimation)使用给定路径移动视图,同时显示路径(虚线)。 在沿给定路径设置视图动画时,我希望它朝向它将要到达的方向。 换句话说,我希望视图在移动时逐渐旋转 经过一些研究,我发现使用关键时间和值进行CAKeyFrameAnimation可能是可行的,但没有成功。我不知道如何实施它 由于我还是IOS编程新手,我不知道是否有其他(更好的?)方法来制作此类动画 以下是我使用的代码: - (CGMutablePathRef) getPathFromPoints:
- (CGMutablePathRef) getPathFromPoints:(NSMutableArray*)points {
CGMutablePathRef path = CGPathCreateMutable();
CGPoint p = [points[0] CGPointValue];
CGPathMoveToPoint(path, NULL, p.x, p.y);
for (int i = 1; i < points.count; ++i) {
p = [points[i] CGPointValue];
CGPathAddLineToPoint(path, NULL, p.x, p.y);
}
return path;
}
- (void)animateView:(UIView*)view
withPoints:(NSMutableArray*)points
withFillMode:(NSString*)fillMode
withDuration:(int)duration
withRepeatCount:(int)repeat
withSpeed:(int)speed
withDashed:(BOOL)dashed{
CGMutablePathRef path = [self getPathFromPoints:points];
if (dashed) {
CAShapeLayer* pathLayer = [CAShapeLayer layer];
pathLayer.frame = view.superview.layer.bounds;
pathLayer.bounds = view.superview.layer.bounds;
pathLayer.geometryFlipped = NO;
pathLayer.path = path;
pathLayer.strokeColor = [[UIColor blackColor] CGColor];
pathLayer.fillColor = nil;
pathLayer.lineWidth = 10.0f;
pathLayer.lineJoin = kCALineJoinBevel;
[pathLayer setLineDashPattern:[[NSArray alloc] initWithObjects:
[NSNumber numberWithInt:15],
[NSNumber numberWithInt:15],
nil]];
[view.superview.layer addSublayer:pathLayer];
CABasicAnimation *dottedAnimation = [CABasicAnimation animationWithKeyPath:@"strokeEnd"];
dottedAnimation.duration = duration;
dottedAnimation.fromValue = [NSNumber numberWithFloat:0.0f];
dottedAnimation.toValue = [NSNumber numberWithFloat:1.0f];
[pathLayer addAnimation:dottedAnimation forKey:@"strokeEnd"];
[view.superview bringSubviewToFront:view];
}
CAKeyframeAnimation* pathAnimation = [CAKeyframeAnimation animationWithKeyPath:@"position"];
pathAnimation.calculationMode = kCAAnimationPaced;
pathAnimation.fillMode = fillMode;
pathAnimation.duration = duration;
pathAnimation.repeatCount = repeat;
pathAnimation.speed = speed;
pathAnimation.path = path;
[view.layer addAnimation:pathAnimation forKey:_myAnimationKey];
CGPoint p = [[points lastObject] CGPointValue];
view.layer.position = CGPointMake(p.x, p.y);
}
-(CGMutablePathRef)getPathFromPoints:(NSMutableArray*)点{
CGMutablePathRef path=CGPathCreateMutable();
CGPoint=[points[0]CGPointValue];
CGPathMoveToPoint(路径,NULL,p.x,p.y);
对于(int i=1;i
提前感谢您的帮助,请查看CAKeyframeAnimation的rotationMode属性。这将完全实现你希望做的事情 将此行添加到-animateView:方法中,在该方法中设置*pathAnimation CAKeyframeAnimation实例的属性: pathAnimation.rotationMode=kCAAnimationRotateAuto 还有另一个设置值kCAAnimationRotateAutoReverse,它允许动画对象相对于路径切线保持反平行方向
链接到Apple文档:您可能需要添加一个示例或其他解释。