0

我有一个 UIView,其形状从路径绘制,在循环内使用数组中的值作为半径和笔划宽度。混合很重要,开关颜色也很重要(忽略 rgb 宏)。

我想要做的是从oldShapeRadius值动画到newShapeRadius影响圆弧半径strokeWidthstrokedArc.

现在我看了很多 Core Animation 的例子,但我看不出它会如何影响效果的绘制,如下所示,以及如何使用 CALayers?

//my UIView
#import "QuartzCore/CALayer.h"
#define degreesToRadians( degrees ) ( ( degrees ) / 180.0 * M_PI )

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];

    if (self) {
        self.strokeWidth=38.0f;
        self.radius=47.0f;
        oldShapeRadius=[[NSMutableArray alloc] initWithObjects:[NSNumber numberWithFloat:1], [NSNumber numberWithFloat:1], [NSNumber numberWithFloat:1.0], [NSNumber numberWithFloat:1], nil];
        newShapeRadius=[[NSMutableArray alloc] initWithObjects:[NSNumber numberWithFloat:0.6], [NSNumber numberWithFloat:0.9], [NSNumber numberWithFloat:1.0], [NSNumber numberWithFloat:0.5], nil];
    }
}

- (void)drawRect:(CGRect)rect
{   
    for (int i=0; i<4; i++) {
        UIColor *shapeColor;

        switch (i) {
            case 0:
                shapeColor=UIColorFromRGB(CATEGORY_COLOR_BLUE);
                break;
            case 1:
                shapeColor=UIColorFromRGB(CATEGORY_COLOR_GREEN);
                break;
            case 2:
                shapeColor=UIColorFromRGB(CATEGORY_COLOR_PINK);
                break;
            case 3:
                shapeColor=UIColorFromRGB(CATEGORY_COLOR_YELLOW);
                break;
            default:
                break;
        }

        CGMutablePathRef arc = CGPathCreateMutable();

        CGPathAddArc(arc, NULL, 160.0f,85, radius*[[oldShapeRadius objectAtIndex:i] floatValue], degreesToRadians(90*i), degreesToRadians(90*(i+1)), NO);
        CGPathRef strokedArc =CGPathCreateCopyByStrokingPath(arc, NULL, strokeWidth*[[oldShapeRadius objectAtIndex:i] floatValue], kCGLineCapRound,kCGLineJoinRound,0.0f);

        context = UIGraphicsGetCurrentContext();
        CGContextSetBlendMode(context, kCGBlendModeMultiply);
        CGContextAddPath(context, strokedArc);
        CGContextSetFillColorWithColor(context, shapeColor.CGColor);

        CGContextDrawPath(context, kCGPathFill);    
}
}

哦,Stackoverflow 之王,我可以帮忙。大卫·伦奎斯特在附近吗哈哈。提前谢谢了。

4

0 回答 0