可能重复:
Using UIPinchGestureRecognizer to scale uiviews in single direction
我的代码如下:
UIPinchGestureRecognizer *croperViewGessture = [[UIPinchGestureRecognizer alloc]initWithTarget:self action:@selector(croperViewScale:)];
croperViewGessture.delegate=self;
[croperView addGestureRecognizer:croperViewGessture];
-(void)CanvasScale:(id)sender
{
if([(UIPinchGestureRecognizer *)sender state]==UIGestureRecognizerStateBegan)
{
if ([sender numberOfTouches] == 2) {
_pntOrig[0] = [(UIPinchGestureRecognizer *)sender locationOfTouch:0 inView:cropedAngle];
_pntOrig[1] = [(UIPinchGestureRecognizer *)sender locationOfTouch:1 inView:cropedAngle];
} else {
_pntOrig[0] = [(UIPinchGestureRecognizer *)sender locationInView:cropedAngle];
_pntOrig[1] = _pntOrig[0];
}
_lenOrigX = fabs(_pntOrig[1].x - _pntOrig[0].x);
_lenOrigY = fabs(_pntOrig[1].y - _pntOrig[0].y);
_xScale = 1.0;
_yScale = 1.0;
}
if ([(UIPinchGestureRecognizer *)sender state] == UIGestureRecognizerStateChanged) {
if ([sender numberOfTouches] == 2) {
CGPoint pntNew[2];
pntNew[0] = [(UIPinchGestureRecognizer *)sender locationOfTouch:0 inView:cropedAngle];
pntNew[1] = [(UIPinchGestureRecognizer *)sender locationOfTouch:1 inView:cropedAngle];
CGFloat lenX = fabs(pntNew[1].x - pntNew[0].x);
CGFloat lenY = fabs(pntNew[1].y - pntNew[0].y);
CGFloat dX = fabs(lenX - _lenOrigX);
CGFloat dY = fabs(lenY - _lenOrigY);
CGFloat tot = dX + dY;
CGFloat pX = dX / tot;
CGFloat pY = dY / tot;
CGFloat scale = [(UIPinchGestureRecognizer *)sender scale];
CGFloat dscale = scale - 1.0;
_xScale = dscale * pX + 1;
_yScale = dscale * pY + 1;
}
}
CGAffineTransform transform = cropedAngle.transform;
CGAffineTransform newTarnsform = CGAffineTransformScale(transform, _lenOrigX, _lenOrigY);
[cropedAngle setTransform:newTarnsform];
}
但问题是当我做Zoomin或Zoomout时,然后在屏幕上查看传播并禁用后请查看我的代码并告诉我有什么问题。
请帮我解决这个问题 我提前感谢你。
答案 0 :(得分:2)
我为UIPinchGestureRecognizer
编写了自己的自定义扩展程序,除了正常xScale
之外,还提供了yScale
和scale
。这是UIPinchGestureRecognizer
的替代品。您现在可以选择查看普通scale
或新xScale
和yScale
。相应地使用这些值可根据用户执行捏合手势的方式缩放视图。
RMPinchGestureRecognizer.h
#import <Foundation/Foundation.h>
#import <UIKit/UIGestureRecognizerSubclass.h>
@interface RMPinchGestureRecognizer : UIPinchGestureRecognizer {
CGPoint _pntOrig[2];
CGFloat _lenOrigX;
CGFloat _lenOrigY;
CGFloat _xScale;
CGFloat _yScale;
}
@property (nonatomic, readonly) CGFloat xScale;
@property (nonatomic, readonly) CGFloat yScale;
@end
RMPinchGestureRecognizer.m
#import "RMPinchGestureRecognizer.h"
@implementation RMPinchGestureRecognizer
@synthesize xScale = _xScale;
@synthesize yScale = _yScale;
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
[super touchesMoved:touches withEvent:event];
if (self.state == UIGestureRecognizerStateChanged) {
if ([self numberOfTouches] == 2) {
CGPoint pntNew[2];
pntNew[0] = [self locationOfTouch:0 inView:self.view];
pntNew[1] = [self locationOfTouch:1 inView:self.view];
CGFloat lenX = fabs(pntNew[1].x - pntNew[0].x);
CGFloat lenY = fabs(pntNew[1].y - pntNew[0].y);
CGFloat dX = fabs(lenX - _lenOrigX);
CGFloat dY = fabs(lenY - _lenOrigY);
CGFloat tot = dX + dY;
CGFloat pX = dX / tot;
CGFloat pY = dY / tot;
CGFloat scale = [self scale];
CGFloat dscale = scale - 1.0;
_xScale = dscale * pX + 1;
_yScale = dscale * pY + 1;
}
}
}
- (void)setState:(UIGestureRecognizerState)state {
if (state == UIGestureRecognizerStateBegan) {
if ([self numberOfTouches] == 2) {
_pntOrig[0] = [self locationOfTouch:0 inView:self.view];
_pntOrig[1] = [self locationOfTouch:1 inView:self.view];
} else {
_pntOrig[0] = [self locationInView:self.view];
_pntOrig[1] = _pntOrig[0];
}
_lenOrigX = fabs(_pntOrig[1].x - _pntOrig[0].x);
_lenOrigY = fabs(_pntOrig[1].y - _pntOrig[0].y);
_xScale = 1.0;
_yScale = 1.0;
}
[super setState:state];
}
@end
答案 1 :(得分:0)
看看HERE
您的专家CGAffineTransformScale(transform, scaleTemp,scaleTemp);
使用相同的scaleTemp
变量来修改转换的x和y值。