用于计时器动画的 CABasicAnimation 不适用于 iOS 8+
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 无效。
将最后一个参数从 NO 切换为 YES。
几年前我用这段代码实现了一个计时动画。 是一个可爱的动画,但不适用于 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 无效。
将最后一个参数从 NO 切换为 YES。