gpt4 book ai didi

ios - 应用完成时 CABasicAnimation 闪烁

转载 作者:可可西里 更新时间:2023-11-01 04:15:24 24 4
gpt4 key购买 nike

我正在尝试按度数将旋转动画应用到 UIImageView 并在完成 block 中保留旋转转换。

我面临的问题是,当执行完成 block 时,从动画的结束状态传递到完成 block 会产生可见的闪烁。

这是我目前使用的代码:

if (futureAngle == currentAngle) {
return;
}

float rotationAngle;
if (futureAngle < currentAngle) {
rotationAngle = futureAngle - currentAngle;
}else{
rotationAngle = futureAngle - currentAngle;
}

float animationDuration = fabs(rotationAngle) / 100;
rotationAngle = GLKMathDegreesToRadians(rotationAngle);

[CATransaction begin];
CABasicAnimation *rotationAnimation;
rotationAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
rotationAnimation.byValue = [NSNumber numberWithFloat:rotationAngle];
rotationAnimation.duration = animationDuration;
rotationAnimation.removedOnCompletion = YES;

[CATransaction setCompletionBlock:^{
view.transform = CGAffineTransformRotate(view.transform, rotationAngle);
}];

[view.layer addAnimation:rotationAnimation forKey:@"rotationAnimation"];
[CATransaction commit];

最佳答案

当你说闪烁时,我假设你的意思是在动画结束时,它会在返回最终状态之前暂时返回到初始状态?这可以通过以下方式解决

  • 设置最终view.transform在你开始动画之前(你不再需要 completionBlock);
  • 通过设置动画的fillModekCAFillModeForwards并设置 removedOnCompletionfalse .

就我个人而言,我认为在开始动画之前将动画属性设置为其目标值是执行此操作的最简单方法。

因此:

- (void)rotate:(UIView *)view by:(CGFloat)delta {
float animationDuration = 2.0;
CGFloat currentAngle = self.angle; // retrieve saved angle
CGFloat nextAngle = self.angle + delta; // increment it
self.angle = nextAngle; // save new value

view.transform = CGAffineTransformMakeRotation(nextAngle); // set property to destination rotation

CABasicAnimation *rotationAnimation;
rotationAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"]; // now rotate
rotationAnimation.fromValue = @(currentAngle);
rotationAnimation.toValue = @(nextAngle);
rotationAnimation.duration = animationDuration;
rotationAnimation.removedOnCompletion = YES;
[view.layer addAnimation:rotationAnimation forKey:@"rotationAnimation"];
}

或者,我认为更简单,只需调整 transform :

- (void)rotate:(UIView *)view by:(CGFloat)delta {
float animationDuration = 2.0;
CGAffineTransform transform = view.transform; // retrieve current transform
CGAffineTransform nextTransform = CGAffineTransformRotate(transform, delta); // increment it

view.transform = nextTransform; // set property to destination rotation

CABasicAnimation *rotationAnimation;
rotationAnimation = [CABasicAnimation animationWithKeyPath:@"transform"]; // now rotate
rotationAnimation.fromValue = [NSValue valueWithCGAffineTransform:transform];
rotationAnimation.toValue = [NSValue valueWithCGAffineTransform:nextTransform];
rotationAnimation.duration = animationDuration;
rotationAnimation.removedOnCompletion = YES;
[view.layer addAnimation:rotationAnimation forKey:@"rotationAnimation"];
}

关于ios - 应用完成时 CABasicAnimation 闪烁,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38081182/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com