将长手势识别器添加到hostView CorePlot

时间:2014-04-21 07:04:14

标签: ios objective-c core-plot

我需要在饼图中心添加手势识别器

enter image description here

Here我读到可以将我的手势rec添加到HostView。我用了下一个:

UILongPressGestureRecognizer *longPressGesture = [[[UILongPressGestureRecognizer alloc]
                                                       initWithTarget:self
                                                       action:@selector(centerBtnDblPrsd)]
                                                      autorelease];
    [longPressGesture setMinimumPressDuration:1];
    [self.hostView addGestureRecognizer:longPressGesture];

(我已添加到.h文件中)

但是这不起作用,我怎样才能将手势rec添加到hostView?

修改 我的CorePlot代码:

-(void)configureHost {

    // 1 - Set up view frame
    CGRect parentRect = self.firstPieChartView.bounds;
    //CGSize toolbarSize = self.myPieChartPlaceholder.bounds.size;


    parentRect = CGRectMake(0, 80,
                            parentRect.size.width,
                            parentRect.size.height);


    // 2 - Create host view
    self.hostView = [(CPTGraphHostingView *) [CPTGraphHostingView alloc] initWithFrame:parentRect];
    self.hostView.allowPinchScaling = YES;

    self.hostView.backgroundColor = [UIColor clearColor];
    [self.view addSubview:self.hostView];


}

-(void)configureGraph {

    // 1 - Create and initialize graph
    graph = [[CPTXYGraph alloc] initWithFrame:self.hostView.bounds];
    self.hostView.hostedGraph = graph;

    self.hostView.backgroundColor = [UIColor clearColor];

    graph.paddingLeft = 0.0f;
    graph.paddingTop = 0.0f;
    graph.paddingRight = 0.0f;
    graph.paddingBottom = 0.0f;
    graph.axisSet = nil;


    // 2 - Set up text style
    CPTMutableTextStyle *textStyle = [CPTMutableTextStyle textStyle];
    textStyle.color = [CPTColor grayColor];
    textStyle.fontName = @"Helvetica-Thin";
    textStyle.fontSize = 17.0f;

    // 3 - Configure title
    NSString *title =  @"";         //_infoLabel.text;
    graph.title = title;
    graph.titleTextStyle = textStyle;
    graph.titlePlotAreaFrameAnchor = CPTRectAnchorTop;
    graph.titleDisplacement = CGPointMake(0.0f, -12.0f);

    UIImage *img = [UIImage imageNamed:@"hostViewPlchldr.png"];
    CPTImage *cptImg=[CPTImage imageWithCGImage:img.CGImage scale:img.scale*2];
    graph.fill = [CPTFill fillWithImage:cptImg];
}

-(void)configureChart {

    // 1 - Get reference to graph
    graph = self.hostView.hostedGraph;
    // 2 - Create chart
    CPTPieChart *pieChart = [[CPTPieChart alloc] init];

    CPTMutableLineStyle *line=[CPTMutableLineStyle lineStyle];
    line.lineColor=[CPTColor grayColor];
    line.lineWidth = 1.0;


    pieChart.borderLineStyle = line;

    //Положение надписей на секторах
    pieChart.labelOffset = -30;

    pieChart.dataSource = self;
    pieChart.delegate = self;

    pieChart.pieRadius = (self.hostView.bounds.size.height * 1.3) / 3;
    pieChart.pieInnerRadius = 35.0;
    pieChart.identifier = graph.title;
    pieChart.startAngle = M_PI/2;
    pieChart.sliceDirection = CPTPieDirectionClockwise;

    // 3 - Create gradient
    CPTGradient *overlayGradient = [[CPTGradient alloc] init];
    overlayGradient.gradientType = CPTGradientTypeRadial;
    overlayGradient = [overlayGradient addColorStop:[[CPTColor blackColor] colorWithAlphaComponent:0.0] atPosition:0.9];
    overlayGradient = [overlayGradient addColorStop:[[CPTColor blackColor] colorWithAlphaComponent:0.4] atPosition:1.0];
    pieChart.overlayFill = [CPTFill fillWithGradient:overlayGradient];
    // 4 - Add chart to graph
    [graph addPlot:pieChart];

}


-(void)viewDidLoad
{

    _firstPieChartView.backgroundColor = [UIColor clearColor];
    [super viewDidLoad];

      UILongPressGestureRecognizer *longPressGesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longTap)];
    [longPressGesture setMinimumPressDuration:1];
    [self.hostView addGestureRecognizer:longPressGesture];
    [self.hostView setUserInteractionEnabled:YES];
}

1 个答案:

答案 0 :(得分:1)

我也测试了你的代码

-(void)viewDidLoad
{
  UILongPressGestureRecognizer *longPressGesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longTap)];
  [longPressGesture setMinimumPressDuration:1];
  [self.tempView addGestureRecognizer:longPressGesture];
  [self.tempView setUserInteractionEnabled:YES];
}

-(void)longTap
{
 NSLog(@"tapped");
}

我的longTap方法正在被调用...如果您的stil无法正常工作,您可以提供更多代码,以便我可以更好地查看您的问题。