iOS UiButton UILongPressGestureRecognizer在UITableViewCell中提供错误的视图坐标

时间:2014-01-07 15:45:55

标签: ios ios7 uigesturerecognizer

我遇到了UILongPressGestureRecognizer的一些奇怪问题。

我已将LongPressRecognizer附加到UITableViewCell中的UIButton。我想要做的是获取按钮的矩形,这样如果有人按下此按钮,我就可以显示UIPopOver。

我遇到的问题是我得错了坐标。如果只是使用轻击动作,坐标是正确的。如果我使用长按,则坐标在Y轴上关闭。

长按代码:

- (IBAction)LongPress:(id)sender {

   UILongPressGestureRecognizer* longPress = sender;

   if (longPress.state == UIGestureRecognizerStateEnded) {

       NSLog(@"sender Description %@", [longPress.view description]);

       NSLog(@"coordinates sender bounds %f, %f, %f, %f", longPress.view.frame.origin.x, longPress.view.frame.origin.y, longPress.view.bounds.size.height, longPress.view.bounds.size.width);

       CGRect conv = [longPress.view convertRect:longPress.view.bounds toView:self.view];
       NSLog(@"translated bounds %f %f %f %f", conv.origin.x, conv.origin.y, conv.size.height, conv.size.width);

       popupSelector = [[PopupSelector alloc] initWithStyle:UITableViewStylePlain];
       popupSelector.delegate = self;

       popOverController = [[UIPopoverController alloc] initWithContentViewController:popupSelector];
       [popOverController presentPopoverFromRect:longPress.view.frame inView:longPress.view.superview permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
    }
}

将弹出窗口弹出到屏幕底部的某个位置。日志输出为:

2014-01-08 01:19:36.554 the-app[2081:60b] sender Description <UIButton: 0x168a8170; baseClass = UIButton; frame = (37 40; 31 31); opaque = NO; autoresize = TM+BM; tag = 9; gestureRecognizers = <NSArray: 0x168afcd0>; layer = <CALayer: 0x168a22a0>>
2014-01-08 01:19:36.556 the-app[2081:60b] coordinates sender bounds 37.000000, 40.000000, 31.000000, 31.000000
2014-01-08 01:19:36.557 the-app[2081:60b] translated bounds 583.000000 893.000000 31.000000 31.000000

但是,如果我使用按钮按下动作,我会得到正确的坐标,并且弹出框出现在正确的位置。

- (IBAction)tappedit:(id)sender {

    NSLog(@"sender Description %@", [sender description]);    

    UIButton* button = sender;

        NSLog(@"coordinates sender bounds %f, %f, %f, %f", button.frame.origin.x, button.frame.origin.y, button.bounds.size.height, button.bounds.size.width);

        CGRect conv = [button convertRect:button.bounds toView:self.view];
        NSLog(@"translated bounds %f %f %f %f", conv.origin.x, conv.origin.y, conv.size.height, conv.size.width);

        popupSelector = [[PopupSelector alloc] initWithStyle:UITableViewStylePlain];
        popupSelector.delegate = self;

        popOverController = [[UIPopoverController alloc] initWithContentViewController:popupSelector];
        [popOverController presentPopoverFromRect:button.frame inView:button.superview permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];

}

这使得弹出窗口位于正确的位置。

2014-01-08 01:19:30.054 the-app[2081:60b] sender Description <UIButton: 0x15516170; baseClass = UIButton; frame = (37 40; 31 31); opaque = NO; autoresize = TM+BM; tag = 9; gestureRecognizers = <NSArray: 0x1551fbc0>; layer = <CALayer: 0x15513400>>
2014-01-08 01:19:30.055 the-app[2081:60b] coordinates sender bounds 37.000000, 40.000000, 31.000000, 31.000000
2014-01-08 01:19:30.056 the-app[2081:60b] translated bounds 583.000000 311.000000 31.000000 31.000000

注意:我使用convertRect尝试找到与superview相关的位置并转换边界。无论哪种方式,无论是直接指定视图,还是指定已翻译的边界,它都会显示在错误的位置。

所以...首先,这里发生了什么?其次,如何在使用longpressgesture时获得UIButton的正确矩形?

0 个答案:

没有答案