0

我要实现的效果:

这是代码片段:

    for (NSInteger i = 0; i < count; i++) {
    key = [NSString stringWithFormat:@"Property%d", i];
    barHeigth = valueHeight * ([[_values objectAtIndex:i] floatValue]/valueMax);
    CGRect barRect = CGRectMake(marginX, marginY - barHeigth, barWidth, barHeigth);
    CGRect bgBarRect = CGRectMake(marginX, 0, barWidth, marginY);
    BarProperty *property = _barViewDic[key];
    if (!property) {
        property = [[BarProperty alloc] init];
        property.gradientStartColor = RGBACOLOR(93, 193, 247, 0.9);
        property.gradientEndColor = RGBACOLOR(37, 114, 207, 0.9);
        _barViewDic[key] = property;
        [property release];
    }
    property.foreheadRect = barRect;
    property.backgroundRect = bgBarRect;
    [_barPropertiesArray addObject:property];

    key = [NSString stringWithFormat:@"BarView%d", i];
    BarView *barView = _barViewDic[key];
    if (!barView) {
        barView = [[BarView alloc] initWithFrame:barRect];
        barView.backgroundColor = [UIColor clearColor];
        _barViewDic[key] = barView;
        [barView release];
    }
    barView.property = property;
    barView.frame = bgBarRect;
    [self addSubview:barView];
    marginX += barWidth + padding;
}

在截图上,每个蓝色渐变矩形都是 BarView 的一个实例,问题出在这里: barView.frame = bgBarRect 绘制函数很慢,
否则 barView.frame = barRect,
一切顺利,性能良好。

注意:bgBarRect 和 barRect 的唯一区别是它们的高度,bgBarRect 的高度大于 barRect 的高度

这是 BarView 的绘制代码:

- (CGRect)getBarBounds
{
    CGFloat marginY = CGRectGetHeight(self.bounds) - CGRectGetHeight(self.property.foreheadRect);
    return CGRectMake(0, marginY, CGRectGetWidth(self.property.foreheadRect), CGRectGetHeight(self.property.foreheadRect));
}
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    CGRect barBounds = [self getBarBounds];
    CGPoint startPoint = CGPointMake(CGRectGetMidX(barBounds)/2, 0);
    CGPoint endPoint = CGPointMake(CGRectGetMidX(barBounds)/2, CGRectGetMaxY(barBounds));

    CGColorSpaceRef patternSapce = CGColorSpaceCreatePattern(NULL);

    static const CGPatternCallbacks callbacks = {0, &drawPattern, NULL};
    CGPatternRef pattern = CGPatternCreate(NULL, barBounds,
                                            CGAffineTransformIdentity,
                                            H_PATTERN_SIZE,
                                            V_PATTERN_SIZE,
                                            kCGPatternTilingNoDistortion,
                                            TRUE,
                                            &callbacks);

    CGContextRef context = UIGraphicsGetCurrentContext();

    CGContextSaveGState(context);
    CGContextClipToRect(context, barBounds);
    // Drawing code
    CGFloat alpha = 1.0;
    CGContextSetFillColorSpace(context, patternSapce);

    CGContextSetFillPattern(context, pattern, &alpha);
    CGContextFillRect(context, barBounds);

    CGGradientRef myGradient;
    CGColorSpaceRef myColorspace;
    const size_t num_locations = 2;
    CGFloat locations[2] = { 0.0, 1.0 };
    CGFloat components[8] = {
        93/255.0, 193/255.0, 247/255.0, 0.9,
        37/255.0, 114/255.0, 207/255.0, 0.9
    };

    myColorspace = CGColorSpaceCreateDeviceRGB();
    myGradient = CGGradientCreateWithColorComponents (myColorspace, components,
                                                      locations, num_locations);

    CGContextDrawLinearGradient(context, myGradient, startPoint, endPoint, 0);

    CGColorSpaceRelease(patternSapce);
    CGPatternRelease(pattern);

    CGContextRestoreGState(context);        
}

任何人都知道这一点,提前谢谢。

4

0 回答 0