问题
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!
回答1:
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!
来源:https://stackoverflow.com/questions/13375568/uiimagepickercontroller-with-cameraoverlayview-focusses-when-button-in-overlay-i