1

我刚刚开始使用 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];

然后以单独的方法将“屏幕上”带入:

[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 被屏蔽了吗?

谢谢,

托德。

4

1 回答 1

1

哦亲爱的。我想我刚刚发现了问题:我正在从 GCD 块中调用进度指示器。我将调用放入代码的主体(实际上),现在一切似乎都很好......

于 2012-09-17T16:11:58.440 回答