目标C-将指针旋转到圆圈中

时间:2015-09-05 12:29:52

标签: ios objective-c caanimation

当值改变时,我需要在圆形图像的边缘移动图像。我尝试将此代码作为示例移动动画,但没有效果。

CGMutablePathRef path=CGPathCreateMutable()
CGPathMoveToPoint(path,NULL,image.frame.origin.x,image.frame.origin.y)
CGPathAddQuadCurveToPoint(path,NULL,image.frame.origin.x,image.frame.origin.y,pointer.frame.origin.x,pointer.frame.origin.y);

CAKeyframeAnimation *circle=[CAKeyframeAnimation animationWithKeyPath:@"circle"];
circle.path=path;
[circle setCalculationMode=kCAAnimationCubic];
[circle setFillMode=kCAFillModeForwards];
[pointer.layer addAnimation:circle forKey:nil];

pointerUIImage)是可移动对象,imageUIImage)是背景图片

2 个答案:

答案 0 :(得分:6)

您可以更改定位点,然后为transform属性设置动画,例如

UIView *box = [[UIView alloc] initWithFrame:CGRectMake(0, 0, boxSize.width, boxSize.height)];  // `origin` doesn't matter, because we'll set `center` below
box.layer.anchorPoint = CGPointMake(0.5, radius / boxSize.height + 0.5);
box.center = center;   // this is the center of the arc
box.backgroundColor = [UIColor lightGrayColor];
box.layer.transform = CATransform3DMakeRotation( M_PI * 3.0 / 4.0, 0, 0, 1);
[self.view addSubview:box];

// note you could use `CABasicAnimation`, but I'd use `CAKeyframeAnimation` so that I control direction of animation

CAKeyframeAnimation *rotate = [CAKeyframeAnimation animationWithKeyPath:@"transform"];

NSMutableArray *values = [NSMutableArray array];
for (NSInteger i = 3; i >= -3; i--) {
    [values addObject:[NSValue valueWithCATransform3D:CATransform3DMakeRotation(-M_PI * i / 4.0, 0, 0, 1)]];
}
rotate.values = values;

rotate.duration = 5.0;

[box.layer addAnimation:rotate forKey:@"rotateBox"];

enter image description here

答案 1 :(得分:0)

将这些声明为全局

  tmax=100; 
  min=0;
  oldpsin=0; 

调用旋转

spin=valueToRotate*280.0/(max-min)*M_PI/180.0;
needle.transform=CGAffineTransformRotate(needle.transform, spin-oldspin);
oldspin=spin ;

感谢所有帮助。