当操作正在进行时使用 UIPinchGestureRecognizer 缩放 ImageView 时,图像“尝试”保持在初始 frame.origin。
即使在平移手势将图像移动到另一个位置之后,捏合也会立即将其移回初始位置。
这个公然复制的 Apple Touches 示例应用程序的所有代码,用于演示手势。
下面是 pinch 的代码,我在 github 上推送了一个示例应用程序来展示这种行为:https://github.com/atokubi/TestImageManipulation
提前感谢您的帮助。
- (IBAction)scaleItemUIPinchGestureRecognizer *)gestureRecognizer {
[self adjustAnchorPointForGestureRecognizer:gestureRecognizer];
if ([gestureRecognizer state] == UIGestureRecognizerStateBegan || [gestureRecognizer state] == UIGestureRecognizerStateChanged) {
[gestureRecognizer view].transform = CGAffineTransformScale([[gestureRecognizer view] transform], [gestureRecognizer scale], [gestureRecognizer scale]);
[gestureRecognizer setScale:1];
}
}
- (void)adjustAnchorPointForGestureRecognizerUIGestureRecognizer *)gestureRecognizer{
if (gestureRecognizer.state == UIGestureRecognizerStateBegan) {
UIView *piece = gestureRecognizer.view;
CGPoint locationInView = [gestureRecognizer locationInView:piece];
CGPoint locationInSuperview = [gestureRecognizer locationInView:piece.superview];
piece.layer.anchorPoint = CGPointMake(locationInView.x / piece.bounds.size.width, locationInView.y / piece.bounds.size.height);
piece.center = locationInSuperview;
}
}
如果你想正确地重新居中图像,你可以使用这个方法:
// To re-center the image after zooming in and zooming out
- (void)centerViewContents
{
CGSize boundsSize = self.bounds.size;
CGRect contentsFrame = self.imageView.frame;
if (contentsFrame.size.width < boundsSize.width)
{
contentsFrame.origin.x = (boundsSize.width - contentsFrame.size.width) / 2.0f;
}
else
{
contentsFrame.origin.x = 0.0f;
}
if (contentsFrame.size.height < boundsSize.height)
{
contentsFrame.origin.y = (boundsSize.height - contentsFrame.size.height) / 2.0f;
}
else
{
contentsFrame.origin.y = 0.0f;
}
self.imageView.frame = contentsFrame;
}
在你的“adjustAnchorPointForGestureRecognizer”中添加这个方法,方法如下:
- (void)adjustAnchorPointForGestureRecognizerUIGestureRecognizer *)gestureRecognizer
{
if (gestureRecognizer.state == UIGestureRecognizerStateBegan) {
UIView *piece = gestureRecognizer.view;
CGPoint locationInView = [gestureRecognizer locationInView:piece];
CGPoint locationInSuperview = [gestureRecognizer locationInView:piece.superview];
piece.layer.anchorPoint = CGPointMake(locationInView.x / piece.bounds.size.width, locationInView.y / piece.bounds.size.height);
piece.center = locationInSuperview;
[self centerViewContents];
}
}
我下载了你的代码并实现了这个。它有效。
如果有帮助请告诉我。
关于ios - 在捏合时,即使在平移到不同位置后, ImageView 也会移回原始帧的原点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21764280/
欢迎光临 OStack程序员社区-中国程序员成长平台 (https://ostack.cn/) | Powered by Discuz! X3.4 |