我尝试将核心图缩放限制在 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 或 <2,我将其设置为 2 或 100,问题是最终值并不总是在这个 renege 内。第二个问题是,当我放大到极限时(例如,当我放大时)我想阻止缩放,而现在在极限附近有一点缩小,然后一点点放大(重复)。
如何改进代码?