ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • iOS) Custom Camera에서 view 터치 할때 사각형 이미지 표시하기
    Programing Language/iOS(Swift) 2019. 11. 14. 15:05
    728x90
    반응형

    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
    반응형
Designed by Tistory.