CABasicAnimation中的闪烁可旋转 [英] Flickering in CABasicAnimation for rotation

查看:202
本文介绍了CABasicAnimation中的闪烁可旋转的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

每当按下按钮时,我都试图将CAShapeLayer从其当前角度旋转一个角度.

I am trying to rotate a CAShapeLayer by an angle from its current angle whenever a button is pressed.

我正在使用委托函数animationDidStop在动画结束时设置图层的转换,因为我注意到动画仅更改了表示层的转换,而没有更改图层本身.

I am using delegate function animationDidStop to set the transform of the layer during end of animation, since I have noticed animation only changes the transform of the presentation layer and not the layer in itself.

但是,由于在代理函数animationDidStop中更新转换之前,图层已返回其先前的转换,所以动画完成后,动画中似乎出现了随机闪烁.如何消除闪烁?

But there are random flickering in the animation which seems to be during the end of the animation when the animation is complete due to layer going back to its previous transform just before the transform is updated in the delegate function animationDidStop. How do I remove the flicker?

@implementation ViewController

- (void)animationDidStop:(CAAnimation *)theAnimation finished:(BOOL)flag
{
    [CATransaction begin];
    [CATransaction setValue: (id) kCFBooleanTrue forKey: kCATransactionDisableActions];
    self.parentLayer.transform = CATransform3DRotate(self.parentLayer.transform, DEG2RAD(60.0), 0, 0, 1);
    [CATransaction commit];
}

- (IBAction)rotateBySixtyPressed:(id)sender {
    CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"transform.rotation"];
    animation.duration = 3.0;
    animation.byValue = [NSNumber numberWithFloat:DEG2RAD(60.0)];
    [animation setDelegate:self];
    [self.parentLayer addAnimation:animation forKey:animation.keyPath];
}

推荐答案

我已经通过引用此博客解决了它: https://stackoverflow.com/a/7690841/3902153

I have solved it referring this blog: http://oleb.net/blog/2012/11/prevent-caanimation-snap-back/ and this answer https://stackoverflow.com/a/7690841/3902153

我不再使用委托函数.

- (IBAction)rotateBySixtyPressed:(id)sender {
    CATransform3D old_transform = self.parentLayer.transform;
    self.parentLayer.transform = CATransform3DRotate(self.parentLayer.transform, DEG2RAD(60.0), 0, 0, 1);
    CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"transform"];
    animation.fromValue = [NSValue valueWithCATransform3D:old_transform];
    animation.duration = 3.0;
    [self.parentLayer addAnimation:animation forKey:@"transform"];
}

这篇关于CABasicAnimation中的闪烁可旋转的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆