用于计时器动画的CABasicAnimation不适用于iOS 8+

时间:2016-02-16 12:17:56

标签: ios xcode animation cabasicanimation

一年前,我使用此代码实现了计时器动画。 是一个可爱的动画,但不适用于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"];

}

1 个答案:

答案 0 :(得分:2)

我似乎记得在某些时候CGPathAddArc的功能发生了变化,并且根据您的开始和结束角度的值,您必须在呼叫上翻转顺时针标志。尝试使用顺时针= YES。

我刚在我的应用程序中进行了一些测试并确认了这一点。对于您正在使用的角度,顺时针NO适用于iOS< = 7,但对于iOS> = 8则无效。

将最后一个参数从NO切换为YES。