0ndre_ 0ndre_ - 4 months ago 22
Swift Question

Custom swift camera circle touch response

I am trying to add a circle to my custom camera view.I have a problem to center it to my "touch point", because it adds my circle to the top left corner when I touch the screen, but when I replace "centerPoint" next to arcCenter with some CGPoint(//Some constrains) in the second line of code it adds my point to the right CGPoint.

This is my code so far:

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

Answer

I Created this function und added it to my touchesBegan function:

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

    }