我正在尝试添加tap以将焦点集中到我的自定义相机视图上,我成功地做到了这一点,但现在我需要显示一些内容,以便用户能够知道他实际上在做什么。。。。显示一个用户界面,比如苹果的黄色方块,或者SnapChat的白色圆圈。自定义指示器。。。
这是我的代码:

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

            if let device = captureDevice {
                do {
                    try device.lockForConfiguration()

                    device.focusPointOfInterest = focusPoint
                    //device.focusMode = .continuousAutoFocus
                    device.focusMode = .autoFocus
                    //device.focusMode = .locked
                    device.exposurePointOfInterest = focusPoint
                    device.exposureMode = AVCaptureExposureMode.continuousAutoExposure
                    device.unlockForConfiguration()
                }
                catch {
                    // just ignore
                }
            }
        }
    }

现在,我怎样才能添加一个自定义的指标,在点击焦点?谢谢

最佳答案

在屏幕其余部分的顶部添加覆盖视图。然后可以在其draw()函数中绘制指示器。每当需要更新覆盖显示时,请调用其setNeedsDisplay()函数。在下面的例子中,我在set函数中包含了接触点。请确保禁用此视图上的用户交互,否则触摸将无法通过底层用户界面。

class OverlayView : UIView {

    override func draw(_ rect: CGRect) {
        // draw your touch indicator here
        if let pt = _touchPoint {
            let color:UIColor = UIColor.gray
            let rect = CGRect(x:pt.x - 30, y:pt.y - 30, width:60, height:60)
            let bpath:UIBezierPath = UIBezierPath(rect: rect)

            color.set()
            bpath.stroke()
        }
    }

    private var _touchPoint : CGPoint?
    var touchPoint : CGPoint? {
        set(value) {
            _touchPoint = value
            setNeedsDisplay()
        }
        get {
            return _touchPoint
        }
    }

}

关于ios - 可视化点击 View ,显示点击指示器,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/45168271/

10-12 00:15
查看更多