Ios7 可以在两个方向上设置动画的动画饼图

Ios7 可以在两个方向上设置动画的动画饼图,ios7,uiview,core-animation,calayer,blending,Ios7,Uiview,Core Animation,Calayer,Blending,我正在尝试构建一个动画饼图,根据其进度的值是增加还是减少,可以在两个方向上为饼图设置动画。我从提供的示例代码开始,并根据建议进行了一些调整 问题在于“后退”动画尚未完全正常工作。它只能部分清除馅饼。我不确定这是因为我使用动画不正确还是计算坐标不正确,因为如果我插入CGContextSetFillColorWithColor(context,UIColor.redColor.CGColor)进入第一个如果分支,并将混合模式更改为kCGBlendModeNormal同样,在那里,结果同样关闭 下面是

我正在尝试构建一个动画饼图,根据其
进度
的值是增加还是减少,可以在两个方向上为饼图设置动画。我从提供的示例代码开始,并根据建议进行了一些调整

问题在于“后退”动画尚未完全正常工作。它只能部分清除馅饼。我不确定这是因为我使用动画不正确还是计算坐标不正确,因为如果我插入
CGContextSetFillColorWithColor(context,UIColor.redColor.CGColor)
进入第一个
如果
分支,并将混合模式更改为
kCGBlendModeNormal
同样,在那里,结果同样关闭

下面是我当前的代码(摘自
PieLayer.m
)——谁能提出改进建议

- (void)drawInContext:(CGContextRef)context {
    PieView *pieView = self.delegate;
    NSAssert(pieView != nil, nil);

    CGRect rect = CGRectInset(self.bounds, 1, 1);

    CGFloat radius = MIN(CGRectGetMidX(rect), CGRectGetMidY(rect));
    CGPoint center = CGPointMake(radius, CGRectGetMidY(rect));

    CGContextSetFillColorWithColor(context, UIColor.clearColor.CGColor);
    CGContextSetStrokeColorWithColor(context, pieView.circumferenceTintColor.CGColor);
    CGContextSetLineWidth(context, 2.0f);

    CGContextFillEllipseInRect(context, rect);
    CGContextStrokeEllipseInRect(context, rect);

    CGFloat startAngle   = -M_PI / 2;
    CGFloat endAngle     = self.progress     * 2 * M_PI + startAngle;
    CGFloat lastEndAngle = self.lastProgress * 2 * M_PI + startAngle;

    if (self.lastProgress > self.progress) {
        NSLog(@"clear sector between progress=%f and lastProgress=%f", self.progress, self.lastProgress);

        CGContextMoveToPoint(context, center.x, center.y);
        CGContextAddArc(context, center.x, center.y, radius, endAngle, lastEndAngle, 0);
        CGContextClosePath(context);

        CGContextSetBlendMode(context, kCGBlendModeClear);
        CGContextFillPath(context);
    }

    if (self.progress > 0) {
        NSLog(@"draw sector to progress=%f (lastProgress=%f)", self.progress, self.lastProgress);

        CGContextSetFillColorWithColor(context, pieView.sectorTintColor.CGColor);

        CGContextMoveToPoint(context, center.x, center.y);
        CGContextAddArc(context, center.x, center.y, radius, startAngle, endAngle, 0);
        CGContextClosePath(context);

        CGContextSetBlendMode(context, kCGBlendModeNormal);
        CGContextFillPath(context);
    }

    ((PieLayer *)self.modelLayer).lastProgress = self.progress;

    [super drawInContext:context];
}

您发布的答案()中的代码对我来说在两个方向都很好,即使使用
clearColor
:请参阅。
clearColor
无法产生正确行为的唯一情况是背景色为纯色且前景清晰:在这种情况下,需要更改混合模式,正如您在代码中指出的那样

我尝试了这三种配置,在所有情况下,层都正确响应
-setProgress:

  • DZRoundProgressView
    作为主视图的子视图
    UIView
  • 主视图的类设置为
    DZRoundProgressView
  • DZRoundProgressView
    作为主
    UIView
    的子层
此外,上一帧中的层内容不太可能在下一次绘制调用时仍然可见:显然,
-drawInContext:
的默认行为是在执行前清除上下文,如所述。
如果您没有使用图层的
内容
属性,并且没有对绘图管道进行黑客攻击,则
self.progress
的值可能有问题;可能表示层和模型层存在一些问题,或者在属性的实现中存在一些问题


事实上,这并不能解决您的问题,因为我无法复制或隔离它,但应该允许您解决它。

您链接的第一个问题中的代码在两个方向上都可以正常工作,对我来说,以
clearColor
为背景的事件。它不仅在前景清晰时才起作用,但在代码中似乎并非如此。瞧。@PietroSaccardi你说得对。我还看到它现在在一个独立的例子中起作用。我必须在其他地方集成链接代码时出错…@PietroSaccardi Strange,但是如果我将
DZRoundProgressViewLayer
放在视图层次结构的较低一层(将空的
UIView
作为superview),那么它将开始在两个方向工作。(使用
CGColorRef backgroundColor=UIColor.clearColor.CGColor;
)如果您想将您的评论作为答案发布,我将接受它,因为它导致了一个解决方案。+1您在解决与解决方面也是正确的。