【问题标题】:UILongPressGestureRecognizerUILongPressGestureRecognizer
【发布时间】:2011-05-18 05:45:29
【问题描述】:

我正在尝试创建一个应用程序,当触发 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;
    }   
}

【问题讨论】:

    标签: iphone objective-c ios


    【解决方案1】:

    你试过recognizer.view吗?

    【讨论】:

      【解决方案2】:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-11-26
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多