aneuryzm
aneuryzm

Reputation: 64864

How to get touch coordinates before presenting a popover?

In my code I need to get the coordinates of a touch before to present a popover. This is the code:

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

However the popover is presented before the touchesBegan invocation. How can I solve this? I would prefer to not use delays or run the popover code in the generic touchesBegan method.

Upvotes: 0

Views: 788

Answers (3)

Javier Quevedo
Javier Quevedo

Reputation: 2046

First add the gesture recognizer when the view loads.

@property (nonatomic, strong) UITapGestureRecognizer *tapRecognizer;


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

Also make sure that you present the popover inside the tap recognizer handler function instead of when you declare it (as you do now)!

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

Upvotes: 2

aneuryzm
aneuryzm

Reputation: 64864

I've found the solution (requiring some javascript) here: http://www.mindfirelabs.com/forum/viewtopic.php?f=10&t=142

Upvotes: 0

Balu
Balu

Reputation: 8460

you can get by using this delegate method add delegate to gestures,

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch

Upvotes: 0

Related Questions