首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >CABasicAnimation并不总是有效

CABasicAnimation并不总是有效
EN

Stack Overflow用户
提问于 2012-09-18 00:06:37
回答 1查看 441关注 0票数 1

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

代码语言:javascript
运行
复制
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];

然后以单独的方法显示在屏幕上,如下所示:

代码语言:javascript
运行
复制
[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吗?

谢谢,

托德。

EN

回答 1

Stack Overflow用户

发布于 2012-09-18 00:11:58

哦,亲爱的。我想我找到了问题所在:我从GCD块中调用进度指示器。我拿出调用并进入代码的主体(可以这样),现在看起来一切都很好……

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/12463111

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档