如何将触摸事件传递给饼图

时间:2011-02-09 09:04:18

标签: objective-c ios core-plot

我是核心情节框架的新手,面临一个问题:

我是CPGraphHostigView绘制饼图。由于CPGraphHostigView未检测到触摸事件,因此我可以检测到触摸事件UIView。现在,我如何将此触摸事件传递给饼图,以便CPPieChartDelegate方法。

- (void)pieChart:(CPPieChart *)plot sliceWasSelectedAtRecordIndex:(NSUInteger)index

被调用。

感谢任何帮助。提前谢谢。

-Ravi

1 个答案:

答案 0 :(得分:1)

CPGraphHostingView继承自UIView,继承自UIResponder。因此,CPGraphHostingView会检测到触摸事件。

为了检测饼图切片上的触摸事件,您应遵循以下步骤:

  1. 在Interface Builder中向您的视图添加CPGraphHostingView。您可以通过添加简单的UIView并手动将其类更改为CPGraphHostingView;
  2. 来实现此目的
  3. 在代码中声明您的托管视图:

    // In your view controller .h file:
        ...
        CPGraphHostingView *graphHosting;
    }
    
    @property (nonatomic, retain) IBOutlet CPGraphHostingView *graphHosting;
    
    
    // In your view controller .h file:
    @synthesize graphHosting;
    
  4. 在Interface Builder中与IBOutlet建立连接;

  5. 初始化图表并将其链接到图表托管视图:

    self.graph = [[CPXYGraph alloc] initWithFrame: self.graphHosting.bounds];
    self.graphHosting.hostedGraph = self.graph;
    
  6. 初始化饼图并将视图控制器设置为其委托:

    CPPieChart *pieChart = [[CPPieChart alloc] init];
    pieChart.delegate = self;
    ...
    [self.graph addPlot:pieChart]
    [pieChart release];
    
  7. <CPPieChartDelegate>添加到视图控制器的声明中,并实现方法:

    - (void)pieChart:(CPPieChart *)plot sliceWasSelectedAtRecordIndex:(NSUInteger)index