自定义快速相机圈触摸响应

时间:2016-07-09 22:12:53

标签: ios xcode swift camera

我正在尝试在我的自定义相机视图中添加一个圆圈。我有一个问题是将其置于我的“触摸点”中,因为当我触摸屏幕时它会将我的圆圈添加到左上角,但是当我更换时在arcCenter旁边的“centerPoint”,在第二行代码中有一些CGPoint(// Some constraints),它将我的点添加到正确的CGPoint。

到目前为止,这是我的代码:

func pointInCamera(centerPoint:CGPoint){


        let circlePath = UIBezierPath(arcCenter: centerPoint, radius: CGFloat(30), startAngle: CGFloat(0), endAngle:CGFloat(M_PI * 2), clockwise: true)//This "centerPoint" is the problem I guess             
        let shapeLayer = CAShapeLayer()
        shapeLayer.path = circlePath.CGPath

        shapeLayer.fillColor = UIColor(red: 240/255, green: 240/255, blue: 240/255, alpha: 0.3).CGColor
        shapeLayer.strokeColor = UIColor(red: 0, green: 191/255, blue: 1, alpha: 0.9).CGColor
        shapeLayer.lineWidth = 2.0

        view.layer.addSublayer(shapeLayer)

    }

    //Focus camera

    override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
        let screenSize = cameraView.bounds.size
        if let touchPoint = touches.first {
            let x = touchPoint.locationInView(cameraView).y / screenSize.height
            let y = 1.0 - touchPoint.locationInView(cameraView).x / screenSize.width
            let focusPoint = CGPoint(x: x, y: y)

            let backCamera = AVCaptureDevice.defaultDeviceWithMediaType(AVMediaTypeVideo)

            if let device = backCamera {
                do {
                    try device.lockForConfiguration()
                    device.focusPointOfInterest = focusPoint

                    pointInCamera(focusPoint)
                    device.focusMode = .AutoFocus
                    device.exposurePointOfInterest = focusPoint
                    device.exposureMode = AVCaptureExposureMode.ContinuousAutoExposure
                    device.unlockForConfiguration()


                }
                catch {
                    // just ignore
                }
            }
        }
    }

1 个答案:

答案 0 :(得分:1)

我创建了这个函数并将其添加到我的touchesBegan函数中:

func pointInCamera(centerPoint:CGPoint){

        let circlePath = UIBezierPath(arcCenter: centerPoint, radius: CGFloat(30), startAngle: CGFloat(0), endAngle:CGFloat(M_PI * 2), clockwise: true)

        let shapeLayer = CAShapeLayer()
        shapeLayer.path = circlePath.CGPath

        shapeLayer.fillColor = UIColor(red: 240/255, green: 240/255, blue: 240/255, alpha: 0.3).CGColor
        shapeLayer.strokeColor = UIColor(red: 0, green: 191/255, blue: 1, alpha: 0.9).CGColor
        shapeLayer.lineWidth = 2.0

        view.layer.addSublayer(shapeLayer)

        delay(0.5) { 
            shapeLayer.removeFromSuperlayer()
        }

    }