実装したい効果:
コード スニペットは次のとおりです。
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 のインスタンスです。ここに問題があります
。
注: 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);
}
誰でもその点を知っています、事前に感謝します。