CABasicAnimationsを使い始めたばかりです。これまでのところ、同じコードが必ずしも何かに対して2回機能するとは限らないように思われます。ある特定の例(私のすべての病気を治すかもしれない解決策!)では、私は自分自身の(不確定な)進行状況インジケーターを作成しました。タスクが完了するまで回転される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は私を少し怒らせています。インターネットからコードを切り取って貼り付けたのですが、うまくいかないこともあります。私の唯一の考えは、他のスレッドによってブロックされているということです。GCDを使用して少なくとも4つのプロセスがディスパッチされています。MacBookProをブロックしただけですか?
ありがとう、
トッド。