简体   繁体   English

UILongPressGestureRecognizer

[英]UILongPressGestureRecognizer

I am trying to create an app where UIButtons can be draged and dropped when a UILongPressGestureRecognizer gesture is fired. 我正在尝试创建一个应用程序,当UILongPressGestureRecognizer手势被触发时,UIButtons可以被拖放。

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 ). 这可以通过一个按钮(即ivar button )很好地工作。 How can I send to the handleLongPress function the current button that is being pressed? 如何向handleLongPress函数发送正在按下的当前按钮? In other words, I would like to do something like the following where I pass in sender 换句话说,我想在传递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. 您必须将所有按钮循环到handleLongPress中。 Using locationInView and pointInside:withEvent: to determine which button is under the pressing finger. 使用locationInView和pointInside:withEvent:确定按下手指下面的按钮。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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