-
iOS) Custom Camera에서 view 터치 할때 사각형 이미지 표시하기Programing Language/iOS(Swift) 2019. 11. 14. 15:05728x90반응형
1. CameraFocusSquare 클래스 생성
// // CameraFocusSquare.swift // customCameraView // // Created by Ik ju Song on 2019/11/14. // Copyright © 2019 Ik ju Song. All rights reserved. // import UIKit class CameraFocusSquare: UIView,CAAnimationDelegate { internal let kSelectionAnimation:String = "selectionAnimation" fileprivate var _selectionBlink: CABasicAnimation? convenience init(touchPoint: CGPoint) { self.init() self.updatePoint(touchPoint) self.backgroundColor = UIColor.clear self.layer.borderWidth = 1.0 self.layer.borderColor = UIColor.orange.cgColor initBlink() } override init(frame: CGRect) { super.init(frame: frame) } fileprivate func initBlink() { // create the blink animation self._selectionBlink = CABasicAnimation(keyPath: "borderColor") self._selectionBlink!.toValue = (UIColor.white.cgColor as AnyObject) self._selectionBlink!.repeatCount = 3 // number of blinks self._selectionBlink!.duration = 0.4 // this is duration per blink self._selectionBlink!.delegate = self } required init?(coder aDecoder: NSCoder) { fatalError("init(coder:) has not been implemented") } /** Updates the location of the view based on the incoming touchPoint. */ func updatePoint(_ touchPoint: CGPoint) { let squareWidth: CGFloat = 80 let frame: CGRect = CGRect(x: touchPoint.x - squareWidth / 2, y: touchPoint.y - squareWidth / 2, width: squareWidth, height: squareWidth) self.frame = frame } /** This unhides the view and initiates the animation by adding it to the layer. */ func animateFocusingAction() { if let blink = _selectionBlink { // make the view visible self.alpha = 1.0 self.isHidden = false // initiate the animation self.layer.add(blink, forKey: kSelectionAnimation) } } /** Hides the view after the animation stops. Since the animation is automatically removed, we don't need to do anything else here. */ public func animationDidStop(_ anim: CAAnimation, finished flag: Bool){ if flag{ // hide the view self.alpha = 0.0 self.isHidden = true } } }
2. ViewContorller클래스에 코드 작성
@objc func tapToFocus(_ gesture : UILongPressGestureRecognizer) { if (gesture.state == UIGestureRecognizer.State.began) { //view1은 카메라 스크린이 나오는 뷰 let touchPoint:CGPoint = gesture.location(in: self.view1) let focusSquare = CameraFocusSquare(touchPoint: touchPoint) self.view1.addSubview(focusSquare) focusSquare.setNeedsDisplay() focusSquare.animateFocusingAction() print(#function) } }
3. viewContorller 클래스에 델리게이트 작성 (UIGestureRecognizerDelegate )
class ViewController: UIViewController, AVCapturePhotoCaptureDelegate, UIGestureRecognizerDelegate
4. viewDidLoad 함수에 코드추가
override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view. setuplayout() let touch_ractangle = UILongPressGestureRecognizer(target: self, action: #selector(tapToFocus)) touch_ractangle.delegate = self view1.addGestureRecognizer(touch_ractangle) }
끝
728x90반응형'Programing Language > iOS(Swift)' 카테고리의 다른 글
iOS ) 특정위치(좌표)에 동그라미 그리기 (점찍기) (0) 2019.11.21 iOS ) Storyboard없이 레이아웃 꾸밀때 constraint 작업 편하게 해주는 메서드 (0) 2019.11.15 iOS) Custom Camera 화면 전환 (0) 2019.11.14 iOS) 멀티쓰레딩 - GCD, DispatchQueue (0) 2019.11.13 iOS ) Custom Camera에서 tap to Foucs 기능 구현하기 (0) 2019.11.13