UILongPressGestureRecognizer

时间:2011-05-18 05:45:30

标签: iphone objective-c ios

我正在尝试创建一个应用程序,当UILongPressGestureRecognizer手势被触发时,UIButtons可以被拖放。

我有:

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

并且

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

一个按钮(即ivar button)可以很好地工作。如何向handleLongPress函数发送正在按下的当前按钮?换句话说,我想在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;
    }   
}

2 个答案:

答案 0 :(得分:12)

你试过recognizer.view吗?

答案 1 :(得分:-2)

您必须将所有按钮循环到handleLongPress中。 使用locationInView和pointInside:withEvent:确定按下手指的按钮。