UILongPressGestureRecognizer 不工作

发布于 2025-01-02 19:55:58 字数 2271 浏览 0 评论 0原文

我正在尝试创建一个应用程序,当触发 UILongPressGestureRecognizer 手势时,可以在其中拖放 UIButtons。事实上,我的应用程序在任何 iPad 上都运行良好。它仅在低于 iOS 5.0 的 iPhone 中产生问题,因为它在装有 iOS 5.0 的 iPhone 中工作正常。

UILongPressGestureRecognizer * gesture = [[UILongPressGestureRecognizer alloc] initWithTarget: self action: @selector(moveActionGestureRecognizerStateChanged:)];
gesture.minimumPressDuration = 0.5;
gesture.delegate = self;
[self.dragView addGestureRecognizer: gesture];
[gesture release]; 

- (void) moveActionGestureRecognizerStateChanged: (UILongPressGestureRecognizer *) recognizer
{
    switch ( recognizer.state )
    {
        default:
        case UIGestureRecognizerStateFailed:
        {
            dragView.alpha=1.0;
            [dragView release];
            dragView=nil;
            break;
        }
        case UIGestureRecognizerStatePossible:
        {
               dragView.alpha=0.8;
            dragView.frame=CGRectMake(dragView.frame.origin.x, dragView.frame.origin.y, dragView.frame.size.width, dragView.frame.size.height);
        }
        case UIGestureRecognizerStateCancelled:
        {
            dragView.alpha=1.0;
            [dragView release];
            dragView=nil;
            break;
        }            
        case UIGestureRecognizerStateEnded:
        {
            //Set dragView on target position

            break;
        }
        case UIGestureRecognizerStateBegan:
        {
            //NSLog(@"Began");
            dragView.alpha=0.8;        
            dragView.frame=CGRectMake(dragView.frame.origin.x, dragView.frame.origin.y, dragView.frame.size.width, dragView.frame.size.height);
            [self bringSubviewToFront:dragView];
            break;
        }           
        case UIGestureRecognizerStateChanged:
        {
            [self.view bringSubviewToFront:dragView];
            CGPoint offset = [recognizer locationInView: self.scrollView];
            dragView.frame=CGRectMake(offset.x, offset.y, dragView.frame.size.width, dragView.frame.size.height);
        }            
            break;
    }
}

我有 2 台设备:iPhone 3G(装有 iOS 4.2.1)和 iPhone 4(装有 5.0)。此功能在装有 iOS 5.0 的 iPhone 4 中工作正常,但在装有 iOS 4.2.1 的 iPhone 3g 中无法正常工作。有时它可以在 iPhone 3g 中工作,但有时它不调用委托方法。

如果您有任何解决方案,请告诉我。

谢谢!

I am trying to create an app where UIButtons can be draged and dropped when a UILongPressGestureRecognizer gesture is fired. Actually my app is working fine in any iPad. it creates problem in iPhone only and lower than iOS 5.0 menas its work fine in iPhone with iOS 5.0.

UILongPressGestureRecognizer * gesture = [[UILongPressGestureRecognizer alloc] initWithTarget: self action: @selector(moveActionGestureRecognizerStateChanged:)];
gesture.minimumPressDuration = 0.5;
gesture.delegate = self;
[self.dragView addGestureRecognizer: gesture];
[gesture release]; 

- (void) moveActionGestureRecognizerStateChanged: (UILongPressGestureRecognizer *) recognizer
{
    switch ( recognizer.state )
    {
        default:
        case UIGestureRecognizerStateFailed:
        {
            dragView.alpha=1.0;
            [dragView release];
            dragView=nil;
            break;
        }
        case UIGestureRecognizerStatePossible:
        {
               dragView.alpha=0.8;
            dragView.frame=CGRectMake(dragView.frame.origin.x, dragView.frame.origin.y, dragView.frame.size.width, dragView.frame.size.height);
        }
        case UIGestureRecognizerStateCancelled:
        {
            dragView.alpha=1.0;
            [dragView release];
            dragView=nil;
            break;
        }            
        case UIGestureRecognizerStateEnded:
        {
            //Set dragView on target position

            break;
        }
        case UIGestureRecognizerStateBegan:
        {
            //NSLog(@"Began");
            dragView.alpha=0.8;        
            dragView.frame=CGRectMake(dragView.frame.origin.x, dragView.frame.origin.y, dragView.frame.size.width, dragView.frame.size.height);
            [self bringSubviewToFront:dragView];
            break;
        }           
        case UIGestureRecognizerStateChanged:
        {
            [self.view bringSubviewToFront:dragView];
            CGPoint offset = [recognizer locationInView: self.scrollView];
            dragView.frame=CGRectMake(offset.x, offset.y, dragView.frame.size.width, dragView.frame.size.height);
        }            
            break;
    }
}

I have 2 devices iPhone 3G with iOS 4.2.1 and iPhone 4 with 5.0. This functionality is working fine in iPhone 4 with iOS 5.0 but its not working properly in iPhone 3g with iOS 4.2.1. Sometimes it working in iPhone 3g but sometimes its not calling delegate methods.

Let me know if you have any solution for that.

Thanks!

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

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

发布评论

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

评论(1

旧伤还要旧人安 2025-01-09 19:55:58

委托方法根本没有被调用,或者没有选择案例?我不知道这是否真的解决了您的问题,但您应该修复您的 switch (在默认情况下进行战利品):

switch ( recognizer.state )
{
    case someCase:
    {
        // ...
        break;
    }

    default:
        break;
}

Is the delegate method not called at all, or is no case selected? I don't know if this really solves your problem, but you should fix your switch (take a loot at the default case):

switch ( recognizer.state )
{
    case someCase:
    {
        // ...
        break;
    }

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