Add UIScrollView to AVCamManual-Swift

118 views Asked by At

I am using the third-party Swift port of Apple's AVCamManual sample code as the foundation for an app I am building. This can be found on GitHub here. My port to Swift 3 is on my GitHub repository here.

I want to be able to add functionality to allow the user to use a pinch gesture to zoom the video preview, and also use a pan gesture to move the zoomed video preview. It seems that using a UIScrollView is the best choice to do this, but I am unable to figure out how to do this.

I did find this post on SO that discusses doing this using ObjC, but have not been successful in getting this to work using Swift to add it to the AVCamManual-Swift project. I added UIScrollViewDelegate to the class declaration like this:

class AAPLCameraViewController: UIViewController, AVCaptureFileOutputRecordingDelegate, UIScrollViewDelegate {

I added an IBOutlet for the scroller:

// Set IBOutlet for UIScrollView
@IBOutlet var scroller:UIScrollView?

I then added the following code at the end of viewDidLoad():

// Setup UIScrollView
let scroller = UIScrollView()
scroller.isScrollEnabled = true
scroller.backgroundColor = UIColor.clear
scroller.showsHorizontalScrollIndicator = false
scroller.isPagingEnabled = false
scroller.bounces = false
scroller.delegate = self

self.view.addSubview(scroller)

The app builds and runs fine, but there it is no response to pinch or pan gestures.

In the storyboard I dragged a UIScrollView object to the Preview View, connected it to the IBOutlet, then set it's constraints to match that of the Preview View. The app runs but no zooming and scrolling goodness is occurring. I'm not sure what I am missing.

Approaching it from a different angle, I had also tried to select the Preview View in the storyboard, then select Editor > Embed In > Scroll View, but Scroll View is inactive. When selecting all of the objects within the Preview View I was then able to embed them in a Scroll View, but this does not include the preview layer, so when running all I see is the video preview with none of the controls.

Any suggestions on what I'm missing? I feel like I'm very close to figuring this out, but since I'm a noob I could be far out in the weeds.

0

There are 0 answers