ios - 可视化点击 View ,显示点击指示器

标签 ios iphone swift xcode avfoundation

我正在尝试添加点击以聚焦到我的自定义相机 View 上,我设法做到了,但现在我需要显示一些东西,以便用户可以知道他实际上正在做某事......显示一个 UI,如 Apple 的黄色正方形,或 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
                }
            }
        }
    }

现在如何在该点击上添加自定义指示器以聚焦?谢谢

最佳答案

在屏幕的其余部分之上添加一个叠加 View 。然后您可以在其 draw() 函数中绘制您的指标。每当您需要覆盖显示更新时调用其 setNeedsDisplay() 函数。在下面的示例中,我在设置函数中包含了 touchPoint。确保在此 View 上禁用用户交互,否则您的触摸将无法通过底层用户界面。

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/

相关文章:

ios - 如何暂停和恢复多个 NSTimer?

ios - 我如何让用户输入密码以注销。解析/swift

iOS - 将 GIF 从 URL 保存到已保存的相册

ios:NSURLSessionDataTask 并从不同 View 下载

ios - 使用 UIImagePickerController() 选择图像会自动旋转它们

ios - 使用具有外部 cocoa pod 依赖项的 Swift 创建静态库

iphone - 使用 AVAssetExportSession 导出 Wav 文件

iphone - 模拟器上有声音但设备上没有

ios - 在 calloutAccessoryControlTapped 上检测仅点击 rightCalloutAccessoryView

swift - 调用中参数 'dictionary' 缺少参数