2012-03-11 120 views
0

我有一張圖片可以使用旋轉手勢進行旋轉,但是我想要根據旋轉速度繼續旋轉圖像。旋轉手勢結束後繼續旋轉圖像

它在我鬆開手指後旋轉,但當它停止時它不會保持在旋轉的角度。它將手指放開後的旋轉角度設置爲最後一個角度。

- (void)rotateCoaster:(UIRotationGestureRecognizer *)recognizer { 

    if(recognizer.numberOfTouches >= 2) { 
     twoTouchesDetected = YES; 
    } 
    else { 
     twoTouchesDetected = NO; 
    } 


    if(recognizer.state == UIGestureRecognizerStateBegan || 
     recognizer.state == UIGestureRecognizerStateChanged) 
    { 
     recognizer.view.transform = CGAffineTransformRotate(recognizer.view.transform, 
                  recognizer.rotation); 
     _player.rotationVelocity = recognizer.velocity; 
     _player.rotation = recognizer.rotation; 

     [recognizer setRotation:0]; 
    } 

    if(!twoTouchesDetected) { 
     [self rotationCalculations:nil]; 
     rotating = YES; 
    } 

    if(recognizer.state == UIGestureRecognizerStateEnded || 
     recognizer.state == UIGestureRecognizerStateCancelled) { 
     [self dropDownCoaster]; 
    } 
    _player.rotationVelocity = recognizer.velocity; 
} 

- (void)rotationCalculations:(NSTimer *) dt { 
// dTime += dt.timeInterval; 

    [CATransaction begin]; 
    [CATransaction setValue:[NSNumber numberWithFloat:2.0] forKey:kCATransactionAnimationDuration]; 

    CABasicAnimation *animation; 
    animation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"]; 
// animation.fromValue = [NSNumber numberWithFloat:0.0]; 
// animation.toValue = [NSNumber numberWithFloat:_player.rotationVelocity * M_PI]; 
    animation.fromValue = nil; 
    animation.toValue = nil; 
    animation.byValue = [NSNumber numberWithFloat:_player.rotationVelocity * M_PI]; 
    animation.timingFunction = [CAMediaTimingFunction functionWithName: kCAMediaTimingFunctionEaseOut]; 
    animation.delegate = self; 
    [_coasterImageView.layer addAnimation:animation forKey:@"rotationAnimation"]; 

    [CATransaction commit]; 

} 
+0

「但其停止行爲正常」是非常不精確的。請描述不良行爲以及您期望/希望做的事情。 – NJones 2012-03-11 15:58:32

+0

@NJones我有更新我的文章。對不起,我讀過它,它根本沒有幫助。 – Bassem 2012-03-15 11:46:57

回答

1

我相信在這裏你的問題是,作爲WWDC 2011屆421 - Core Animation的要點@〜26:00說,「顯式動畫不會影響您層層次的模型值」。

基本上,除了使用Core Animation來動畫旋轉外,還需要更改模型對象(UIView或CALayer)上的屬性。所以當動畫結束並且視圖再次正常渲染時,它處於正確的位置和正確的旋轉。

很有可能是這樣的:

_coasterImageView.view.transform = CGAffineTransform... //rotation with toValue 

如果你打算做大量的工作與核心動畫我強烈建議您觀看視頻。

+0

謝謝,我會看看 – Bassem 2012-04-02 06:01:07