![](/img/trans.png)
[英]Make a chronometer iOS animation with CABasicAnimation and CAShapeLayer
[英]CABasicAnimation used for a chronometer animation doesn't work with iOS 8+
幾年前,我已經使用此代碼實現了計時器動畫。 是一個可愛的動畫,但不適用於iOS8>
應該會出現一個圓圈,每0.1秒丟失一個餅,但動畫不會開始。 它適用於我的iPhone 5 iOS 7.1
我試圖解決它,但2小時后我沒有解決方案。
可以在CABasicAnimation方面有更多經驗的人可以幫助我嗎?
謝謝。
這是代碼:
//Animazione CRONOMETRO
-(void) startCronometro{
//SetTime
counterStart = TIME;
[sfondoCronometro setImage:[UIImage imageNamed:@"cronoStart.png"]];
maskLayer = [CAShapeLayer layer];
CGFloat maskHeight = sfondoCronometro.frame.size.height;
CGFloat maskWidth = sfondoCronometro.frame.size.width;
CGPoint centerPoint;
centerPoint = CGPointMake(sfondoCronometro.frame.size.width/2, (sfondoCronometro.frame.size.height/2));
//Make the radius of our arc large enough to reach into the corners of the image view.
CGFloat radius = sqrtf(maskWidth * maskWidth + maskHeight * maskHeight)/2;
//Don't fill the path, but stroke it in black.
maskLayer.fillColor = [[UIColor clearColor] CGColor];
maskLayer.strokeColor = [[UIColor blackColor] CGColor];
maskLayer.lineWidth = 25;
CGMutablePathRef arcPath = CGPathCreateMutable();
//Move to the starting point of the arc so there is no initial line connecting to the arc
CGPathMoveToPoint(arcPath, nil, centerPoint.x, centerPoint.y-radius/2);
//Create an arc at 1/2 our circle radius, with a line thickess of the full circle radius
CGPathAddArc(arcPath, nil, centerPoint.x, centerPoint.y, radius/2, 3*M_PI/2, -M_PI/2, NO);
maskLayer.path = arcPath;//[aPath CGPath];//arcPath;
//Start with an empty mask path (draw 0% of the arc)
maskLayer.strokeEnd = 0;
CFRelease(arcPath);
//Install the mask layer into out image view's layer.
sfondoCronometro.layer.mask = maskLayer;
//Set our mask layer's frame to the parent layer's bounds.
sfondoCronometro.layer.mask.frame = sfondoCronometro.layer.bounds;
//Create an animation that increases the stroke length to 1, then reverses it back to zero.
CABasicAnimation *swipe = [CABasicAnimation animationWithKeyPath:@"strokeEnd"];
swipe.duration = TIME;
NSLog(@"TIME: %f", swipe.duration);
swipe.delegate = self;
// [swipe setValue:@"string" forKey:@"key"];
swipe.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];
swipe.fillMode = kCAFillModeForwards;
swipe.removedOnCompletion = NO;
swipe.toValue = [NSNumber numberWithFloat: 1.0];
[maskLayer addAnimation: swipe forKey: @"strokeEnd"];
}
我似乎記得在某些時候CGPathAddArc的功能發生了變化,並且根據開始和結束角度的值,您必須在調用時翻轉順時針標志。 嘗試使用順時針= YES。
我只是在我的一個應用程序中做了一些測試,並確認了這一點。 對於您使用的角度,順時針NO不適用於iOS <= 7,但不適用於iOS> = 8。
將最后一個參數從“否”切換為“是”。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.