RotationGesture 后平移 UIView 导致视图折叠

发布于 2025-01-04 05:17:46 字数 1650 浏览 0 评论 0原文

我将尝试用文字来描述一些只能用视频来描述的东西。

我创建了一个简单的 iOS 应用程序,其中包含一个包含单个图像视图的故事板。我添加了两个手势识别器:UIPanGestureRecognizer 和 UIRotationGestureRecognizer 及其相应的 IBActions。

当我第一次在模拟器中启动应用程序时,图像视图正确平移。图像视图也可以正确旋转。然而,旋转后,任何后续平移都会失败。当我尝试在旋转后平移时,无论平移的方向如何,图像都会迅速缩放到零并消失,即它崩溃或内爆到消失的点。

手势识别器是使用以下代码创建的。 myImageView 设置为 IBOutlet UIImageView。

UIPanGestureRecognizer *panRec = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(processPan:)];
[myImageView addGestureRecognizer:panRec];
UIRotationGestureRecognizer *rotRec = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(processRotation:)];
[myImageView addGestureRecognizer:rotRec];

我已经尽我所能编写了相关的操作。它们基本上是我在 iOS 文档中找到的方法的轻微修改。这些如下所示。

-(IBAction)processPan:(UIPanGestureRecognizer *)sender
{
    if(sender.state == UIGestureRecognizerStateChanged)
    {
        CGPoint translation = [sender translationInView:self.view];
        CGRect newFrame = myImageView.frame;
        newFrame.origin.x += translation.x; 
        newFrame.origin.y += translation.y;
        myImageView.frame = newFrame;
        [sender setTranslation:CGPointMake(0, 0) inView:self.view];
    }
}

-(IBAction)processRotation:(UIRotationGestureRecognizer *)sender
{
    if(sender.state == UIGestureRecognizerStateChanged)
    {
        myImageView.transform = CGAffineTransformRotate(myImageView.transform, sender.rotation);
        [sender setRotation:0];
    }
}

那么我错过了什么?我是新手,所以希望我的无知能被容忍。

我正在 MacBook 上的 OS X 版本 10.7.3 上运行 Xcode 版本 4.2.1(如果有帮助的话)。非常感谢您花时间阅读我的问题。 Stack Overflow 是一个令人难以置信的资源!

-戴夫

I'm going to try to describe with words something that might only be describable with video.

I have created a simple iOS app with a storyboard containing a single image view. I have added two gesture recognizers: a UIPanGestureRecognizer and a UIRotationGestureRecognizer along with their corresponding IBActions.

When I first start the application in the simulator, the image view pans correctly. The image view also rotates correctly. After a rotation, however, any subsequent pan fails. When I try to pan after a rotation, regardless of the direction of the pan, the image rapidly scales to zero and disappears, i.e., it collapses or implodes to a point that disappears.

The gesture recognizers are created using the following code. myImageView is set up as an IBOutlet UIImageView.

UIPanGestureRecognizer *panRec = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(processPan:)];
[myImageView addGestureRecognizer:panRec];
UIRotationGestureRecognizer *rotRec = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(processRotation:)];
[myImageView addGestureRecognizer:rotRec];

I've written the associated actions as best I know how. They are basically slight modifications of the methods I found in the iOS documentation. These are shown below.

-(IBAction)processPan:(UIPanGestureRecognizer *)sender
{
    if(sender.state == UIGestureRecognizerStateChanged)
    {
        CGPoint translation = [sender translationInView:self.view];
        CGRect newFrame = myImageView.frame;
        newFrame.origin.x += translation.x; 
        newFrame.origin.y += translation.y;
        myImageView.frame = newFrame;
        [sender setTranslation:CGPointMake(0, 0) inView:self.view];
    }
}

-(IBAction)processRotation:(UIRotationGestureRecognizer *)sender
{
    if(sender.state == UIGestureRecognizerStateChanged)
    {
        myImageView.transform = CGAffineTransformRotate(myImageView.transform, sender.rotation);
        [sender setRotation:0];
    }
}

So what am I missing? I am new at this, so hopefully my ignorance will be tolerated.

I am running Xcode version 4.2.1 on OS X version 10.7.3 on a MacBook if that helps. Thank you so much for taking the time to read my question. Stack Overflow is an unbelievable resource!

-Dave

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

渡你暖光 2025-01-11 05:17:46

好吧,我不知道我是否想出了一个解决方案,或者我是否想出了一个拼凑。基本上,平移代码对我不起作用。每当视图旋转或缩放时,平移代码都会严重扭曲或折叠正在平移的视图。我盯着变换矩阵和框架坐标系,直到我几乎失明。

我在第一篇文章中列出的翻译代码基本上是从 Apple 的 iOS 事件处理指南的手势识别器部分中的清单 3-2“处理捏合、平移和双击手势”复制的,所以我认为它可以这对我来说是个窍门。好吧,我最终使用 UIImageView 中心为其编写了自己的代码,并且根本没有弄乱框架。这对我有用。

CGPoint translation = [sender translationInView:self.superview];
CGPoint newCenter = CGPointMake(self.myImageView.center.x + translation.x, self.myImageView.center.y + translation.y);
[self.myImageView setCenter:newCenter];
[sender setTranslation:CGPointMake(0, 0) inView:self.superview];

我使用超级视图作为翻译的参考,以防它被旋转。现在似乎有效了。

这项努力可能揭示了我对框架的理解是不正确的。如果有人能告诉我如何纠正我的理解,我将不胜感激。

-戴夫

Well, I don't know if I've come up with a solution or if I've come up with a kludge. Basically, the pan code wasn't working for me. Any time the view was rotated or scaled, the panning code would seriously distort or collapse the view being translated. I stared at transform matrices and frame coordinate systems until I just about went blind.

The translation code I listed in my first post was basically copied from Listing 3-2, "Handling pinch, pan, and double-tap gestures" from the Gesture Recognizers section out of Apple's Event Handling Guide for iOS, so I figured it would do the trick for me. Well, I ended up writing my own code for it using the UIImageView center and not messing with the frame at all. Here is what worked for me.

CGPoint translation = [sender translationInView:self.superview];
CGPoint newCenter = CGPointMake(self.myImageView.center.x + translation.x, self.myImageView.center.y + translation.y);
[self.myImageView setCenter:newCenter];
[sender setTranslation:CGPointMake(0, 0) inView:self.superview];

I used the superview as a reference for the translation in case it was rotated. It seems to work now.

This effort probably reveals something about how my understanding of frames isn't correct. If someone can tell me how to correct my understanding, I'd appreciate it.

-Dave

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文