我正在制作一个测速仪应用程序,我希望箭头的行为正确-如果我在一秒钟内从零开始到200公里的发射,我希望它绕杆移动,但是如果我将旋转角度设置得足够大,它就会从底部开始很短的路。
我如何使其绕整圈而不是短途行驶?
这是我用于轮换的(简单)代码:
[UIView animateWithDuration:0.3 animations:^(){
self.arrow.transform = CGAffineTransformRotate(CGAffineTransformIdentity, -4.4);
}];
我认为我可以将其旋转成小块,但有时可能需要将其从零快速旋转到最大值(例如,如果我们没有速度的读数并且已经达到高速,则我们需要旋转箭头的大部分屏幕)。
附带的问题-如何将动画排入队列,以便可以逐个应用它们?
最佳答案
不久前,我也遇到了类似的问题。我只需要旋转180度,但有时需要顺时针旋转,有时需要逆时针旋转,但是我总是在两个方向之间来回翻转,因此代码中具有“rotated”属性。
您需要使用CALayer和CAKeyframeAnimations,以下是对我有用的代码:
-(void) showHideSpinnerWithDuration:(float) duration;
{
CALayer* layer = _spinner.layer;
CAKeyframeAnimation* animation;
animation = [CAKeyframeAnimation animationWithKeyPath:@"transform.rotation.z"];
animation.duration = duration;
animation.cumulative = YES;
animation.repeatCount = 1;
animation.removedOnCompletion = NO;
animation.fillMode = kCAFillModeForwards;
if (_rotated) {
animation.values = [NSArray arrayWithObjects:
[NSNumber numberWithFloat:DegreesToRadians(180)],
[NSNumber numberWithFloat:DegreesToRadians(0)],
nil];
self.rotated = NO;
} else {
animation.values = [NSArray arrayWithObjects:
[NSNumber numberWithFloat:DegreesToRadians(0)],
[NSNumber numberWithFloat:DegreesToRadians(180)],
nil];
self.rotated = YES;
}
animation.keyTimes = [NSArray arrayWithObjects:
[NSNumber numberWithFloat:0.0],
[NSNumber numberWithFloat:duration], nil];
animation.timingFunctions = [NSArray arrayWithObjects:
[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut],
[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut], nil];
[layer addAnimation:animation forKey:@"transform.rotation.z"];
}
关于ios - 使用CGAffineTransform旋转UIView-如何使其完整路径而不是短路径?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/17928521/