在我的应用程序中,我添加了 cameraOverlayView
到相机选择器。它包含三个 subview ,两个 UIButtons
和一个 UIImageView
.图像是可拖动的。这些按钮用于将图像更改为另一个图像。
当我触摸一个按钮或图像时,相机的点击对焦矩形将始终显示在下方(并且实际焦点会改变)。设置exclusiveTouch
至YES
在所有三个 subview 上都不会改变行为。
任何想法,我怎样才能阻止相机选择器触摸我的覆盖 View 的按钮/ subview (但仍然可以触摸对焦、切换相机等)?
提供更多信息。我正在使用以下代码,以允许在具有覆盖 View 的同时进行相机交互,这些 subview 仍然可以触摸。
// CameraOverlayView.m
[...]
// ignore touches on this view, but not on the subviews
-(id)hitTest:(CGPoint)point withEvent:(UIEvent *)event
{
id hitView = [super hitTest:point withEvent:event];
if (hitView == self) return nil;
else return hitView;
}
最佳答案
我发现有几种方法可以阻止这种情况。没有一种方法能完全按照我的意愿去做。但是调整它应该会给你一些有用的东西。
1.) 禁用相机控制。 cameraPickerController.showsCameraControls = NO;
这最终将取消触摸以从屏幕聚焦,但它也会取消其他控件。
2.) 控制覆盖 View 中的 hitTest 并将这些区域设置为零。这个解决方案很尴尬,但对我有用。
这是我管理 mask 的overlayView
- (id)initWithFrame:(CGRect)frame imagePicker: (UIImagePickerController *) imagepicker{
self = [super initWithFrame:frame];
if (self) {
faceMaskButton = [UIButton buttonWithType:UIButtonTypeCustom];
faceMaskButton.backgroundColor = [UIColor colorWithRed:1 green:1 blue:1 alpha:.2];
faceMaskButton.frame = CGRectMake(10, 10, 70, 35);
faceMaskButton.layer.borderColor = [[UIColor colorWithRed:0 green:0 blue:0 alpha:.6] CGColor];
faceMaskButton.layer.borderWidth = 1;
faceMaskButton.layer.cornerRadius = 17;
[faceMaskButton setImage:[UIImage imageNamed:@"Facemask button"] forState:UIControlStateNormal];
[faceMaskButton addTarget:self action:@selector(facemask) forControlEvents:UIControlEventTouchDown];
faceMaskButton.exclusiveTouch = YES;
faceMaskButton.autoresizingMask = UIViewAutoresizingFlexibleTopMargin|UIViewAutoresizingFlexibleBottomMargin|UIViewAutoresizingFlexibleLeftMargin|UIViewAutoresizingFlexibleRightMargin;
[self addSubview:faceMaskButton];
loadingFacemask = NO;
overlayImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"Camera Face Overlay"]];
overlayImage.frame = CGRectMake((self.bounds.size.width - 400)/2, (self.bounds.size.height - 400)/3, 400, 400);
overlayImage.autoresizingMask = UIViewAutoresizingFlexibleTopMargin|UIViewAutoresizingFlexibleBottomMargin|UIViewAutoresizingFlexibleLeftMargin|UIViewAutoresizingFlexibleRightMargin;
overlayImage.alpha = 0.9f;
[self addSubview:overlayImage];
imagePickerController = imagepicker;
}
return self;
}
- (void) facemask{
if (!loadingFacemask) {
loadingFacemask = YES;
[UIView animateWithDuration:.3 animations:^{
overlayImage.alpha = overlayImage.alpha? 0:1;
faceMaskButton.backgroundColor = [UIColor colorWithRed:1 green:1 blue:1 alpha:1];
} completion:^(BOOL finished) {
faceMaskButton.backgroundColor = [UIColor colorWithRed:1 green:1 blue:1 alpha:.2];
loadingFacemask = NO;
}];
}
}
-(UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event{
if ((self.bounds.size.height - 44)<point.y){
return imagePickerController.view;
}
if (point.x < faceMaskButton.bounds.origin.x + faceMaskButton.bounds.size.width +10 && point.y < faceMaskButton.bounds.origin.y + faceMaskButton.bounds.size.height + 10) {
[self facemask];
return nil;
}
return self;
}
因此,如果您进行调整,这将对您有用。我仍在寻找能够在没有所有黑客攻击的情况下完成这项工作的魔杖。仍然没有迹象:(
关于ios - iOS中相机覆盖 View (相机选择器)的独家触摸?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8898758/