I have tried many things but could not resolve this issue. I have buttons on top of AVCaptureVideoPreviewLayer defined as follow:
AVCaptureVideoPreviewLayer *previewLayer = (AVCaptureVideoPreviewLayer *)self.previewView.layer;
For the buttons, I defined a gesture recognizer for dragging:
UIPanGestureRecognizer *focPanRecognizer;
focPanRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(wasDragged:)];
[focPanRecognizer setMinimumNumberOfTouches:1];
[focPanRecognizer setMaximumNumberOfTouches:1];
focPanRecognizer.cancelsTouchesInView = YES;
[self.focusButton addGestureRecognizer:focPanRecognizer];
[self.focusButton setUserInteractionEnabled:YES];
The dragging is handled by the following method:
- (void)wasDragged:(UIPanGestureRecognizer *)recognizer
{
if (recognizer.state == UIGestureRecognizerStateChanged ||
recognizer.state == UIGestureRecognizerStateEnded) {
UIButton *draggedButton = (UIButton *) recognizer.view;
CGPoint translation = [recognizer translationInView:self.previewView];
CGRect newButtonFrame = draggedButton.frame;
newButtonFrame.origin.x += translation.x;
newButtonFrame.origin.y += translation.y;
draggedButton.frame = newButtonFrame;
[recognizer setTranslation:CGPointZero inView:self.previewView];
}
}
When I drag the button, it drags properly if I close the camera lens. If the camera lens is open, the button immediately jumps back to its original location.
What could be the problem? Helps is much appreciated.