2013-11-22 64 views
1

我試圖通過旋轉超過180度來動畫移動的儀表,但它不會工作。 Im使用CGAffineTransform旋轉儀表,該儀表使用淨結果進行旋轉。這意味着使用此功能時我不能選擇CW或CCW旋轉。我如何修改此代碼以使其旋轉4弧度。目前,旋轉被轉換爲淨結果,使旋轉最小化。在iOS上使用動畫旋轉UIImageView超過180度

[UIView beginAnimations:nil context:NULL]; 
[UIView setAnimationDuration:1.25]; 
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut]; 
CGAffineTransform transform = CGAffineTransformMakeRotation(4); 
self.meter.transform = transform; 
[UIView commitAnimations]; 

編輯: 從我設法得到它的工作通過這樣

[UIView animateWithDuration:1.5 animations:^{ 
    CABasicAnimation *fullRotation; 
    fullRotation = [CABasicAnimation animationWithKeyPath:@"transform.rotation"]; 
    fullRotation.fromValue = [NSNumber numberWithFloat:0]; 
    fullRotation.toValue = [NSNumber numberWithFloat:((330*M_PI)/180)]; 
    fullRotation.duration = 2.0f; 
    fullRotation.repeatCount = 1; 
    fullRotation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]; 
    fullRotation.fillMode = kCAFillModeForwards; 
    fullRotation.removedOnCompletion = NO; 

    // add animation in your view 
    [self.meter.layer addAnimation:fullRotation forKey:@"330"]; 
    [self performSelector:@selector(stopRotate:) withObject:self.meter afterDelay:2.0]; 
}]; 

回答

0

//添加這頭前綴

#define MAXFLOAT 0x1.fffffep+127f 

//添加旋轉

[UIView animateWithDuration:1.5 animations:^{ 
     CABasicAnimation *fullRotation; 
    fullRotation = [CABasicAnimation animationWithKeyPath:@"transform.rotation"]; 
    fullRotation.fromValue = [NSNumber numberWithFloat:0]; 
    fullRotation.toValue = [NSNumber numberWithFloat:(2*M_PI))]; 
    fullRotation.duration =1.0f; 
    fullRotation.repeatCount = MAXFLOAT; 
    // add animation in your view 
    [yourView.layer addAnimation:fullRotation forKey:@"360"]; 
    [self performSelector:@selector(stopRotate:) withObject:yourView afterDelay:1.0]; 
}]; 

並添加此代碼停止旋轉從視圖

-(void)stopRotate :(UIView *)yourView 
{ 
    [yourView.layer removeAnimationForKey:@"360"]; 
} 
+0

此答案無法正常工作,但我設法通過編輯來獲得它的工作,請參閱我的編輯。 – Zeezer

0

您需要使用下面的宏的弧度度轉換的答案:

#define RADIANS_TO_DEGREES(radians) ((radians) * (180.0/M_PI)) 

和將值傳遞給函數:

-(UIImage*)rotateImage:(UIImage*)img forAngle:(CGFloat)degree { 

UIView *rotatedViewBox = [[UIView alloc]initWithFrame:CGRectMake(0, 0, img.size.width, img.size.height)]; 
CGAffineTransform t = CGAffineTransformMakeRotation(degree); 
rotatedViewBox.transform = t; 
CGSize rotatedSize = rotatedViewBox.frame.size; 

UIGraphicsBeginImageContext(rotatedSize); 

CGContextRef context = UIGraphicsGetCurrentContext(); 
CGContextTranslateCTM(context, rotatedSize.width, rotatedSize.height); 
CGContextRotateCTM(context, radian); 
CGContextScaleCTM(context, 1.0, -1.0); 

CGContextDrawImage(context, CGRectMake(-img.size.width, -img.size.height, img.size.width, img.size.height), img.CGImage); 
UIImage *returnImg = UIGraphicsGetImageFromCurrentImageContext(); 

UIGraphicsEndImageContext(); 
return returnImg; 
} 
+0

似乎沒有工作。當我將圖像發送到此功能時,沒有任何反應。進出口像這樣:self.meter.image – Zeezer

+0

@Zeezer它的工作,我已經測試。你可以請你發佈你的代碼。你怎麼使用它? –

+0

@Zeezer我已修復它。 –

0

試試這個,只是改變的基本屬性,以滿足您的要求:

CATransform3D rotationTransform = CATransform3DMakeRotation(((4) * (180.0/M_PI)), 0, 0, 1.0); 

    CABasicAnimation* rotationAnimation; 
    rotationAnimation = [CABasicAnimation animationWithKeyPath:@"transform"]; 
    rotationAnimation.toValue = [NSValue valueWithCATransform3D:rotationTransform]; 
    rotationAnimation.duration = 0.6f; 
    rotationAnimation.cumulative = YES; 
    rotationAnimation.repeatCount = 1; 

    //Add this two lines 
    rotationAnimation.fillMode = kCAFillModeForwards; 
    rotationAnimation.removedOnCompletion = NO; 

    [self.meter.layer addAnimation:rotationAnimation forKey:@"rotationAnimation"]; 

讓我知道是你的作品。

+0

它沒有旋轉4弧度,旋轉完成後圖像返回到其原始狀態。 – Zeezer

+0

我編輯了上面的代碼。 – Greg

+0

它並不真正起作用,因爲它選擇了最短的路徑。但我可以通過增加repeatCount來修改,謝謝。我可以使用easeinandout嗎?這只是一個線性動畫,我希望它像我的例子一樣平滑。 – Zeezer