在我的应用程序中,我有一个图表,在xaxis上,我需要显示一些带有背景图像的按钮以及对每个按钮的操作。我可以添加带有文本的标签作为xaxis标签,如下所示。
int labelLocations = 0;
NSMutableArray *customXLabels = [NSMutableArray array];
for (NSString *day in arrData) {
CPTTextStyle *xaxisTextStyle = [CPTTextStyle textStyle];
xaxisTextStyle.color = [CPTColor colorWithComponentRed:204.0/255.0 green:204.0/255.0 blue:204.0/255.0 alpha:1.0];
xaxisTextStyle.fontSize = 18.0;
xaxisTextStyle.textAlignment = CPTTextAlignmentRight;
CPTAxisLabel *newLabel = [[CPTAxisLabel alloc] initWithText:day textStyle:xaxisTextStyle];
newLabel.tickLocation = [[NSNumber numberWithInt:labelLocations] decimalValue];
newLabel.offset = axisSet.xAxis.labelOffset + axisSet.xAxis.majorTickLength;
newLabel.alignment = CPTAlignmentLeft;
[customXLabels addObject:newLabel];
labelLocations++;
UIImage* backgroundImage = [UIImage imageNamed:@"icon_edit.png"];
CALayer* aLayer = [CALayer layer];
CGFloat nativeWidth = CGImageGetWidth(backgroundImage.CGImage);
CGFloat nativeHeight = CGImageGetHeight(backgroundImage.CGImage);
CGRect startFrame = CGRectMake(newLabel.contentLayer.frame.origin.x+newLabel.contentLayer.frame.size.width,0, nativeWidth, nativeHeight);
aLayer.contents = (id)backgroundImage.CGImage;
aLayer.frame = startFrame;
[newLabel.contentLayer addSublayer:aLayer];
newLabel.contentLayer.backgroundColor = [UIColor redColor].CGColor;
}
axisSet.xAxis.axisLabels = [NSSet setWithArray:customXLabels];
可以帮我添加按钮而不是CPTAxisLabel。
谢谢您的帮助
您可以使用轴委托来检测轴标签上的触摸。实现该-axis:labelWasSelected:
方法或其他选择方法之一。该contentLayer
轴的标签是CPTTextLayer
这样你就可以添加背景填充和边框,使它看起来像一个按钮。
如果您想使用real UIButton
,请将它们添加为图形托管视图的父视图的子视图,而不是添加到托管视图。使用绘图空间将数据坐标转换为像素坐标,以找到与图形对齐的按钮位置。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句