@interface GameLayer : CCLayer{float lastScale;}@end@implementation GameLayer-(id) init{ if ((self = [super init] )) {lastScale = 1.f; UIPinchGestureRecognizer *gestureRecognizer = [[[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(handlePinchFrom:)] autorelease]; [[[CCDirector sharedDirector] openGLView] addGestureRecognizer:gestureRecognizer];}return self;}-(void) handlePinchFrom:(UIPinchGestureRecognizer*)recognizer{if([recognizer state] == UIGestureRecognizerStateBegan){ lastScale = self.scale;}float nowScale; nowScale = (lastScale - 1) + recognizer.scale; nowScale = MIN(nowScale,2);//设置缩放上限nowScale = MAX(nowScale,1);//设置缩放下限self.scale = nowScale;}
另外一种:
- (void)handlePinchFrom:(UIPinchGestureRecognizer *)recognizer { static float lastScale; if([recognizer state] == UIGestureRecognizerStateBegan) { // Reset the last scale, necessary if there are multiple objects with different scales lastScale = [recognizer scale]; NSLog(@"lastScale:%f",lastScale); } if ([recognizer state] == UIGestureRecognizerStateBegan || [recognizer state] == UIGestureRecognizerStateChanged) { // Constants to adjust the max/min values of zoom const CGFloat kMaxScale = 2.5; const CGFloat kMinScale = 0.3; const CGFloat kSpeed = 0.75; float currentScale = [[[recognizer view].layer valueForKeyPath:@"transform.scale"] floatValue]; CGFloat newScale = 1 - (lastScale - [recognizer scale]) * (kSpeed); NSLog(@"newScale:%f",newScale); newScale = MIN(newScale, kMaxScale / currentScale); NSLog(@"new1:%f kmax:%f",newScale,kMaxScale/currentScale); newScale = MAX(newScale, kMinScale / currentScale); NSLog(@"new2:%f kmin:%f",newScale,kMinScale/currentScale); NSLog(@"%f",[recognizer scale]); CGAffineTransform transform = CGAffineTransformScale([[recognizer view] transform], newScale, newScale); [recognizer view].transform = transform; lastScale = [recognizer scale]; // Store the previous scale factor for the next pinch gesture call }}
转自:子山龙人
http://www.cnblogs.com/zilongshanren/archive/2011/05/03/2033620.html