Exlusive touch in cameraoverlay view (of the camerapicker) in iOS?

≯℡__Kan透↙ 提交于 2019-12-06 14:02:49

There are a couple of ways to stop this that I found. None of the ways do exactly what I would like it to do. But tweaking it around should give you something usable.

1.) Disable camera control. cameraPickerController.showsCameraControls = NO; This will ultimately take down the touch to focus from the screen but it would take down other controls too.

2.) Control the hitTest in your overlayView and set those area to nil. This solution is very awkward but it works for me.

This is my overlayView that manages a face mask

- (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;
}

So if you get tweaking, this will work for you. I am still looking for the magic wand that make this work without all the hacks. No sign of it still :(

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