UIPanGestureRecogniser 的问题

发布于 2024-12-18 19:59:32 字数 2087 浏览 1 评论 0原文

使用 XCode 4.2 并尝试掌握 UIGestureRecognisers。到目前为止,一切似乎都进展顺利,但仍然存在一些问题。

当我使用滑动手势识别器时,一切都很好,它可以识别所有不同方向的滑动,并且会连续这样做。我现在的问题是,当使用平移手势识别器时,它可以很好地识别第一次平移滑动,但随后拒绝接受任何进一步的手势。所以我可以根据需要移动物品一次,但之后就什么也做不了。

我将我的手势设置如下:

UIPanGestureRecognizer *panBody = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panBody:)];
[bodyGestureView addGestureRecognizer:panBody];

然后这是我的“panBody”方法来处理这一切:

- (void)panBody:(UIPanGestureRecognizer *)recognizer
{
CGPoint translate = [recognizer translationInView:self.view];

CGRect bodyPanelFrame = bodyPanel.frame;
bodyPanelFrame.origin.x += translate.x;
bodyPanelFrame.origin.y += translate.y;
recognizer.view.frame = bodyPanelFrame;

CGRect topPanelFrame = topPanel.frame;
topPanelFrame.origin.x += translate.x;
topPanelFrame.origin.y += translate.y;
recognizer.view.frame = topPanelFrame;

CGRect sidePanelFrame = sidePanel.frame;
sidePanelFrame.origin.x += translate.x;
sidePanelFrame.origin.y += translate.y;
recognizer.view.frame = sidePanelFrame;

NSLog(@"Panning");

if (recognizer.state == UIGestureRecognizerStateEnded)
{
    bodyPanel.frame = bodyPanelFrame;

    if((topPanel.frame.origin.x + translate.x) <= 193)
    {
        topPanel.frame = CGRectMake(topPanelFrame.origin.x, topPanel.frame.origin.y, topPanel.frame.size.width, topPanel.frame.size.height);
    }
    else
    {
        topPanel.frame = CGRectMake(193, 0, topPanel.frame.size.width, topPanel.frame.size.height);
        NSLog(@"Top panel not in frame");
    }

    if((sidePanel.frame.origin.y + translate.y) < 57)
    {
        sidePanel.frame = CGRectMake(sidePanel.frame.origin.x, sidePanelFrame.origin.y, sidePanel.frame.size.width, sidePanel.frame.size.height);
    }
    else
    {
        sidePanel.frame = CGRectMake(0, 56, sidePanel.frame.size.width, sidePanel.frame.size.height);
        NSLog(@"Side panel not in frame");
    }
}
}

bodyPanel、topPanel 和 sidePanel 是链接到覆盖在我的界面顶部的 UIView 的 IBOutlet。xib

如果有人可以阐明此信息,那太好了,因为我完全不知道发生了什么!

谢谢,

马特

Working with XCode 4.2 and trying to get to grips with the UIGestureRecognisers. All seems to be going fairly well so far, but am still having a few issues.

When I was using the Swipe gesture recognisers, everything was fine, it would recognise swipes in all different directions and would do so continuously. My problem now is, when using the pan gesture recogniser, it recognises the first pan swipe fine, but then just refuses to accept any further gestures. So I can move items about once as needed, but after that, can do nothing.

I set my gesture up as follows:

UIPanGestureRecognizer *panBody = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panBody:)];
[bodyGestureView addGestureRecognizer:panBody];

Then this is my 'panBody' method that handles it all:

- (void)panBody:(UIPanGestureRecognizer *)recognizer
{
CGPoint translate = [recognizer translationInView:self.view];

CGRect bodyPanelFrame = bodyPanel.frame;
bodyPanelFrame.origin.x += translate.x;
bodyPanelFrame.origin.y += translate.y;
recognizer.view.frame = bodyPanelFrame;

CGRect topPanelFrame = topPanel.frame;
topPanelFrame.origin.x += translate.x;
topPanelFrame.origin.y += translate.y;
recognizer.view.frame = topPanelFrame;

CGRect sidePanelFrame = sidePanel.frame;
sidePanelFrame.origin.x += translate.x;
sidePanelFrame.origin.y += translate.y;
recognizer.view.frame = sidePanelFrame;

NSLog(@"Panning");

if (recognizer.state == UIGestureRecognizerStateEnded)
{
    bodyPanel.frame = bodyPanelFrame;

    if((topPanel.frame.origin.x + translate.x) <= 193)
    {
        topPanel.frame = CGRectMake(topPanelFrame.origin.x, topPanel.frame.origin.y, topPanel.frame.size.width, topPanel.frame.size.height);
    }
    else
    {
        topPanel.frame = CGRectMake(193, 0, topPanel.frame.size.width, topPanel.frame.size.height);
        NSLog(@"Top panel not in frame");
    }

    if((sidePanel.frame.origin.y + translate.y) < 57)
    {
        sidePanel.frame = CGRectMake(sidePanel.frame.origin.x, sidePanelFrame.origin.y, sidePanel.frame.size.width, sidePanel.frame.size.height);
    }
    else
    {
        sidePanel.frame = CGRectMake(0, 56, sidePanel.frame.size.width, sidePanel.frame.size.height);
        NSLog(@"Side panel not in frame");
    }
}
}

bodyPanel, topPanel and sidePanel are IBOutlets linked to UIView's overlayed across the top of my interface .xib

If anybody could shed any light on this information, that would be great cause I have absolutely no idea what is going on!!

Thanks,

Matt

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

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

发布评论

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

评论(1

吲‖鸣 2024-12-25 19:59:32

检查这一点

if (recognizer.state == UIGestureRecognizerStateChanged)

首先,我会在进行翻译之前 (还有许多其他可能的状态,这些状态不会证明您采取任何行动是合理的)。另外,我会在每次回调时重置翻译,因为您正在使用 UIPanGestureRecognizer 方法累积它们。

- (void)setTranslation:(CGPoint)translation inView:(UIView *)view

如果手势识别器停止,则可能是另一个手势识别器正在干扰它。您那里还有活动的 UISwipeGestureRecognizer 吗?如果是这样,您可能应该停用其中之一。您还可以查看此方法

- (void)requireGestureRecognizerToFail:(UIGestureRecognizer *)otherGestureRecognizer

,它允许您指定应优先考虑哪个识别器。

First I would check that

if (recognizer.state == UIGestureRecognizerStateChanged)

before doing your translations (there are many other possible states which would not justify you taking any action). Also I would reset the translation at every callback given you are accumulating them using the UIPanGestureRecognizer method

- (void)setTranslation:(CGPoint)translation inView:(UIView *)view

If the gesture recognizer stops it might be that another gesture recognizer is interfering with it. Do you still have an active UISwipeGestureRecognizer there? If so you should probably deactivate one of them. You can also look at this method

- (void)requireGestureRecognizerToFail:(UIGestureRecognizer *)otherGestureRecognizer

which allows you to specify which recognizer should be given priority.

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