2015-09-05 41 views
1

隨着值的變化,我需要在圓形圖像邊緣移動圖像。我試着將這段代碼作爲示例移動到動畫中,但沒有效果。Objective C-在圓圈中旋轉指針

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)的背景圖像

+0

您是否嘗試使用'CGAffineTransformMakeRotation'旋轉並更改圖層的錨點? – anhtu

回答

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

+0

box.layer.anchorPoint = CGPointMake(0.5,radius/boxSize.height + 0.5);我如何在這裏找到半徑? – Prarthana

+0

@Prarthana - 在我的例子中,它是我的藍色圓弧的半徑。在您的設備中,它是灰色圓圈的半徑(或者灰色圓圈的半徑加上正在旋轉的圖形圖像的圖像視圖的高度的一半)。 – Rob

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 ; 

感謝呼籲所有伸出援助之手。