UILongPressGestureRecognizer

UILongPressGestureRecognizer

我正在尝试创建一个应用,在该应用中,可以在触发UILongPressGestureRecognizer手势时拖放UIButton。

我有:

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

最佳答案

您尝试过recognizer.view吗?

关于iphone - UILongPressGestureRecognizer,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/6040372/

10-13 03:50