简体   繁体   中英

UILongPressGestureRecognizer

I am trying to create an app where UIButtons can be draged and dropped when a UILongPressGestureRecognizer gesture is fired.

I have:

UIGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc]
initWithTarget:self action:@selector(handleLongPress:)];

And

- (void)handleLongPress:(UILongPressGestureRecognizer *)recognizer {
    CGPoint location = [recognizer locationInView:self.view];

    switch (recognizer.state) {
        case UIGestureRecognizerStateBegan:
            //NSLog(@"handleLongPress: StateBegan");
            break;
        case UIGestureRecognizerStateChanged:
            if(location.y > 75.0 && location.x > 25 && location.x < 300)
                button.frame = CGRectMake(location.x-25, location.y-15, 50, 30);           
            break;
        case UIGestureRecognizerStateEnded:
            //NSLog(@"handleLongPress: StateEnded");
            break;
        default:
            break;
    }   
}

This works great with one button (namely the ivar button ). How can I send to the handleLongPress function the current button that is being pressed? In other words, I would like to do something like the following where I pass in sender

- (void)handleLongPress:(UILongPressGestureRecognizer *)recognizer:(id)sender {
    CGPoint location = [recognizer locationInView:self.view];

    switch (recognizer.state) {
        case UIGestureRecognizerStateBegan:
            //NSLog(@"handleLongPress: StateBegan");
            break;
        case UIGestureRecognizerStateChanged:
            if(location.y > 75.0 && location.x > 25 && location.x < 300)
                sender.frame = CGRectMake(location.x-25, location.y-15, 50, 30);           
            break;
        case UIGestureRecognizerStateEnded:
            //NSLog(@"handleLongPress: StateEnded");
            break;
        default:
            break;
    }   
}

你尝试过recognizer.view吗?

You'll have to loop through all your button into handleLongPress. Using locationInView and pointInside:withEvent: to determine which button is under the pressing finger.

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