旋转 iOS UILayer 像一个滴答作响的时钟,不流畅地转了一圈
Rotating iOS UILayer like a ticking clock, not smoothly in a circle
我可以将图像连续旋转一定度数,但我想将图像旋转一点点,暂停,再旋转一点,暂停等。
以下代码连续执行此操作:
// rotate
CGFloat finalValue = 360 / 14.f;
CABasicAnimation *rotationAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation"];
[rotationAnimation setFromValue:[self degreesToNumber:0]];
[rotationAnimation setToValue:[self degreesToNumber:finalValue]];
[rotationAnimation setDuration:5.0];
[rotationAnimation setRemovedOnCompletion:NO];
[rotationAnimation setFillMode:kCAFillModeForwards];
[self.secondHandImageView.layer addAnimation:rotationAnimation forKey:@"rotate"];
有没有办法用我需要的角度变化次数将其包装在一个 for 循环中,并设置特定动画的持续时间和延迟?我没有尝试过。以下是我目前正在尝试的:
// rotate in ticks, so
NSTimeInterval delay = 0;
CGFloat currentAngle = 0;
CGFloat finalAngle = 360 / 14.f;
// angle difference
CGFloat numberOfTicks = 25.f;
CGFloat angleDelta = finalAngle / numberOfTicks;
for (NSUInteger tick = 0; tick < numberOfTicks; tick++) {
[UIView animateWithDuration:0.1 delay:delay options:UIViewAnimationOptionCurveLinear animations:^{
self.secondHandImageView.layer.transform = CATransform3DMakeRotation(0, 0, angleDelta, 1.0);
} completion:nil];
// update delay
delay += .2; // 200 milliseconds, 5 tickets every second
currentAngle += angleDelta;
}
您的代码没有问题 - 除了您不能在 UIView 动画块内为图层设置动画。相反,您可以为视图设置动画。将 (3D) 层变换替换为 (2D) 视图变换:
self.secondHandImageView.transform =
CGAffineTransformRotate(self.tickView.transform,angleDelta);
我可以将图像连续旋转一定度数,但我想将图像旋转一点点,暂停,再旋转一点,暂停等。
以下代码连续执行此操作:
// rotate
CGFloat finalValue = 360 / 14.f;
CABasicAnimation *rotationAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation"];
[rotationAnimation setFromValue:[self degreesToNumber:0]];
[rotationAnimation setToValue:[self degreesToNumber:finalValue]];
[rotationAnimation setDuration:5.0];
[rotationAnimation setRemovedOnCompletion:NO];
[rotationAnimation setFillMode:kCAFillModeForwards];
[self.secondHandImageView.layer addAnimation:rotationAnimation forKey:@"rotate"];
有没有办法用我需要的角度变化次数将其包装在一个 for 循环中,并设置特定动画的持续时间和延迟?我没有尝试过。以下是我目前正在尝试的:
// rotate in ticks, so
NSTimeInterval delay = 0;
CGFloat currentAngle = 0;
CGFloat finalAngle = 360 / 14.f;
// angle difference
CGFloat numberOfTicks = 25.f;
CGFloat angleDelta = finalAngle / numberOfTicks;
for (NSUInteger tick = 0; tick < numberOfTicks; tick++) {
[UIView animateWithDuration:0.1 delay:delay options:UIViewAnimationOptionCurveLinear animations:^{
self.secondHandImageView.layer.transform = CATransform3DMakeRotation(0, 0, angleDelta, 1.0);
} completion:nil];
// update delay
delay += .2; // 200 milliseconds, 5 tickets every second
currentAngle += angleDelta;
}
您的代码没有问题 - 除了您不能在 UIView 动画块内为图层设置动画。相反,您可以为视图设置动画。将 (3D) 层变换替换为 (2D) 视图变换:
self.secondHandImageView.transform =
CGAffineTransformRotate(self.tickView.transform,angleDelta);