如何获得停止/恢复 CABasicAnimation 工作的解决方案?

发布于 2024-11-27 20:15:27 字数 1929 浏览 2 评论 0原文

我正在使用 CABasicAnimation 旋转 UIImageView,但无法恢复暂停的动画。动画在 viewDidLoad 方法中开始:

UIImageView *img = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"MyImage.png"]];
self.myImage = img;
[self.view addSubview:img];
[img release];
CABasicAnimation *fullRotation = [CABasicAnimation animationWithKeyPath:@"transform.rotation"];
fullRotation.fromValue = [NSNumber numberWithFloat:0];
fullRotation.toValue = [NSNumber numberWithFloat:((360*M_PI)/180)];
fullRotation.duration = 10;
fullRotation.repeatCount = LARGE_VAL;
[img.layer addAnimation:fullRotation forKey:@"360"];

我需要有不间断的可重复动画,当视图出现在屏幕上时就会恢复。所以我已经阅读了这篇文章(此处)并实施了解决方案提供了我的苹果(解决方案)用于停止和恢复图层动画。因此,我使用了这些方法:

-(void)pauseLayer:(CALayer*)layer{
    CFTimeInterval pausedTime = [layer convertTime:CACurrentMediaTime() fromLayer:nil];
    layer.speed = 0.0;
    layer.timeOffset = pausedTime;
}

-(void)resumeLayer:(CALayer*)layer{
    CFTimeInterval pausedTime = [layer timeOffset];
    layer.speed = 1.0;
    layer.timeOffset = 0.0;
    layer.beginTime = 0.0;
    CFTimeInterval timeSincePause = [layer convertTime:CACurrentMediaTime() fromLayer:nil] - pausedTime;
    layer.beginTime = timeSincePause;
}

我已使用并传递 myImage 属性的图层将这些方法添加到 viewWillAppear 和 viewWillDisappear 中:

-(void)viewWillDisappear:(BOOL)animated{
    [self pauseLayer:self.myImage.layer];
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self resumeLayer:self.myImage.layer];
}

当视图出现在屏幕上时,图像会进行初始旋转。但随后我将一些 UIViewController 放在屏幕上,然后返回到具有图像旋转的视图。问题是当视图出现在屏幕上时,图像的旋转不会恢复。我已经检查过:我的图像 UIImageView 属性不为零, viewWillDisappear 和 viewWillAppear 方法都被调用。但动画并没有恢复。我是不是做错了什么,或者错过了什么?

I'm using CABasicAnimation for rotating UIImageView and I'm unable to resume paused animation. The animation starts in viewDidLoad method:

UIImageView *img = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"MyImage.png"]];
self.myImage = img;
[self.view addSubview:img];
[img release];
CABasicAnimation *fullRotation = [CABasicAnimation animationWithKeyPath:@"transform.rotation"];
fullRotation.fromValue = [NSNumber numberWithFloat:0];
fullRotation.toValue = [NSNumber numberWithFloat:((360*M_PI)/180)];
fullRotation.duration = 10;
fullRotation.repeatCount = LARGE_VAL;
[img.layer addAnimation:fullRotation forKey:@"360"];

I need to have non-stop repeatable animation that resumes when the view appears on screen. So I have read this post (here) and implemented solution provided my apple (solution) for stopping and resuming layer animation. So, I have used these methods:

-(void)pauseLayer:(CALayer*)layer{
    CFTimeInterval pausedTime = [layer convertTime:CACurrentMediaTime() fromLayer:nil];
    layer.speed = 0.0;
    layer.timeOffset = pausedTime;
}

-(void)resumeLayer:(CALayer*)layer{
    CFTimeInterval pausedTime = [layer timeOffset];
    layer.speed = 1.0;
    layer.timeOffset = 0.0;
    layer.beginTime = 0.0;
    CFTimeInterval timeSincePause = [layer convertTime:CACurrentMediaTime() fromLayer:nil] - pausedTime;
    layer.beginTime = timeSincePause;
}

And I have added these methods to viewWillAppear and viewWillDisappear using and passing the layer of myImage property:

-(void)viewWillDisappear:(BOOL)animated{
    [self pauseLayer:self.myImage.layer];
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self resumeLayer:self.myImage.layer];
}

The image does initial rotation when the view goes on screen. But then I'm putting some UIViewController on screen, then going back to the view with image rotation. The problem is the rotation of the image is not resumed when the view appears on screen. I have checked: my image UIImageView property is not nil, both viewWillDisappear and viewWillAppear methods get invoked. But the animation is not resuming. Did I something wrong, or missed something?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

最舍不得你 2024-12-04 20:15:27

我和你有同样的问题。经过大量谷歌搜索后...我找到了解决方案。

对于您的情况,只需在设置 CABasicAnimation 时添加以下代码:

fullRotation.removedOnCompletion = NO;

就是这样。我希望它对你有用。

I had the same problem to yours. After a lot of googling... I got a solution for it.

In your case, just add the following code when you set CABasicAnimation:

fullRotation.removedOnCompletion = NO;

That's it. I hope it would work for you.

婴鹅 2024-12-04 20:15:27

解决方法、解决方法和解决方法。到目前为止,我所采取的解决方案是放弃建议的停止/恢复图层动画的解决方案:),删除 viewWillDisappear: 中的所有动画

[self.myImage.layer removeAllAnimations];  

,然后在 viewWillAppear 中启动新的动画。我知道我不会恢复旧的,但就我而言,它对人眼来说并不那么明显,所以我想我会很好。

Workarounds, workarounds and workarounds. So far, the solution I have got working is to throw away the suggested solution for stoping/resuming layer animation :), to remove all the animations in viewWillDisappear:

[self.myImage.layer removeAllAnimations];  

and then to start a new animation in viewWillAppear. I know that I'm not resuming the old one but, in my case, it's not so visible to the humans eye, so I think I will be good.

不…忘初心 2024-12-04 20:15:27

我认为你的问题是内存管理。

试试这个:

UIImageView *img = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"MyImage.png"]];
self.myImage = img;
[img release];

CABasicAnimation *fullRotation = [CABasicAnimation animationWithKeyPath:@"transform.rotation"];
fullRotation.fromValue = [NSNumber numberWithFloat:0];
fullRotation.toValue = [NSNumber numberWithFloat:((360*M_PI)/180)];
fullRotation.duration = 10;
fullRotation.repeatCount = LARGE_VAL;
[self.myImage.layer addAnimation:fullRotation forKey:@"360"];

[self.view addSubview:self.myImage];

告诉我这是否有效。

I think your problem is memory management.

Try this instead:

UIImageView *img = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"MyImage.png"]];
self.myImage = img;
[img release];

CABasicAnimation *fullRotation = [CABasicAnimation animationWithKeyPath:@"transform.rotation"];
fullRotation.fromValue = [NSNumber numberWithFloat:0];
fullRotation.toValue = [NSNumber numberWithFloat:((360*M_PI)/180)];
fullRotation.duration = 10;
fullRotation.repeatCount = LARGE_VAL;
[self.myImage.layer addAnimation:fullRotation forKey:@"360"];

[self.view addSubview:self.myImage];

Tell me if this works.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文