拖动后,相机预览图层上的uibutton会跳回到其初始位置(UIPanGestureRecognizer手势)

时间:2016-03-08 07:15:19

标签: ios camera uibutton drag pan

我尝试过很多东西,但无法解决这个问题。我在AVCaptureVideoPreviewLayer上面的按钮定义如下:

AVCaptureVideoPreviewLayer *previewLayer = (AVCaptureVideoPreviewLayer *)self.previewView.layer;

对于按钮,我定义了一个用于拖动的手势识别器:

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];

拖动由以下方法处理:

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

当我拖动按钮时,如果我关闭相机镜头,它会正确拖动。如果相机镜头打开,按钮会立即跳回原位。

可能是什么问题?非常感谢帮助。

1 个答案:

答案 0 :(得分:0)

问题在于布局中使用的约束。更新约束有助于jkanter在iOS >> Dragged View is Jumping Back to Original Position >> Auto Layout Combined with UIPanGestureRecognizer Issue

的答案中所解释的