0

在我的代码中,我需要在呈现弹出框之前获取触摸的坐标。这是代码:

UITapGestureRecognizer *tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapped:)];
[tapRecognizer setNumberOfTapsRequired:1];
[tapRecognizer setDelegate:self];
[self.view addGestureRecognizer:tapRecognizer];
FreeAndNil(tapRecognizer);

ratingSliderViewController = [[CMRatingSliderViewController alloc] init];
ratingPopoverController = [[UIPopoverController alloc] initWithContentViewController:ratingSliderViewController];
[ratingPopoverController setDelegate:self];
[ratingPopoverController setPopoverContentSize:CGSizeMake(360.0, 50.0)];
[self setPopoverController:ratingPopoverController];
[ratingPopoverController presentPopoverFromRect:CGRectMake(latestTouchPoint.x, latestTouchPoint.y, 10.0,10.0) inView:detailView permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];

[self.view removeGestureRecognizer:tapRecognizer];


...

}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [touches anyObject];
    latestTouchPoint = [touch locationInView:self.view];
}

然而,弹出框是在touchesBegan调用之前呈现的。我该如何解决这个问题?我宁愿不使用延迟或在通用touchesBegan方法中运行弹出代码。

4

3 回答 3

2

首先在视图加载时添加手势识别器。

@property (nonatomic, strong) UITapGestureRecognizer *tapRecognizer;


 - (void)viewDidLoad
{
    [super viewDidLoad];
    _tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapped:)];
    [_tapRecognizer setNumberOfTapsRequired:1];
    [self.view addGestureRecognizer:_tapRecognizer];
}

还要确保在点击识别器处理函数中显示弹出框,而不是在声明它时(就像你现在所做的那样)!

-(void) tapRecognizer:(UITapGestureRecognizer *)tabRecognizer{

     CGPoint touchedPoint = [gestureRecognizer locationInView:self.view];
     NSLog(@"Point x %f x %f", aPoint.x, aPoint.y);

     ratingSliderViewController = [[CMRatingSliderViewController alloc] init];
     ratingPopoverController = [[UIPopoverController alloc] initWithContentViewController:ratingSliderViewController];
     [ratingPopoverController setDelegate:self];
     [ratingPopoverController setPopoverContentSize:CGSizeMake(360.0, 50.0)];
     [self setPopoverController:ratingPopoverController];
     [ratingPopoverController presentPopoverFromRect:CGRectMake(touchedPoint,     touchedPoint, 10.0,10.0) inView:detailView   permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
 }
于 2013-05-01T11:53:15.163 回答
0

我在这里找到了解决方案(需要一些 javascript):http: //www.mindfirelabs.com/forum/viewtopic.php? f=10&t=142

于 2013-05-01T15:17:00.980 回答
0

您可以使用此委托方法将委托添加到手势,

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
于 2013-05-01T11:54:11.600 回答