博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
cocos2d视图zoom缩放
阅读量:5913 次
发布时间:2019-06-19

本文共 2344 字,大约阅读时间需要 7 分钟。

@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

 

 

 

 

转载于:https://www.cnblogs.com/pengyingh/articles/2515300.html

你可能感兴趣的文章
proc文件系统探索 之 根目录下的文件[六]
查看>>
搭建ICINGA监控
查看>>
DataSet
查看>>
第三方分享功能
查看>>
Quartz.NET 前一次任务未执行完成时不触发下次的解决方法
查看>>
SQL中的null值
查看>>
python unittest之断言及示例
查看>>
online_judge_1106
查看>>
JAVA_内部类
查看>>
jxl 导入excel
查看>>
Mysql之performance Schema
查看>>
虚拟机linux上网问题
查看>>
XMLHttpRequest - 原始AJAX初步
查看>>
laravel/lumen 单元测试
查看>>
csu2161: 漫漫上学路(Hash+最短路)
查看>>
重复引用错误:duplicate symbols for architecture x86_64
查看>>
计算机图形学 课设
查看>>
ucenter1.5通讯过程分析(转载)
查看>>
js和html5实现画板
查看>>
浏览器中可以访问,但是git命令、go get命令使用时却无法连接
查看>>