这应该是非常简单的事情,但我还没有成功地使用块来使其工作。对此有一些问题和答案,但我发现的所有问题都可以通过使用来解决CABasicAnimation
而不是由UIView
基于块的动画,这就是我所追求的。
以下代码不起作用(基于块),没有动画:
CGAffineTransform spin = CGAffineTransformRotate(spiningView.transform, DEGREES_RADIANS(360));
CATransform3D identity = CATransform3DIdentity;
CATransform3D spin2 = CATransform3DRotate(identity, DEGREES_RADIANS(360), 0.0f, 0.0f, 1.0f);
[UIView animateWithDuration:3.0f
delay:0.0f
options:UIViewAnimationOptionCurveLinear
animations:^
{
spiningView.transform = spin;
//spiningView.layer.transform = spin2;
//Have also tried the above, doesn't work either.
}
completion:^(BOOL finished)
{
spiningView.transform = spin;
//spiningView.layer.transform = spin2;
}];
根据我的理解,当我们每次使用 Block-Based 时,不会发生动画,当UIViewAnimation
块“看到”开始值与最终值相同。公平地说,将其设置为 360 度移动将意味着该对象保持在原来的位置。但它必须是一种使用基于块的动画来制作动画的方法,因为以下CABasicAnimation
会完美地工作:
CABasicAnimation* rotationAnimation;
rotationAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
rotationAnimation.fromValue = [NSNumber numberWithFloat:0.0f];
rotationAnimation.toValue = [NSNumber numberWithFloat: M_PI * 2.0f];
rotationAnimation.duration = 3.0f;
rotationAnimation.cumulative = YES;
rotationAnimation.repeatCount = 1;
[spiningView.layer addAnimation:rotationAnimation forKey:@"rotationAnimation"];
另外,以下基于块的工作正常,但动画之间有一个停止(首先旋转到 180 度,然后再旋转 180 度以完成旋转),这不是我想要的:
[UIView animateWithDuration:3.0f
delay:0.0f
options:UIViewAnimationOptionCurveLinear
animations:^
{
spiningView.transform = CGAffineTransformRotate(spiningView.transform, DEGREES_RADIANS(180));;
}
completion:^(BOOL finished)
{
[UIView animateWithDuration:3.0f
delay:0.0f
options:UIViewAnimationOptionCurveLinear
animations:^
{
spiningView.transform = CGAffineTransformRotate(spiningView.transform, DEGREES_RADIANS(360));
}
completion:^(BOOL finished)
{
}];
}];
我知道我可以节省很多时间并且只需要使用CABasicAnimation
并完成它,但我想know为什么在这种情况下一个有效而另一个无效(进行 360 度旋转)。我希望您能给我一个关于本例中的 2 和一些可以执行完整 360 度旋转的代码(基于块)之间的详细解释。
提前致谢。