我正在使用 UIPinchGestureRecognizer 计算 X 和 Y 坐标的变化,然后确定绘图范围应该改变的方向(X 或 Y)。它工作正常,但不像常规缩放那样平滑,并且响应较晚。有人可以建议我一个更好的方法来做到这一点
- (void)handlePinchGesture:(UIPinchGestureRecognizer *)gestureRecognizer{
if ([gestureRecognizer state] == UIGestureRecognizerStateBegan){
CGPoint translation = [gestureRecognizer locationInView:hostView];
NSLog(@"Sender value %f %f", translation.x,translation.y );
initialX = translation.x;
initialY = translation.y;
return;
}
else if ([gestureRecognizer state] == UIGestureRecognizerStateBegan || [gestureRecognizer state] == UIGestureRecognizerStateChanged){
NSLog(@"inside else");
CGPoint currentTouchLocation = [gestureRecognizer locationInView:hostView];
NSLog(@"currentTouchLocation = %f and %f and ",currentTouchLocation.x, currentTouchLocation.y);
finalX = currentTouchLocation.x;
finalY = currentTouchLocation.y;
}
}
-(CPTPlotRange *)plotSpace:(CPTPlotSpace *)space willChangePlotRangeTo:(CPTPlotRange *)newRange forCoordinate:(CPTCoordinate)coordinate
{
float x = fabsf(finalX - initialX) ;
float y = fabsf(finalY - initialY);
NSLog(@"pinch x = %f pinch y = %f", x, y);
CPTPlotRange *updatedRange = nil;
if (x > y) {
switch ( coordinate ) {
case CPTCoordinateX:
NSLog(@"x is greater than y change x-range");
if (newRange.locationDouble < 0.0F ) {
CPTMutablePlotRange *mutableRange = [[newRange mutableCopy] autorelease];
mutableRange.location = CPTDecimalFromFloat(0.0);
updatedRange = mutableRange;
}
else {
updatedRange = newRange;
}
break;
case CPTCoordinateY:
NSLog(@"x is greater than y keep y range constant");
updatedRange = ((CPTXYPlotSpace *)space).yRange;
break;
}
}
if (x < y) {
switch ( coordinate ) {
case CPTCoordinateX:
NSLog(@"y is greater than x keep x-range constant");
updatedRange = ((CPTXYPlotSpace *)space).xRange;
break;
case CPTCoordinateY:
if (newRange.locationDouble < 0.0F) {
NSLog(@"y is greater than x increase y range");
CPTMutablePlotRange *mutableRange = [[newRange mutableCopy] autorelease];
// mutableRange.location = CPTDecimalFromFloat(0.0);
updatedRange = mutableRange;
}
else {
updatedRange = newRange;
}
break;
}
}
if (x == y) {
switch ( coordinate ) {
case CPTCoordinateX:
NSLog(@"y is equal to x keep x-range constant");
updatedRange = ((CPTXYPlotSpace *)space).xRange;
break;
case CPTCoordinateY:
NSLog(@"y is equal to x keep y-range constant");
//NSLog(@"%d", CPTCoordinateY);
updatedRange = ((CPTXYPlotSpace *)space).yRange;
break;
}
}
return updatedRange;
}