我最近遇到了同样的问题,但找不到任何解决方案。经过一段时间的研究和编码后,我找到了一些解决方案,并想分享一个非常简单的解决方案,因此它可能会帮助您了解如何解决此问题。
我创建了透明的 UIView,并将其放在 CPTGraphHostingView 之上。
该视图正在处理所需的触摸事件。
我们将其命名为 TestView
TestView.h 文件看起来像
@protocol TestViewDelegate <NSObject>
- (void)myTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event;
- (void)myTouchesMoved:(NSSet *)touches withEvent:(UIEvent *)event;
- (void)myTouchesEnded:(NSSet *)touches withEvent:(UIEvent *)event;
@end
@interface TestView : UIView
@property (nonatomic, weak) id <TestViewDelegate>delegate;
@end
测试视图.m
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
}
return self;
}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
[self.delegate myTouchesBegan:touches withEvent:event];
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
[self.delegate myTouchesMoved:touches withEvent:event];
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
[self.delegate myTouchesEnded:touches withEvent:event];
}
TestView 委托(在我的例子中是 viewController,其中包括 corePlot 托管视图)将实现这些方法并查看下面的代码示例
- (void)myTouchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
if (touches.count == 1) {
UITouch *touch = (UITouch *)[[touches allObjects] objectAtIndex:0];
CGPoint point = [touch locationInView:nil];
[self plotSpace:self.plotSpace shouldHandlePointingDeviceDraggedEvent:event atPoint:point];
}
if (touches.count == 2) {
UITouch *touch = (UITouch *)[[touches allObjects] objectAtIndex:1];
CGPoint point = [touch locationInView:nil];
[self plotSpace:self.plotSpace shouldHandlePointingDeviceDraggedEvent:event atPoint:point];
}
}
viewController 中的 CTPlotSpace 委托方法如下所示
- (BOOL)plotSpace:(CPTPlotSpace *)space shouldHandlePointingDeviceDraggedEvent:(id)event atPoint:(CGPoint)point{
NSSet *allTouches = [event allTouches];
if ([allTouches count] >0 ) {
UITouch *touch1 = [[allTouches allObjects] objectAtIndex:0];
if (touch1){
CGPoint pointInPlotArea = [self.graph convertPoint:[touch1 locationInView:self.view] toLayer:self.graph.plotAreaFrame];
// padding
pointInPlotArea.x -=10;
NSDecimal newPoint[2];
[self.graph.defaultPlotSpace plotPoint:newPoint forPlotAreaViewPoint:pointInPlotArea];
NSDecimalRound(&newPoint[0], &newPoint[0], 0, NSRoundPlain);
int x = [[NSDecimalNumber decimalNumberWithDecimal:newPoint[0]] intValue];
x--;
if (x <= 0)
x = 0;
else if (x >= [self.currentDatapoints count])
x = [self.currentDatapoints count] - 1;
selectedCoordination = x;
self.label.text = [NSString stringWithFormat:@"%@", [self.currentDatapoints objectAtIndex:x]];
self.differenceLabel.text = @"";
[touchPlot reloadData];
}
if ([allTouches count] > 1){
secondTouchPlot.hidden = NO;
UITouch *touch2 = [[allTouches allObjects] objectAtIndex:1];
if (touch2) {
CGPoint pointInPlotArea = [self.graph convertPoint:[touch2 locationInView:self.view] toLayer:self.graph.plotAreaFrame];
pointInPlotArea.x -= 10;
NSDecimal newPoint[2];
[self.graph.defaultPlotSpace plotPoint:newPoint forPlotAreaViewPoint:pointInPlotArea];
NSDecimalRound(&newPoint[0], &newPoint[0], 0, NSRoundPlain);
int x = [[NSDecimalNumber decimalNumberWithDecimal:newPoint[0]] intValue];
x--;
if (x <= 0)
x = 0;
else if (x >= [self.currentDatapoints count])
x = [self.currentDatapoints count] - 1;
selectedCoordination2 = x;
self.secondLabel.text = [NSString stringWithFormat:@"%@", [self.currentDatapoints objectAtIndex:x]];
[secondTouchPlot reloadData];
float first = [self.label.text floatValue];
float second = [[self.currentDatapoints objectAtIndex:x] floatValue];
self.differenceLabel.textColor = (first - second) > 0 ? [UIColor greenColor] : [UIColor redColor];
self.differenceLabel.text = [NSString stringWithFormat:@"%f", first - second];
}
}
}
return YES;
}
这就是结果......
这不是优化的代码,这只是我上面提到的如何解决这个问题的想法。
希望能帮助到你...