2012-07-20 44 views
1

我已經使用下面的代碼,我可以旋轉我的圖像。但是在動畫之後,我不知道如何獲得圖像的最終角度。如何測量iPhone中滑動的速度,然後計算圖像的旋轉?

我想計算「觸摸結束」事件後圖像的旋轉角度

- (void)rotateAccordingToAngle:(float)angle 
{ 
    [NumbersImage setTransform:CGAffineTransformRotate(NumbersImage.transform, angle)]; 
} 
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 
{  
    [NumbersImage.layer removeAllAnimations]; 
    previousTimestamp = event.timestamp; 
} 
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
    UITouch *touch = [touches anyObject]; 
    if ([touch view] == NumbersImage) { 

    CGPoint center = CGPointMake(CGRectGetMidX([NumbersImage bounds]), CGRectGetMidY([NumbersImage bounds])); 
    CGPoint currentTouchPoint = [touch locationInView:NumbersImage]; 
    CGPoint previousTouchPoint = [touch previousLocationInView:NumbersImage]; 
    CGFloat angleInRadians = atan2f(currentTouchPoint.y - center.y, currentTouchPoint.x - center.x) - atan2f(previousTouchPoint.y - center.y, previousTouchPoint.x - center.x); 

    [self rotateAccordingToAngle:angleInRadians]; 

    CGFloat angleInDegree = RadiansToDegrees(angleInRadians); 
    FinalDegree +=angleInDegree;   
    revolutions+= (angleInDegree/360.0f); 
} 

} 
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
    UITouch *touch = [touches anyObject]; 

if ([touch view] == NumbersImage) { 

    NSTimeInterval timeSincePrevious = event.timestamp - previousTimestamp; 
    CGFloat revolutionsPerSecond = revolutions/timeSincePrevious; 

    [self startAnimationWithRevolutions:revolutionsPerSecond forTime:5.0f]; 
    NSLog(@"Revolution per second = %f",revolutionsPerSecond); 

    revolutions = 0; 
} 
} 

CGFloat RadiansToDegrees(CGFloat radians) 
{ 
    //NSLog(@"Radians %f",radians); 
    return radians * 180/M_PI; 
}; 


- (void)animationDidStop:(CAAnimation *)theAnimation finished:(BOOL)flag 
{ 
NumbersImage.userInteractionEnabled = TRUE; 
    if (timerUpdate) { 
     [timerUpdate invalidate]; 
     timerUpdate = nil; 
    } 
} 
-(void)updateTransform{ 
    NumbersImage.transform = [[NumbersImage.layer presentationLayer] affineTransform]; 
} 
-(void)startAnimationWithRevolutions:(float)revPerSecond forTime:(float)time 
{ 
    NumbersImage.userInteractionEnabled = FALSE; 
    float totalRevolutions = revPerSecond * time; 

    timerUpdate = [NSTimer scheduledTimerWithTimeInterval:0.1f target:self selector:@selector(updateTransform) userInfo:nil repeats:YES]; 

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

    CABasicAnimation* spinAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation"]; 
CGAffineTransform transform = NumbersImage.transform; 

float fromAngle = atan2(transform.b, transform.a); 
float toAngle = fromAngle + (totalRevolutions*4*M_PI); 

NSLog(@"To Angle = %f",toAngle); 
NSLog(@"From Angle = %f",fromAngle); 
spinAnimation.fromValue = [NSNumber numberWithFloat:fromAngle]; 
spinAnimation.toValue = [NSNumber numberWithFloat:toAngle]; 
spinAnimation.repeatCount = 0; 
spinAnimation.removedOnCompletion = NO; 
spinAnimation.delegate = self; 
spinAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut]; 
[NumbersImage.layer addAnimation:spinAnimation forKey:@"spinAnimation"]; 
[CATransaction commit]; 


//NSLog(@"Total Rotation = %f",totalRevolutions); 

//NSLog(@"Degree Rotation %f", temp); 

//[self Rotate]; 
CGFloat temp = RadiansToDegrees(fromAngle); 
FinalDegree += temp; 
//NSLog(@"FinalDegree %f",FinalDegree); 


//NSLog(@"Final degree %f",FinalDegree); 
//NSLog(@"Total %f",total); 

} 

CGFloat DegreesToRadians(CGFloat degrees) 
{ 
    return degrees * M_PI/180; 
} 

此代碼用於根據用戶觸摸來測量速度和旋轉圖像。

但我無法得到圖像的最終角度。

請有人幫我在這段代碼。

謝謝,

Hemang。

回答

1

你做了錯誤的做法。看看這裏:

  1. 聲明一個實例float x;viewDidLoad使其爲零。
  2. 在touchesMoved你的x增加1(或減少,取決於你檢測到的方向)。
  3. 請記住:x是度的角度,因此,如果在零遞減引線,做x = 360.0;,反之亦然:如果遞增以上360.0引線,做x = 0.0;
  4. 在touchesMoved也有說:

myImageView.transform = CGAffineTransformMakeRotation(x * M_PI/180);

順便說一句,您將始終知道角度 - 這是x。

+0

謝謝Teodor。我很高興它正在工作。 – Hemang 2012-07-20 08:07:30

+0

不客氣。我的例子不處理速度。在這附近玩一點。 – 2012-07-20 08:09:30

+0

我提供的代碼非常適合速度,但我無法在旋轉後管理角度。但你幫助了我。現在它工作得很好。 – Hemang 2012-07-20 11:25:24