Ipad 从两个手指的中心缩放UIImageView,并单击按钮

Ipad 从两个手指的中心缩放UIImageView,并单击按钮,ipad,uipinchgesturerecognizer,Ipad,Uipinchgesturerecognizer,我有一个图像视图,想添加一些自定义捏手势识别器。我可以缩放图像视图,但问题是,它不能从两个手指的中心放大 如何从两个手指的中心进行缩放?这就是我目前正在做的(在viewDidLoad中) 这是夹点法的代码 - (IBAction)handlePinch:(UIPinchGestureRecognizer *)recognizer { recognizer.view.transform = CGAffineTransformScale(recognizer.view.transfor

我有一个图像视图,想添加一些自定义捏手势识别器。我可以缩放图像视图,但问题是,它不能从两个手指的中心放大

如何从两个手指的中心进行缩放?这就是我目前正在做的(在
viewDidLoad
中)

这是夹点法的代码

- (IBAction)handlePinch:(UIPinchGestureRecognizer *)recognizer
{  

    recognizer.view.transform = CGAffineTransformScale(recognizer.view.transform, recognizer.scale, recognizer.scale);
    recognizer.scale = 1;

}
代表们…现在我更新我的问题。。 我想在点击按钮时缩放图像..我正在做的是

-(iAction)缩放图像:(id)发送方 {


}

问题在于,您所做的只是缩放视图

不尝试将缩放平移到两个手指点的中心

所以抓住两个指尖。计算出他们的中心并添加一个翻译

请注意,下面的代码将在原则上显示您需要做什么,我可以保证它将需要被修复,因为仿射变换在实践中不是微不足道的,我不是您的错误修复程序

- (IBAction)handlePinch:(UIPinchGestureRecognizer *)recognizer
{
    UIView *view = recognizer.view;

    CGRect frame = view.bounds;

    //you may wish to get more accurate by querying and calculating against the touches via 
    //- (NSUInteger)numberOfTouches;
    //- (CGPoint)locationOfTouch:(NSUInteger)touchIndex inView:(UIView*)view;
    CGPoint nominalCenter = [recognizer locationInView:view];

    CGFloat deltaFromCenterX = CGRectGetMidX(frame) - nominalCenter.x;

    CGFloat deltaFromCenterY = CGRectGetMidY(frame) - nominalCenter.y;

    currentScale = currentScale * recognizer.scale;

    CGAffineTransform scale = CGAffineTransformMakeScale(currentScale, currentScale);

    CGAffineTransform translate = CGAffineTransformMakeTranslation(deltaFromCenterX, deltaFromCenterY);

    //possibly its going to be scale then translate instead
    CGAffineTransform final = CGAffineTransformConcat(translate, scale);

    recognizer.view.transform = final;
    recognizer.scale = 1;
}

问题是,您所做的只是缩放视图

不尝试将缩放平移到两个手指点的中心

所以抓住两个指尖。计算出他们的中心并添加一个翻译

请注意,下面的代码将在原则上显示您需要做什么,我可以保证它将需要被修复,因为仿射变换在实践中不是微不足道的,我不是您的错误修复程序

- (IBAction)handlePinch:(UIPinchGestureRecognizer *)recognizer
{
    UIView *view = recognizer.view;

    CGRect frame = view.bounds;

    //you may wish to get more accurate by querying and calculating against the touches via 
    //- (NSUInteger)numberOfTouches;
    //- (CGPoint)locationOfTouch:(NSUInteger)touchIndex inView:(UIView*)view;
    CGPoint nominalCenter = [recognizer locationInView:view];

    CGFloat deltaFromCenterX = CGRectGetMidX(frame) - nominalCenter.x;

    CGFloat deltaFromCenterY = CGRectGetMidY(frame) - nominalCenter.y;

    currentScale = currentScale * recognizer.scale;

    CGAffineTransform scale = CGAffineTransformMakeScale(currentScale, currentScale);

    CGAffineTransform translate = CGAffineTransformMakeTranslation(deltaFromCenterX, deltaFromCenterY);

    //possibly its going to be scale then translate instead
    CGAffineTransform final = CGAffineTransformConcat(translate, scale);

    recognizer.view.transform = final;
    recognizer.scale = 1;
}

这也会起作用。只需将图像移动到收缩的中心,缩放它,然后在一次变换中返回到其位置

- (void) pinch:(UIPinchGestureRecognizer *) recognizer {

    CGPoint anchor = [recognizer locationInView:imageToScale];
    anchor = CGPointMake(anchor.x - imageToScale.bounds.size.width/2, anchor.y-imageToScale.bounds.size.height/2);

    CGAffineTransform affineMatrix = imageToScale.transform;
    affineMatrix = CGAffineTransformTranslate(affineMatrix, anchor.x, anchor.y);
    affineMatrix = CGAffineTransformScale(affineMatrix, [recognizer scale], [recognizer scale]);
    affineMatrix = CGAffineTransformTranslate(affineMatrix, -anchor.x, -anchor.y);
    imageToScale.transform = affineMatrix;

    [recognizer setScale:1];
}

这也会起作用。只需将图像移动到收缩的中心,缩放它,然后在一次变换中返回到其位置

- (void) pinch:(UIPinchGestureRecognizer *) recognizer {

    CGPoint anchor = [recognizer locationInView:imageToScale];
    anchor = CGPointMake(anchor.x - imageToScale.bounds.size.width/2, anchor.y-imageToScale.bounds.size.height/2);

    CGAffineTransform affineMatrix = imageToScale.transform;
    affineMatrix = CGAffineTransformTranslate(affineMatrix, anchor.x, anchor.y);
    affineMatrix = CGAffineTransformScale(affineMatrix, [recognizer scale], [recognizer scale]);
    affineMatrix = CGAffineTransformTranslate(affineMatrix, -anchor.x, -anchor.y);
    imageToScale.transform = affineMatrix;

    [recognizer setScale:1];
}

请向我们提供
pinch:
方法的实际实现。您添加手势识别器的代码不相关。请将您评论中的代码包含在原始问题中。使用编辑链接。我更新了问题…请看一看..than请为我们提供
pinch:
方法的实际实现。您添加手势识别器的代码不相关。请将您评论中的代码包含在原始问题中。使用编辑链接。我更新了问题…请看一看..答案是..thanx…AdjustAnchorpointForgestureRecogniter帮助…获得了正确的缩放效果..thanx很多Odrakir…现在的问题是,我想通过点击按钮缩放图像(从图像中心)是否可以使用捏手势识别器??我正在做的是有问题的更新…这个缩放现在让我很痛苦…我不明白你说的“按按钮单击缩放图像”是什么意思。实际上,现在我用两个手指缩放图像视图,这与按按钮单击的功能相同(指按按钮单击后,我要按特定中心缩放图像视图)。我成功地使用上述代码进行了缩放,但我希望在单击按钮后使用指定的中心缩放图像。您只需将定位点设置为特定的中心,而不是位置查看,并将比例设置为您想要缩放的任何比例,而不是[识别器比例]这就是问题所在…使用view.transform也会调整我的子视图的大小。。。有没有办法防止这种情况发生?thanx的回答是…AdjustAnchorPointForgestureRecogener帮助…获得了正确的缩放效果..thanx很多Odrakir…现在的问题是,我想通过点击按钮(从图像中心)来缩放图像,用捏手势识别器可以吗??我正在做的是有问题的更新…这个缩放现在让我很痛苦…我不明白你说的“按按钮单击缩放图像”是什么意思。实际上,现在我用两个手指缩放图像视图,这与按按钮单击的功能相同(指按按钮单击后,我要按特定中心缩放图像视图)。我成功地使用上述代码进行了缩放,但我希望在单击按钮后使用指定的中心缩放图像。您只需将定位点设置为特定的中心,而不是位置查看,并将比例设置为您想要缩放的任何比例,而不是[识别器比例]这就是问题所在…使用view.transform也会调整我的子视图的大小。。。有没有办法防止这种情况发生?