UIImagePickerController with cameraOverlayView focusses when button in overlay is tapped

烈酒焚心 提交于 2019-12-10 21:15:17

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!