我是Objective C的新手,这是我在这里发布的第一个问题所以这可能是世界上最容易回答的问题,但我无法想出这个问题。
我正在尝试创建一个使用相机的iOS应用。我正在使用UIImagePickerController类来显示相机并拍照,但我创建了一个自定义UIView,它包含一堆UIButton实例,它们使用cameraOverlayView属性覆盖相机预览帧。我正在使用a方法将相机放在屏幕上,并带有以下代码:
- (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;
}
当我按下位于相机预览窗口顶部的按钮时,相应的IBAction会被触发,但是底层相机视图也会被轻拍并重新聚焦。因此,每次按下按钮,我都必须手动重新聚焦相机预览才能拍照。正如你所看到我试图使用bringSubViewToFront:,但这似乎不起作用。
当我点击cameraOverlayView上的按钮时,如何防止相机聚焦?
Thnx的帮助!
答案 0 :(得分:0)
将此添加到您的代码中:hitTest:withEvent:
- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event {
UIView *hittedView = [super hitTest:point withEvent:event];
return hittedView == self.button ? hittedView : nil;
}
我提到了这个: UserInteraction enable for subview only。我已经测试了代码。它有效!