【问题标题】:Objective C- Rotate a pointer in a circle目标 C- 将指针旋转一圈
【发布时间】:2015-09-05 12:29:51
【问题描述】:

随着值的变化,我需要围绕圆形图像的边缘移动图像。我尝试将此代码作为示例以在动画中移动但没有效果。

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

pointer (UIImage) 是可移动对象,image (UIImage) 是背景图片

【问题讨论】:

  • 您是否尝试使用CGAffineTransformMakeRotation 旋转并更改图层的锚点?

标签: ios objective-c caanimation


【解决方案1】:

您可以更改锚点,然后为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"];

【讨论】:

  • box.layer.anchorPoint = CGPointMake(0.5, radius / boxSize.height + 0.5);我怎样才能在这里找到半径??
  • @Prarthana - 在我的示例中,它是我的蓝色弧的半径。在你的,它是灰色圆圈的半径(或灰色圆圈的半径加上正在旋转的图形图像的图像视图高度的一半)。
【解决方案2】:

将这些声明为全局

  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 ;

感谢所有的帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多