简体   繁体   中英

UILongPressGestureRecognizer is not working

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!

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;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM