Advertisement
dinophanhk

test focus camera

Aug 3rd, 2015
227
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 1.97 KB | None | 0 0
  1. 1. Dùng AssetsLibrary để custom cameraview.
  2. 2. Giả sử focusView là 1 UIImageView dùng để show ‘focus’ lên khi người ta nhấn vào 1 điểm trên màn hình.
  3.  
  4. // Biến này để ngăn người dùng focus lần nữa khi focus hiện tại đang animate fade out.
  5. // Có nhiều cách để làm, ví dụ xoá forcus view hiện tại và hiện lên location mới.
  6. var canFocus:Bool = true
  7.  
  8.     override func touchesBegan(
  9.         touches: Set<NSObject>,
  10.         withEvent event: UIEvent) {
  11.             if (canFocus == true && isLockingTouch == false) {
  12.                 var anyTouch = touches.first as! UITouch
  13.                 var location = anyTouch.locationInView(self.view)
  14.                 var touchPercent = location.x / Config.kScreenSize.width
  15.                 if (location.y < Config.kScreenSize.height - 50) {
  16.                     canFocus = false
  17.                     focusView.center = CGPointMake(
  18.                         anyTouch.locationInView(self.view).x,
  19.                         anyTouch.locationInView(self.view).y)
  20.                     focusTo(Float(touchPercent))
  21.                 }
  22.             }
  23.     }
  24.    
  25.     func focusTo(value : Float) {
  26.         focusView.alpha = 0.8;
  27.         if let device = captureDevice {
  28.             if(device.lockForConfiguration(nil)) {
  29.                 device.setFocusModeLockedWithLensPosition(
  30.                     value,
  31.                     completionHandler: { (CMTime) -> Void in
  32.                         UIView.animateWithDuration(
  33.                             1.3,
  34.                             animations: { () -> Void in
  35.                                 self.focusView.alpha = 0.5
  36.                             },
  37.                             completion: { (Bool) -> Void in
  38.                                 self.focusView.alpha = 0
  39.                                 self.canFocus = true
  40.                         })
  41.                 })
  42.                 device.unlockForConfiguration()
  43.             }
  44.         }
  45.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement