2011-07-19 49 views

回答

2

軸標籤可以包含任何CPTLayer(它是Core Animation的CALayer的直接子類)作爲其內容。將圖像設置爲圖層背景,並使用此圖層構建自定義標籤。幾個Core Plot示例應用程序演示了自定義標籤,儘管它們都使用文本標籤。

您有您的加入自定義標籤的圖形兩種選擇:

  1. 使用CPTAxisLabelingPolicyNone標籤策略。創建一個包含標籤的NSSet,並將其設置爲座標軸上的axisLabels屬性。如果您使用此功能,請記住除了自定義標籤之外,您還必須提供主要和/或次要的勾選位置。

  2. 使用任何其他標籤策略來生成勾號位置並實施軸委託方法。在您的代表中,在提供的位置創建新標籤並返回NO以禁止自動標籤。

埃裏克

+0

您的回答讓我相當吃驚,但我在CPLayer上繪製圖標itselt時遇到了問題。 CPLayer正在繪製正確,但沒有圖像(圖層的內容)。查看所用代碼的問題更新。 – Lukasz

+0

當您說「在提供的位置創建新標籤」時,您的意思是「tickLocation」屬性?我一直在我的代表基於委託(地點)的參數設置他們,但他們都安裝在位置= 0. – Maverick

3

接受Eric的問題,並感謝他,我爲他提供建議的解決方案運行的代碼。 也許它可以幫助別人:

if (yAxisIcons) { 


    int custonLabelsCount = [self.yAxisIcons count]; 

    NSMutableArray *customLabels = [NSMutableArray arrayWithCapacity:custonLabelsCount]; 

    for (NSUInteger i = 0; i < custonLabelsCount; i++) { 

     NSNumber *tickLocation = [NSNumber numberWithInt:i]; 
     NSString *file = [yAxisIcons objectAtIndex:i]; 
     UIImage *icon = [UIImage imageNamed:file]; 

     CPImageLayer *layer; // My custom CPLayer subclass - see code below 

      CGFloat nativeHeight = 1; 
      CGFloat nativeWidth = 1; 


     if (icon) { 

      layer = [[CPImageLayer alloc] initWithImage:icon]; 
      nativeWidth = 20;//CGImageGetWidth(icon.CGImage); 
      nativeHeight = 20;//CGImageGetHeight(icon.CGImage); 
      //layer.contents = (id)icon.CGImage; 

      if (nativeWidth > biggestCustomIconWidth) { 
       biggestCustomIconWidth = nativeWidth; 
      } 

     }else{ 
      layer = [[CPImageLayer alloc] initWithFrame:CGRectMake(0, 0, 1, 1)]; 
     } 

      CGRect startFrame = CGRectMake(0.0, 0.0, nativeWidth, nativeHeight); 

      layer.frame = startFrame; 
      layer.backgroundColor = [UIColor clearColor].CGColor; 
      CPAxisLabel *newLabel = [[CPAxisLabel alloc] initWithContentLayer:layer]; 
      newLabel.tickLocation = [tickLocation decimalValue]; 
      newLabel.offset = x.labelOffset + x.majorTickLength; 
      [customLabels addObject:newLabel]; 
      [newLabel release]; 
      [layer release]; 

    } 

    y.axisLabels = [NSSet setWithArray:customLabels]; 

} 

CPImageLayer.h

#import "CPLayer.h" 

@interface CPImageLayer : CPLayer { 

    UIImage *_image; 

} 
-(id)initWithImage:(UIImage *)image; 
@end 

CPImageLayer.m

#import "CPImageLayer.h" 
#import "CPLayer.h" 

@implementation CPImageLayer 

-(void)dealloc{ 

    [_image release]; 
    [super dealloc]; 
} 
-(id)initWithImage:(UIImage *)image{ 

    CGRect f = CGRectMake(0, 0, image.size.width, image.size.height); 

    if (self = [super initWithFrame:f]) { 

     _image = [image retain]; 
    } 

    return self; 

} 

-(void)drawInContext:(CGContextRef)ctx{ 

    CGContextDrawImage(ctx, self.bounds, _image.CGImage); 

} 
@end 

享受

+0

謝謝,工作很好! – Maverick