在iOS上使用动画将UIImageView旋转180度以上 [英] Rotating UIImageView more than 180 degrees using animations on iOS
问题描述
我正在尝试通过将移动的仪表旋转180度以上来为其设置动画,但是无法正常工作.我使用 CGAffineTransform
旋转电表,电表使用净结果进行旋转.这意味着使用此功能时,我无法选择CW或CCW旋转.如何修改此代码以使其旋转4弧度.当前,旋转被转换为最终结果,从而使旋转最小.
Im trying to animate a moving meter by rotating it more than 180 degrees, but it wont work. Im using CGAffineTransform
to rotate the meter, which uses a net result to make the rotation. This means that i cannot choose CW or CCW rotation when using this function. How can I modified this code to make it rotate 4 radians. Currently the rotation is transformed to a net result, making the rotation minimal.
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:1.25];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
CGAffineTransform transform = CGAffineTransformMakeRotation(4);
self.meter.transform = transform;
[UIView commitAnimations];
从答案中,我设法做到了这一点
From the answers i managed to get it working by doing this
[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];
}];
推荐答案
///在标头前缀中添加
//Add This in header prefix
#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];
}];
并添加此代码以停止从视图中旋转
And Add this code to stop rotation From view
-(void)stopRotate :(UIView *)yourView
{
[yourView.layer removeAnimationForKey:@"360"];
}
这篇关于在iOS上使用动画将UIImageView旋转180度以上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!