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

853 views Asked by At

In my application, I added a cameraOverlayView to the camerapicker. It contains three subviews, two UIButtons and one UIImageView. The image is draggable. The buttons are used to change the image to another one.

When I touch a button, or the image, the tap-to-focus rect of the camera will always be displayed beneath (and the actual focus will change). Setting exclusiveTouch to YES on all three subviews doesn't change the behaviour.

Any idea, how i can stop the camerapicker from getting the touches of my buttons / subviews of my overlayview (but still getting the touches for touch-to-focus, switching cameras etc.)?

To give some more information. I'm using the following code, to allow camera interaction while having an overlayview, which subviews still get the touches.

// 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

There are 1 answers

0
Byte On

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 :(