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

时间:2021-06-10 20:36:50

I am new to core plot framework and facing an issue:

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

i am CPGraphHostigView to draw a pie chart. As the CPGraphHostigView doesn't detect touch events i have an UIView on it which can detect touches. Now how can i pass this touch events to the pie chart so that CPPieChartDelegate method.

我是CPGraphHostigView绘制饼图。由于CPGraphHostigView没有检测到触摸事件,我在其上有一个可以检测触摸的UIView。现在,我如何将此触摸事件传递给饼图以便CPPieChartDelegate方法。

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

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

gets invoked.

Any help is appreciated. Thanks in advance.

任何帮助表示赞赏。提前致谢。

-Ravi

1 个解决方案

#1


1  

CPGraphHostingView inherits from UIView, which inherits from UIResponder. Therefore, CPGraphHostingViewdoes detect touch events.

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

Here are the steps you should follow in order to detect touch events on your pie chart slices :

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

  1. Add a CPGraphHostingView to your view in Interface Builder. You can achieve this by adding a simple UIView and change its class manually to CPGraphHostingView;
  2. 在Interface Builder中将CPGraphHostingView添加到视图中。您可以通过添加一个简单的UIView并手动将其类更改为CPGraphHostingView来实现此目的;

  3. Declare your hosting view in the code:

    在代码中声明您的主机视图:

    // In your view controller .h file:
        ...
        CPGraphHostingView *graphHosting;
    }
    
    @property (nonatomic, retain) IBOutlet CPGraphHostingView *graphHosting;
    
    
    // In your view controller .h file:
    @synthesize graphHosting;
    
  4. Make the connection with the IBOutlet in Interface Builder;

    在Interface Builder中与IBOutlet建立连接;

  5. Initialize your graph and link it to the graph hosting view:

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

    self.graph = [[CPXYGraph alloc] initWithFrame: self.graphHosting.bounds];
    self.graphHosting.hostedGraph = self.graph;
    
  6. Initialize the pie chart and set the view controller as its delegate:

    初始化饼图并将视图控制器设置为其委托:

    CPPieChart *pieChart = [[CPPieChart alloc] init];
    pieChart.delegate = self;
    ...
    [self.graph addPlot:pieChart]
    [pieChart release];
    
  7. Add <CPPieChartDelegate> to your view controller's declaration, and implement the method:

    添加到视图控制器的声明中,并实现该方法:

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

#1


1  

CPGraphHostingView inherits from UIView, which inherits from UIResponder. Therefore, CPGraphHostingViewdoes detect touch events.

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

Here are the steps you should follow in order to detect touch events on your pie chart slices :

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

  1. Add a CPGraphHostingView to your view in Interface Builder. You can achieve this by adding a simple UIView and change its class manually to CPGraphHostingView;
  2. 在Interface Builder中将CPGraphHostingView添加到视图中。您可以通过添加一个简单的UIView并手动将其类更改为CPGraphHostingView来实现此目的;

  3. Declare your hosting view in the code:

    在代码中声明您的主机视图:

    // In your view controller .h file:
        ...
        CPGraphHostingView *graphHosting;
    }
    
    @property (nonatomic, retain) IBOutlet CPGraphHostingView *graphHosting;
    
    
    // In your view controller .h file:
    @synthesize graphHosting;
    
  4. Make the connection with the IBOutlet in Interface Builder;

    在Interface Builder中与IBOutlet建立连接;

  5. Initialize your graph and link it to the graph hosting view:

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

    self.graph = [[CPXYGraph alloc] initWithFrame: self.graphHosting.bounds];
    self.graphHosting.hostedGraph = self.graph;
    
  6. Initialize the pie chart and set the view controller as its delegate:

    初始化饼图并将视图控制器设置为其委托:

    CPPieChart *pieChart = [[CPPieChart alloc] init];
    pieChart.delegate = self;
    ...
    [self.graph addPlot:pieChart]
    [pieChart release];
    
  7. Add <CPPieChartDelegate> to your view controller's declaration, and implement the method:

    添加到视图控制器的声明中,并实现该方法:

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