3

I'm new to Objective C and it's my first question I'm posting here so this is probably the easiest question in the world to answer, but I couldn't figure this one out.

I am trying to create an iOS app that uses the camera. I'm using the UIImagePickerController class to display the camera and take pictures, but I've created a custom UIView that contains a bunch of UIButton instances which overlay the camera preview frame, using the cameraOverlayView property. I'm using the a method to put the camera on screen with this code:

- (BOOL) startCameraControllerFromViewController: (UIImagePickerController*) imagePickerController
                               usingDelegate: (id <UIImagePickerControllerDelegate,
                                               UINavigationControllerDelegate>) imagePickerControllerDelegate {

if (([UIImagePickerController isSourceTypeAvailable:
      UIImagePickerControllerSourceTypeCamera] == NO)
    || (imagePickerController == nil)
    || (imagePickerControllerDelegate == nil))
    return NO;

imagePickerController.sourceType = UIImagePickerControllerSourceTypeCamera;

imagePickerController.mediaTypes = [[NSArray alloc] initWithObjects: (NSString *) kUTTypeImage, nil];

imagePickerController.allowsEditing = NO;
imagePickerController.showsCameraControls = NO;
imagePickerController.wantsFullScreenLayout = YES;

imagePickerController.delegate = imagePickerControllerDelegate;

self.cameraOverlayViewController = [[CameraOverlayViewController alloc] initWithImagePickerControllerDelegate:imagePickerController nibName:@"CameraOverlayView" bundle:nil];
self.cameraOverlayViewController.view.frame = CGRectMake(0, 0, imagePickerController.view.frame.size.width, imagePickerController.view.frame.size.height);
imagePickerController.cameraOverlayView = self.cameraOverlayViewController.view;

[self presentViewController:imagePickerController animated:self.showCameraAnimation completion:^(void) {
    [self.view bringSubviewToFront:self.cameraOverlayViewController.view];
}];

return YES;

}

When I press a button that is located on top of the camera preview window, the corresponding IBAction gets triggered, but the underlaying camera view also gets tapped and refocusses. So every time I press a button I have to manually refocus the camera preview before I can take a picture. As you can see I've tried to use bringSubViewToFront:, but that doesn't seem to work.

How do I prevent the camera from focussing when I tap a button on the cameraOverlayView?

Thnx for the help!

Nelis86
  • 51
  • 1
  • 6
  • will this work? [imagePickerController.view bringSubviewToFront:self.cameraOverlayViewController.view]; – jithinroy Nov 14 '12 at 09:23

1 Answers1

0

Add this to your code:hitTest:withEvent:

- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event {
    UIView *hittedView = [super hitTest:point withEvent:event];
    return hittedView == self.button ? hittedView : nil;
}

I refered to this: UserInteraction enable for subview only. I've tested the code. And it works!

Community
  • 1
  • 1
sunkehappy
  • 8,970
  • 5
  • 44
  • 65
  • Hmmm, [imagePickerController.view setUserInteractionEnabled:NO] does not seem to do anything. I can still tap to focus. – Nelis86 Nov 19 '12 at 08:51
  • I found this topic: http://iphonedevsdk.com/forum/iphone-sdk-development/76953-tap-to-focus-in-uiimagepickercontroller.html It basically says you have to add the overlay view as a subview to the UIImagePickerController view. The problem is that this disables tap to focus. I need tap to focus, so this isn't the right solution for me, but perhaps someone else may find this useful. – Nelis86 Nov 19 '12 at 09:28