Reputation: 64864
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
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
Reputation: 64864
I've found the solution (requiring some javascript) here: http://www.mindfirelabs.com/forum/viewtopic.php?f=10&t=142
Upvotes: 0
Reputation: 8460
you can get by using this delegate method add delegate to gestures,
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
Upvotes: 0