使用手势放大和缩小View如何设置最大和最小界限
展开全部
第一步:声明手势对象,并加入你想要作用的view上
// 放大缩小手势
UIPinchGestureRecognizer *pinchRecognizer = [[UIPinchGestureRecognizer alloc]
initWithTarget:self action:@selector(scale:)];
[pinchRecognizer setDelegate:self];
[self.view addGestureRecognizer:pinchRecognizer];
[pinchRecognizer release];
第二步:声明3个成员变量,lastScale用来保存上次放大或缩小的那个值,并赋初值为1,因为原大小时就是1.
CGFloat lastScale = 1.0f;
设置可允许放大或缩小的值.
CGFloat mixScale, maxScale;
第三步:实现scale方法,来获取当前scale的值,并设置view的缩放。
- (void)scale:(UIGestureRecogize *)gesure {
if([recognizer state] == UIGestureRecognizerStateBegan) //1 {
lastScale = self.scale; //2 }
float nowScale; //3 nowScale = (lastScale - 1) + recognizer.scale; //4 nowScale = MIN(nowScale,maxScale);//不大于缩放上限 //5 nowScale = MAX(nowScale,mixScale);//不低于缩放下限 //6
CGAffineTransform currentTransform = self.view.transform;
self.view.transform = CGAffineTransformScale(currentTransform, nowScale,nowScale);
}
// 放大缩小手势
UIPinchGestureRecognizer *pinchRecognizer = [[UIPinchGestureRecognizer alloc]
initWithTarget:self action:@selector(scale:)];
[pinchRecognizer setDelegate:self];
[self.view addGestureRecognizer:pinchRecognizer];
[pinchRecognizer release];
第二步:声明3个成员变量,lastScale用来保存上次放大或缩小的那个值,并赋初值为1,因为原大小时就是1.
CGFloat lastScale = 1.0f;
设置可允许放大或缩小的值.
CGFloat mixScale, maxScale;
第三步:实现scale方法,来获取当前scale的值,并设置view的缩放。
- (void)scale:(UIGestureRecogize *)gesure {
if([recognizer state] == UIGestureRecognizerStateBegan) //1 {
lastScale = self.scale; //2 }
float nowScale; //3 nowScale = (lastScale - 1) + recognizer.scale; //4 nowScale = MIN(nowScale,maxScale);//不大于缩放上限 //5 nowScale = MAX(nowScale,mixScale);//不低于缩放下限 //6
CGAffineTransform currentTransform = self.view.transform;
self.view.transform = CGAffineTransformScale(currentTransform, nowScale,nowScale);
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询