2017-07-18 132 views
0

我想添加水龍頭,以專注於我的自定義相機視圖,我設法做到這一點,但現在我需要顯示的東西,使用戶可以知道他實際上在做一些事情。 ......展示一個像蘋果黃色廣場或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 
       } 
      } 
     } 
    } 

現在,我怎麼添加自定義指標上觸摸對焦?謝謝

回答

1

在屏幕的其餘部分頂部添加覆蓋視圖。然後你可以在draw()函數中繪製你的指標。每當你需要覆蓋顯示來更新時調用它的setNeedsDisplay()函數。在下面的例子中,我已經在設置函數中包含了touchPoint。確保在此視圖上禁用用戶交互,否則您的觸摸將無法通過底層用戶界面。

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

} 
+0

因此,如果我將在draw func中添加圖像,它會被看到? – RandomGeek

+0

我已經擴展了示例代碼,以顯示如何在覆蓋視圖中繪製關於可變觸摸點的正方形。 – Spads