我尝试将核心图缩放限制在2个值之间,或多或少我的代码工作,但有时它无法正常工作,这就是代码:
- (void) handlePinchGesture:(UIPinchGestureRecognizer *)gestureRecognizer{
if (gestureRecognizer.state == UIGestureRecognizerStateBegan) {
scale = [gestureRecognizer scale];
}
else if (gestureRecognizer.state == UIGestureRecognizerStateChanged) {
CPTXYPlotSpace *plotSpace = (CPTXYPlotSpace *)self.graph.defaultPlotSpace;
lenghtX = [self getRoundedValue:max_X - min_X];
lenghtY = [self getRoundedValue:max_Y - min_Y];
if (lenghtX <= 100 && lenghtX >= 2) {
double stepScale = lenghtX / 10;
if ([gestureRecognizer scale] < scale) {
min_X = [self getRoundedValue:plotSpace.xRange.minLimitDouble - stepScale];
max_X = [self getRoundedValue:plotSpace.xRange.maxLimitDouble + stepScale];
min_Y = [self getRoundedValue:plotSpace.yRange.minLimitDouble - stepScale];
max_Y = [self getRoundedValue:plotSpace.yRange.maxLimitDouble + stepScale];
}
else {
min_X = [self getRoundedValue:plotSpace.xRange.minLimitDouble + stepScale];
max_X = [self getRoundedValue:plotSpace.xRange.maxLimitDouble - stepScale];
min_Y = [self getRoundedValue:plotSpace.yRange.minLimitDouble + stepScale];
max_Y = [self getRoundedValue:plotSpace.yRange.maxLimitDouble - stepScale];
}
}
else {
if (lenghtX > 100) {
double rest = [self getRoundedValue:(lenghtX - 100)/2];
min_X = [self getRoundedValue:min_X + rest];
max_X = [self getRoundedValue:max_X - rest];
min_Y = [self getRoundedValue:min_Y + rest];
max_Y = [self getRoundedValue:max_Y - rest];
NSLog(@"Lenght:%f Rest: %f",lenghtX,rest);
}
else if (lenghtX < 2) {
double rest = [self getRoundedValue:(2 - lenghtX)/2];
min_X = [self getRoundedValue:min_X - rest];
max_X = [self getRoundedValue:max_X + rest];
min_Y = [self getRoundedValue:min_Y - rest];
max_Y = [self getRoundedValue:max_Y + rest];
NSLog(@"Lenght:%f Rest: %f",lenghtX,rest);
}
}
lenghtX = [self getRoundedValue:max_X - min_X];
lenghtY = [self getRoundedValue:max_Y - min_Y];
NSLog(@"Lenght: %f",lenghtX);
plotSpace.xRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromDouble(min_X) length:CPTDecimalFromFloat(lenghtX)];
plotSpace.yRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromDouble(min_Y) length:CPTDecimalFromFloat(lenghtY)];
scale = [gestureRecognizer scale];
}
else if (gestureRecognizer.state == UIGestureRecognizerStateEnded) {
if (lenghtX <= 100 && lenghtX >= 2) {
CPTXYPlotSpace *plotSpace = (CPTXYPlotSpace *)self.graph.defaultPlotSpace;
min_X = [self getRoundedValue:plotSpace.xRange.minLimitDouble];
max_X = [self getRoundedValue:plotSpace.xRange.maxLimitDouble];
step_punto = (max_X-min_X)/numero_punti;
[self get_N_Value:numero_punti from:min_X toMax:max_X];
}
}
}
- (double) getRoundedValue:(double) value {
double roundedValue = round(value * 100.0) / 100.0;
return roundedValue;
}
在这段代码中,我检查了可见x的长度,如果是> 100或&lt; 2我将它设置为2或100,问题是并不总是最终值在这个renege中。第二个问题是当我缩放到极限时(例如当我放大时)我想阻止变焦,而现在在极限附近有一点缩小然后稍微放大(重复)。 / p>
如何改进代码?
答案 0 :(得分:0)
删除以if (lenghtX <= 100 && lenghtX >= 2)
开头的外部if-else语句。始终计算缩放范围,然后检查长度并根据需要进行调整,使其恢复到2-100范围内。
答案 1 :(得分:0)
您应该在此处查看接受的答案: Core Plot - set zoom level to show only part of graph
这是使用全局X和Y范围设置。这对我来说非常适合。
评论中还提到使用CPTPlotSpaceDelegate的方法:
-(BOOL)plotSpace:(CPTPlotSpace *)space shouldScaleBy:(CGFloat)interactionScale aboutPoint:(CGPoint)interactionPoint;
这将允许您保持最大缩放级别。请注意,实际上通过设置全局X和Y范围来处理最小缩放级别。