gpt4 book ai didi

ios - 捏合时,即使在平移到不同位置后, ImageView 也会移回原始帧的原点

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:36:32 25 4
gpt4 key购买 nike

  1. 当操作正在进行时使用 UIPinchGestureRecognizer 缩放 ImageView 时,图像“尝试”保持在初始 frame.origin。

  2. 即使在平移手势将图像移动到另一个位置后,捏合也会立即将其移回初始位置。

  3. 这个用于演示手势的 Apple Touches 示例应用的公然副本的所有代码。

下面是 pinch 的代码,我在 github 上推送了一个示例应用程序来演示这种行为:https://github.com/atokubi/TestImageManipulation

在此先感谢您的帮助。

- (IBAction)scaleItem:(UIPinchGestureRecognizer *)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)adjustAnchorPointForGestureRecognizer:(UIGestureRecognizer *)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)adjustAnchorPointForGestureRecognizer:(UIGestureRecognizer *)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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com