All I want is to move the UIPopover view under the finger as it moves along the screen. But, that should only happen when I tap on top bar (navigation) and move the finger.
Problem I am facing with attached sample project (~20 KB size) is..
1. Popover is moving fine as I move window in horizontal direction but has issue when this movement is vertical or combined with vertical direction. Vertical direction movement is delayed till my finger crosses half past the popover view. (Note: my popover view is at the top of window in start and I tap & move finger at top bar of popover)
2. Popover moves when I tap and move the finger anywhere in UIPopover, not just the top bar.
...
Below code, ABPopViewController is inherited from UIViewController, nothing in it as of now.
Code snippet related to popover creating and gesture handling is..
Code:
- (IBAction)tapAction:(id)sender {
if (ABPopoverVC == nil) {
// View controller to come up in popover.
ABPopViewController *popViewController = [[ABPopViewController alloc]
initWithNibName:@"ABPopViewController" bundle:nil];
// TODO - Need to check if we really require navigation controller. Normal bar could do.
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:popViewController];
// Add gesture to make the view move along with finger.
UIPanGestureRecognizer *gestureRecognizer = [[UIPanGestureRecognizer alloc]
initWithTarget:self action:@selector(handlePanGesture:)];
[navController.view addGestureRecognizer:gestureRecognizer];
[gestureRecognizer release];
popViewController.title = @"My title";
// Initialize the popover controller with nav controller
ABPopoverVC = [[UIPopoverController alloc] initWithContentViewController:navController];
// Don't let it dismiss, unless explicit close.
ABPopoverVC.passthroughViews = [NSArray arrayWithObject:self.view];
rectPopover = tapMeButton.frame; // Rect to present the popover.
[navController release];
[popViewController release];
}
// Present the popover.
[ABPopoverVC presentPopoverFromRect:rectPopover inView:self.view
permittedArrowDirections:0 animated:YES]; // Would Apple accept this? How does other floats a view controller then?
}
- (void) handlePanGesture:(UIGestureRecognizer*)gestureRecognizer {
NSAssert([gestureRecognizer isKindOfClass:[UIPanGestureRecognizer class]], @"Came across unexpected gesture");
UIPanGestureRecognizer *gestureRecog = (UIPanGestureRecognizer *)gestureRecognizer; // Remove compile warning.
if ([gestureRecognizer state] == UIGestureRecognizerStateChanged || [gestureRecognizer state] == UIGestureRecognizerStateBegan) {
// Translation delta to track the movement of finger.
CGPoint translationPoint = [gestureRecog translationInView:[gestureRecog.view superview]];
NSLog(@"translationPoint = %f %f", translationPoint.x, translationPoint.y);
// Update the popover location.
rectPopover.origin.x += translationPoint.x;
rectPopover.origin.y += translationPoint.y;
NSLog(@"newPositionRect = %f %f", rectPopover.origin.x, rectPopover.origin.y);
// ??? - Why do we need to set the translation? If we don't set it, nav bar gets behind the view.
[gestureRecog setTranslation:CGPointZero inView:[gestureRecog.view superview]];
// Present the popover at updated location.
[ABPopoverVC presentPopoverFromRect:rectPopover inView:self.view permittedArrowDirections:0 animated:NO];
}
}
Kindly help. Many thanks.