CABasicAnimation并不总是有效

时间:2012-09-17 16:06:37

标签: macos cabasicanimation

我刚刚开始使用CABasicAnimations。到目前为止,在我看来,相同的代码不一定会在任何事情上工作两次。在一个特定的例子中(解决方案可以治愈我所有的弊病!)我已经制定了自己的(不确定的)进度指标。只是来自PhotoShop的一个png,它旋转到任务完成,它在视图的initWithRect:

中启动
CALayer *mainLayer = [CALayer layer];
[myView setWantsLayer:YES];
[myView setLayer:mainLayer];    
progressLayer = [CALayer layer];
progressLayer.opacity = 0;
progressLayer.cornerRadius = 0.0;
progressLayer.bounds = CGRectMake(0.0,0.0,50.0,50.0);
NSDictionary* options = [NSDictionary dictionaryWithObjectsAndKeys:
                                         (id)kCFBooleanTrue, (id)kCGImageSourceShouldCache,
                                         (id)kCFBooleanTrue, (id)kCGImageSourceShouldAllowFloat,
                                         (id)kCFBooleanTrue, (id)kCGImageSourceCreateThumbnailWithTransform,
                                         NULL];

CGImageSourceRef isr = CGImageSourceCreateWithURL((__bridge CFURLRef)[[NSBundle mainBundle] URLForImageResource:@"progress_indicator.png"], NULL);
        progressLayer.contents = (__bridge id)CGImageSourceCreateImageAtIndex(isr, 0, (__bridge CFDictionaryRef)options);
[mainLayer addSublayer:progressLayer];

然后用一个单独的方法带来'onscreen':

[CATransaction begin]; //I did this block to snap the indicator to the centre
[CATransaction setValue:(id)kCFBooleanTrue forKey:kCATransactionDisableActions];
progressLayer.anchorPoint = anchorMiddle; //make sure the png is in the view centre
progressLayer.position = viewCentre;
progressLayer.opacity = 1.0;
[CATransaction setValue:(id)kCFBooleanFalse forKey:kCATransactionDisableActions];
[CATransaction commit];
[CATransaction flush];

CABasicAnimation* rotationAnim = [CABasicAnimation animationWithKeyPath: @"transform.rotation.z"];
rotationAnim.fromValue = [NSNumber numberWithFloat:0.0];
rotationAnim.toValue = [NSNumber numberWithFloat:-2 * M_PI];
rotationAnim.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];
rotationAnim.duration = 5;
rotationAnim.repeatCount = 10000;
rotationAnim.removedOnCompletion = NO;
rotationAnim.autoreverses = NO;
[progressLayer addAnimation:rotationAnim forKey:@"transform.rotation.z"];

它通常有效 - 但并非总是如此。一般来说,CABasicAnimations让我有点生气:我切入&从互联网粘贴代码,有时他们有时不工作。我唯一的想法就是被其他线程阻挡了。我有至少4个使用GCD发送的进程。是不是我封锁了我的MacBookPro?

谢谢,

托德。

1 个答案:

答案 0 :(得分:1)

亲爱的,亲爱的。我想我刚刚发现了问题:我在GCD块中调用了进度指示器。我把呼叫拿出来进入代码的主体(就像它一样),现在看起来都很好....