Skip to content

Commit

Permalink
Merge pull request #92 from hyperoslo/support/taking-pictures-with-vo…
Browse files Browse the repository at this point in the history
…lume-button

Support taking pictures with volume button
  • Loading branch information
RamonGilabert committed Jan 19, 2016
2 parents 1512077 + 8ac8a9b commit cb33c00
Showing 1 changed file with 27 additions and 0 deletions.
27 changes: 27 additions & 0 deletions Source/ImagePickerController.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import UIKit
import MediaPlayer

public protocol ImagePickerDelegate: class {

Expand Down Expand Up @@ -59,6 +60,15 @@ public class ImagePickerController: UIViewController {
return gesture
}()

lazy var volumeView: MPVolumeView = { [unowned self] in
let view = MPVolumeView()
view.frame = CGRect(x: 0, y: 0, width: 1, height: 1)

return view
}()

var volume = AVAudioSession.sharedInstance().outputVolume

public weak var delegate: ImagePickerDelegate?
public var stack = ImageStack()
public var imageLimit = 0
Expand Down Expand Up @@ -86,10 +96,15 @@ public class ImagePickerController: UIViewController {
subview.translatesAutoresizingMaskIntoConstraints = false
}

view.addSubview(volumeView)
view.sendSubviewToBack(volumeView)

view.backgroundColor = .whiteColor()
view.backgroundColor = Configuration.mainColor
cameraController.view.addGestureRecognizer(panGestureRecognizer)

try! AVAudioSession.sharedInstance().setActive(true)

subscribe()
setupConstraints()
}
Expand Down Expand Up @@ -124,6 +139,7 @@ public class ImagePickerController: UIViewController {
// MARK: - Notifications

deinit {
try! AVAudioSession.sharedInstance().setActive(false)
NSNotificationCenter.defaultCenter().removeObserver(self)
}

Expand All @@ -142,6 +158,17 @@ public class ImagePickerController: UIViewController {
selector: "adjustButtonTitle:",
name: ImageStack.Notifications.stackDidReload,
object: nil)

NSNotificationCenter.defaultCenter().addObserver(self,
selector: "volumeChanged:",
name: "AVSystemController_SystemVolumeDidChangeNotification",
object: nil)
}

func volumeChanged(notification: NSNotification) {
guard let slider = volumeView.subviews.filter({ $0 is UISlider }).first as? UISlider else { return }
slider.setValue(volume, animated: false)
cameraController.takePicture()
}

func adjustButtonTitle(notification: NSNotification) {
Expand Down

0 comments on commit cb33c00

Please sign in to comment.