Moondra
Moondra

Reputation: 4531

Unrecognized Selector sent to instance (UIPinchGestureRecognizer)

I've looked through previous threads on this subject but I still can't still figure out the solution. I've been at it for a couple of days.

I'm attempting to use the PinchGestureRecognizer on the camera preview screen.

However, I'm getting this error:

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[Testing.CameraViewController pinchActionWithGestureRecgnizer:]: unrecognized selector sent to instance 0x10222d030'

This is my setup: I made a separate class for my camera functions.

class CameraSetup{

var captureSession = AVCaptureSession()
var frontCam : AVCaptureDevice?
var backCam : AVCaptureDevice?
var currentCam: AVCaptureDevice?
var captureInput: AVCaptureDeviceInput?
var captureOutput: AVCapturePhotoOutput?
 var cameraPreviewLayer: AVCaptureVideoPreviewLayer?

var oldZoomScale:CGFloat = 1.0


 //bunch of camera functions......followed by this function



@objc func pinchAction(gestureRecgnizer: UIPinchGestureRecognizer) {

    do { print("func started")
        try currentCam!.lockForConfiguration()
        print(currentCam?.videoZoomFactor)
        print(currentCam?.deviceType)

        // ズームの最大値
        let maxZoomScale: CGFloat = 6.0
        // ズームの最小値
        let minZoomScale: CGFloat = 1.0
        // 現在のカメラのズーム度
        var currentZoomScale: CGFloat = currentCam!.videoZoomFactor
        // ピンチの度合い
        let pinchZoomScale: CGFloat = gestureRecgnizer.scale
        // ピンチアウトの時、前回のズームに今回のズーム-1を指定

        // 例: 前回3.0, 今回1.2のとき、currentZoomScale=3.2

        if pinchZoomScale > 1.0 {

            currentZoomScale = oldZoomScale+pinchZoomScale-1

        } else {

            currentZoomScale = oldZoomScale-(1-pinchZoomScale)*oldZoomScale

        }

        // 最小値より小さく、最大値より大きくならないようにする
        if currentZoomScale < minZoomScale {
            currentZoomScale = minZoomScale
        }
        else if currentZoomScale > maxZoomScale {
            currentZoomScale = maxZoomScale

        }

        // 画面から指が離れたとき、stateがEndedになる。

        if gestureRecgnizer.state == .ended {

            oldZoomScale = currentZoomScale

        }

        currentCam?.videoZoomFactor = currentZoomScale

        defer {currentCam?.unlockForConfiguration()}

    } catch {

        // handle error

        return

    }

}

}

Then I try to call this in my cameraPreviewController

class CameraViewController :UIViewController {

    @IBOutlet weak var flashButtonImage: UIButton!

    @IBOutlet weak var cameraButton: UIButton!
    @IBOutlet weak var imagePreview: UIImageView!               
    var image: UIImage?    
    var cameraSetup: CameraSetup!

 func initializeCamera(){

    cameraSetup = CameraSetup()
    cameraSetup.captureDevice()
    cameraSetup.configureCaptureInput()
    cameraSetup.configureCaptureOutput()
    cameraSetup.configurePreviewLayer(view: imagePreview)



}

override func viewDidLoad() {
       super.viewDidLoad()
       applyRoundCorner(cameraButton)
       initializeCamera()

    let pinchGesture = UIPinchGestureRecognizer(target: self, action: #selector(cameraSetup.pinchAction(gestureRecgnizer:)))

    print("testing")

    view.addGestureRecognizer(pinchGesture)

    }

I checked the outlet connections, but I don't see faulty connections, so I don't think it's a connection problem. I will take a closer look if you guys suggest.

Thank you so much.

Upvotes: 0

Views: 465

Answers (1)

Shehata Gamal
Shehata Gamal

Reputation: 100549

You can't do method in a class and target in another place change

let pinchGesture = UIPinchGestureRecognizer(target: self, action: #selector(cameraSetup.pinchAction(gestureRecgnizer:)))

to

let pinchGesture = UIPinchGestureRecognizer(target:cameraSetup, action: #selector(cameraSetup.pinchAction(gestureRecgnizer:)))

The target should contain the method inside the selector whether you make it cameraSetup or self

Upvotes: 2

Related Questions