![](/img/trans.png)
[英]How to add long press gesture recognizer to a button inside of tableview cell?
[英]Add long gesture recognizer to the hostView CorePlot
我需要將手勢識別器添加到餅圖的中心
在這里,我讀到可以將筆勢記錄添加到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方法被調用...如果您的stil無法正常工作,您可以提供更多代碼,以便針對您的問題進行更多研究。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.