2012-02-01 44 views
0

我需要在用戶進行UIRotationGesture時旋轉方塊。CALayer轉換

我有所有設置的手勢。問題在於,每當用戶移動手指時,方塊就會返回到起始位置,然後移動到新的位置。而不是從以前的位置移動到新的位置。

即如果正方形的旋轉90度,並且用戶繼續旋轉到100平方將回到0度和動畫爲100

基本上我希望廣場鏡像用戶時他們執行旋轉手勢。

- (void)respondToGesture:(UIRotationGestureRecognizer *)rec{ 

NSLog(@"Rotation: %f", rec.rotation); 

[self rotateWithRadian:rec.rotation]; 

if (rec.state == UIGestureRecognizerStateEnded) { 
    NSLog(@"gesture ended"); 
} 

} 



- (void)rotateWithRadian:(float)radian{ 

CABasicAnimation *spin = [CABasicAnimation animationWithKeyPath:@"transform.rotation"]; 

spin.removedOnCompletion = NO; 

spin.fillMode = kCAFillModeForwards; 

[spin setByValue:[NSNumber numberWithFloat:radian]]; 

[spin setDuration:1.0]; 

[squarelayer addAnimation:spin forKey:@"spinAnimation"]; 

回答

2

是不是有直接設置圖層變換而不是使用動畫的原因?

這應該做你想要什麼:

- (void)respondToGesture:(UIRotationGestureRecognizer *)rec { 
    if (rec.state == UIGestureRecognizerStateChanged) { 
     CGAffineTransform currentTransform = squareLayer.affineTransform; 
     squareLayer.affineTransform = CGAffineTransformRotate(currentTransform, gesture.rotation); 
     gesture.rotation = 0; 
    } 
} 

您還需要調整squareLayer.anchorPoint如果你想旋轉圍繞用戶的旋轉中心,而不是層的中心發生。

+0

現貨作品完美乾杯! – geminiCoder 2012-02-01 12:50:54