我需要将手势识别器添加到饼图的中心
Here我读到可以将我的手势记录添加到 HostView。接下来我用了:
UILongPressGestureRecognizer *longPressGesture = [[[UILongPressGestureRecognizer alloc]
initWithTarget:self
action:@selector(centerBtnDblPrsd)]
autorelease];
[longPressGesture setMinimumPressDuration:1];
[self.hostView addGestureRecognizer:longPressGesture];
(我已经添加到 .h 文件)
但这行不通,我怎样才能将手势记录添加到 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];
}
最佳答案
我也测试了你的代码
-(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 方法...如果您仍然无法正常工作,您可以提供更多代码,以便我可以针对您的问题进行更多研究。
关于ios - 将长手势识别器添加到 hostView CorePlot,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23192564/