mirror of
https://github.com/samsonjs/SwiftyCam.git
synced 2026-04-27 15:07:43 +00:00
Added optional delegate methods for running session and added enabled property to SwiftyCamButton
This commit is contained in:
parent
ad38933dca
commit
d837d4d71e
4 changed files with 106 additions and 31 deletions
|
|
@ -22,7 +22,6 @@ class ViewController: SwiftyCamViewController, SwiftyCamViewControllerDelegate {
|
||||||
@IBOutlet weak var flipCameraButton : UIButton!
|
@IBOutlet weak var flipCameraButton : UIButton!
|
||||||
@IBOutlet weak var flashButton : UIButton!
|
@IBOutlet weak var flashButton : UIButton!
|
||||||
|
|
||||||
|
|
||||||
override func viewDidLoad() {
|
override func viewDidLoad() {
|
||||||
super.viewDidLoad()
|
super.viewDidLoad()
|
||||||
shouldPrompToAppSettings = true
|
shouldPrompToAppSettings = true
|
||||||
|
|
@ -31,6 +30,9 @@ class ViewController: SwiftyCamViewController, SwiftyCamViewControllerDelegate {
|
||||||
shouldUseDeviceOrientation = true
|
shouldUseDeviceOrientation = true
|
||||||
allowAutoRotate = true
|
allowAutoRotate = true
|
||||||
audioEnabled = true
|
audioEnabled = true
|
||||||
|
|
||||||
|
// disable capture button until session starts
|
||||||
|
captureButton.buttonEnabled = false
|
||||||
}
|
}
|
||||||
|
|
||||||
override var prefersStatusBarHidden: Bool {
|
override var prefersStatusBarHidden: Bool {
|
||||||
|
|
@ -42,6 +44,17 @@ class ViewController: SwiftyCamViewController, SwiftyCamViewControllerDelegate {
|
||||||
captureButton.delegate = self
|
captureButton.delegate = self
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func swiftyCamSessionDidStartRunning(_ swiftyCam: SwiftyCamViewController) {
|
||||||
|
print("Session did start running")
|
||||||
|
captureButton.buttonEnabled = true
|
||||||
|
}
|
||||||
|
|
||||||
|
func swiftyCamSessionDidStopRunning(_ swiftyCam: SwiftyCamViewController) {
|
||||||
|
print("Session did stop running")
|
||||||
|
captureButton.buttonEnabled = false
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
func swiftyCam(_ swiftyCam: SwiftyCamViewController, didTake photo: UIImage) {
|
func swiftyCam(_ swiftyCam: SwiftyCamViewController, didTake photo: UIImage) {
|
||||||
let newVC = PhotoViewController(image: photo)
|
let newVC = PhotoViewController(image: photo)
|
||||||
self.present(newVC, animated: true, completion: nil)
|
self.present(newVC, animated: true, completion: nil)
|
||||||
|
|
@ -50,19 +63,13 @@ class ViewController: SwiftyCamViewController, SwiftyCamViewControllerDelegate {
|
||||||
func swiftyCam(_ swiftyCam: SwiftyCamViewController, didBeginRecordingVideo camera: SwiftyCamViewController.CameraSelection) {
|
func swiftyCam(_ swiftyCam: SwiftyCamViewController, didBeginRecordingVideo camera: SwiftyCamViewController.CameraSelection) {
|
||||||
print("Did Begin Recording")
|
print("Did Begin Recording")
|
||||||
captureButton.growButton()
|
captureButton.growButton()
|
||||||
UIView.animate(withDuration: 0.25, animations: {
|
hideButtons()
|
||||||
self.flashButton.alpha = 0.0
|
|
||||||
self.flipCameraButton.alpha = 0.0
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func swiftyCam(_ swiftyCam: SwiftyCamViewController, didFinishRecordingVideo camera: SwiftyCamViewController.CameraSelection) {
|
func swiftyCam(_ swiftyCam: SwiftyCamViewController, didFinishRecordingVideo camera: SwiftyCamViewController.CameraSelection) {
|
||||||
print("Did finish Recording")
|
print("Did finish Recording")
|
||||||
captureButton.shrinkButton()
|
captureButton.shrinkButton()
|
||||||
UIView.animate(withDuration: 0.25, animations: {
|
showButtons()
|
||||||
self.flashButton.alpha = 1.0
|
|
||||||
self.flipCameraButton.alpha = 1.0
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func swiftyCam(_ swiftyCam: SwiftyCamViewController, didFinishProcessVideoAt url: URL) {
|
func swiftyCam(_ swiftyCam: SwiftyCamViewController, didFinishProcessVideoAt url: URL) {
|
||||||
|
|
@ -71,22 +78,8 @@ class ViewController: SwiftyCamViewController, SwiftyCamViewControllerDelegate {
|
||||||
}
|
}
|
||||||
|
|
||||||
func swiftyCam(_ swiftyCam: SwiftyCamViewController, didFocusAtPoint point: CGPoint) {
|
func swiftyCam(_ swiftyCam: SwiftyCamViewController, didFocusAtPoint point: CGPoint) {
|
||||||
let focusView = UIImageView(image: #imageLiteral(resourceName: "focus"))
|
print("Did focus at point: \(point)")
|
||||||
focusView.center = point
|
focusAnimationAt(point)
|
||||||
focusView.alpha = 0.0
|
|
||||||
view.addSubview(focusView)
|
|
||||||
|
|
||||||
UIView.animate(withDuration: 0.25, delay: 0.0, options: .curveEaseInOut, animations: {
|
|
||||||
focusView.alpha = 1.0
|
|
||||||
focusView.transform = CGAffineTransform(scaleX: 1.25, y: 1.25)
|
|
||||||
}, completion: { (success) in
|
|
||||||
UIView.animate(withDuration: 0.15, delay: 0.5, options: .curveEaseInOut, animations: {
|
|
||||||
focusView.alpha = 0.0
|
|
||||||
focusView.transform = CGAffineTransform(translationX: 0.6, y: 0.6)
|
|
||||||
}, completion: { (success) in
|
|
||||||
focusView.removeFromSuperview()
|
|
||||||
})
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func swiftyCamDidFailToConfigure(_ swiftyCam: SwiftyCamViewController) {
|
func swiftyCamDidFailToConfigure(_ swiftyCam: SwiftyCamViewController) {
|
||||||
|
|
@ -103,7 +96,6 @@ class ViewController: SwiftyCamViewController, SwiftyCamViewControllerDelegate {
|
||||||
|
|
||||||
func swiftyCam(_ swiftyCam: SwiftyCamViewController, didSwitchCameras camera: SwiftyCamViewController.CameraSelection) {
|
func swiftyCam(_ swiftyCam: SwiftyCamViewController, didSwitchCameras camera: SwiftyCamViewController.CameraSelection) {
|
||||||
print("Camera did change to \(camera.rawValue)")
|
print("Camera did change to \(camera.rawValue)")
|
||||||
|
|
||||||
print(camera)
|
print(camera)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -117,7 +109,48 @@ class ViewController: SwiftyCamViewController, SwiftyCamViewControllerDelegate {
|
||||||
|
|
||||||
@IBAction func toggleFlashTapped(_ sender: Any) {
|
@IBAction func toggleFlashTapped(_ sender: Any) {
|
||||||
flashEnabled = !flashEnabled
|
flashEnabled = !flashEnabled
|
||||||
|
toggleFlashAnimation()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// UI Animations
|
||||||
|
extension ViewController {
|
||||||
|
|
||||||
|
fileprivate func hideButtons() {
|
||||||
|
UIView.animate(withDuration: 0.25) {
|
||||||
|
self.flashButton.alpha = 0.0
|
||||||
|
self.flipCameraButton.alpha = 0.0
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fileprivate func showButtons() {
|
||||||
|
UIView.animate(withDuration: 0.25) {
|
||||||
|
self.flashButton.alpha = 1.0
|
||||||
|
self.flipCameraButton.alpha = 1.0
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fileprivate func focusAnimationAt(_ point: CGPoint) {
|
||||||
|
let focusView = UIImageView(image: #imageLiteral(resourceName: "focus"))
|
||||||
|
focusView.center = point
|
||||||
|
focusView.alpha = 0.0
|
||||||
|
view.addSubview(focusView)
|
||||||
|
|
||||||
|
UIView.animate(withDuration: 0.25, delay: 0.0, options: .curveEaseInOut, animations: {
|
||||||
|
focusView.alpha = 1.0
|
||||||
|
focusView.transform = CGAffineTransform(scaleX: 1.25, y: 1.25)
|
||||||
|
}) { (success) in
|
||||||
|
UIView.animate(withDuration: 0.15, delay: 0.5, options: .curveEaseInOut, animations: {
|
||||||
|
focusView.alpha = 0.0
|
||||||
|
focusView.transform = CGAffineTransform(translationX: 0.6, y: 0.6)
|
||||||
|
}) { (success) in
|
||||||
|
focusView.removeFromSuperview()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fileprivate func toggleFlashAnimation() {
|
||||||
if flashEnabled == true {
|
if flashEnabled == true {
|
||||||
flashButton.setImage(#imageLiteral(resourceName: "flash"), for: UIControlState())
|
flashButton.setImage(#imageLiteral(resourceName: "flash"), for: UIControlState())
|
||||||
} else {
|
} else {
|
||||||
|
|
|
||||||
|
|
@ -54,6 +54,10 @@ open class SwiftyCamButton: UIButton {
|
||||||
|
|
||||||
public weak var delegate: SwiftyCamButtonDelegate?
|
public weak var delegate: SwiftyCamButtonDelegate?
|
||||||
|
|
||||||
|
// Sets whether button is enabled
|
||||||
|
|
||||||
|
public var buttonEnabled = true
|
||||||
|
|
||||||
/// Maximum duration variable
|
/// Maximum duration variable
|
||||||
|
|
||||||
fileprivate var timer : Timer?
|
fileprivate var timer : Timer?
|
||||||
|
|
@ -76,11 +80,19 @@ open class SwiftyCamButton: UIButton {
|
||||||
/// UITapGestureRecognizer Function
|
/// UITapGestureRecognizer Function
|
||||||
|
|
||||||
@objc fileprivate func Tap() {
|
@objc fileprivate func Tap() {
|
||||||
|
guard buttonEnabled == true else {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
delegate?.buttonWasTapped()
|
delegate?.buttonWasTapped()
|
||||||
}
|
}
|
||||||
|
|
||||||
/// UILongPressGestureRecognizer Function
|
/// UILongPressGestureRecognizer Function
|
||||||
@objc fileprivate func LongPress(_ sender:UILongPressGestureRecognizer!) {
|
@objc fileprivate func LongPress(_ sender:UILongPressGestureRecognizer!) {
|
||||||
|
guard buttonEnabled == true else {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
switch sender.state {
|
switch sender.state {
|
||||||
case .began:
|
case .began:
|
||||||
delegate?.buttonDidBeginLongPress()
|
delegate?.buttonDidBeginLongPress()
|
||||||
|
|
|
||||||
|
|
@ -359,8 +359,6 @@ open class SwiftyCamViewController: UIViewController {
|
||||||
NotificationCenter.default.addObserver(self, selector: #selector(captureSessionDidStopRunning), name: .AVCaptureSessionDidStopRunning, object: nil)
|
NotificationCenter.default.addObserver(self, selector: #selector(captureSessionDidStopRunning), name: .AVCaptureSessionDidStopRunning, object: nil)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// MARK: ViewDidAppear
|
// MARK: ViewDidAppear
|
||||||
|
|
||||||
/// ViewDidAppear(_ animated:) Implementation
|
/// ViewDidAppear(_ animated:) Implementation
|
||||||
|
|
@ -1033,12 +1031,18 @@ open class SwiftyCamViewController: UIViewController {
|
||||||
|
|
||||||
@objc private func captureSessionDidStartRunning() {
|
@objc private func captureSessionDidStartRunning() {
|
||||||
sessionRunning = true
|
sessionRunning = true
|
||||||
|
DispatchQueue.main.async {
|
||||||
|
self.cameraDelegate?.swiftyCamSessionDidStartRunning(self)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Called when Notification Center registers session stops running
|
/// Called when Notification Center registers session stops running
|
||||||
|
|
||||||
@objc private func captureSessionDidStopRunning() {
|
@objc private func captureSessionDidStopRunning() {
|
||||||
sessionRunning = false
|
sessionRunning = false
|
||||||
|
DispatchQueue.main.async {
|
||||||
|
self.cameraDelegate?.swiftyCamSessionDidStopRunning(self)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -23,6 +23,24 @@ import AVFoundation
|
||||||
|
|
||||||
public protocol SwiftyCamViewControllerDelegate: class {
|
public protocol SwiftyCamViewControllerDelegate: class {
|
||||||
|
|
||||||
|
/**
|
||||||
|
SwiftyCamViewControllerDelegate function called when when SwiftyCamViewController session did start running.
|
||||||
|
Photos and video capture will be enabled.
|
||||||
|
|
||||||
|
- Parameter swiftyCam: Current SwiftyCamViewController
|
||||||
|
*/
|
||||||
|
|
||||||
|
func swiftyCamSessionDidStartRunning(_ swiftyCam: SwiftyCamViewController)
|
||||||
|
|
||||||
|
/**
|
||||||
|
SwiftyCamViewControllerDelegate function called when when SwiftyCamViewController session did stops running.
|
||||||
|
Photos and video capture will be disabled.
|
||||||
|
|
||||||
|
- Parameter swiftyCam: Current SwiftyCamViewController
|
||||||
|
*/
|
||||||
|
|
||||||
|
func swiftyCamSessionDidStopRunning(_ swiftyCam: SwiftyCamViewController)
|
||||||
|
|
||||||
/**
|
/**
|
||||||
SwiftyCamViewControllerDelegate function called when the takePhoto() function is called.
|
SwiftyCamViewControllerDelegate function called when the takePhoto() function is called.
|
||||||
|
|
||||||
|
|
@ -116,6 +134,14 @@ public protocol SwiftyCamViewControllerDelegate: class {
|
||||||
|
|
||||||
public extension SwiftyCamViewControllerDelegate {
|
public extension SwiftyCamViewControllerDelegate {
|
||||||
|
|
||||||
|
func swiftyCamSessionDidStopRunning(_ swiftyCam: SwiftyCamViewController) {
|
||||||
|
// Optional
|
||||||
|
}
|
||||||
|
|
||||||
|
func swiftyCamSessionDidStartRunning(_ swiftyCam: SwiftyCamViewController) {
|
||||||
|
// Optional
|
||||||
|
}
|
||||||
|
|
||||||
func swiftyCam(_ swiftyCam: SwiftyCamViewController, didTake photo: UIImage) {
|
func swiftyCam(_ swiftyCam: SwiftyCamViewController, didTake photo: UIImage) {
|
||||||
// Optional
|
// Optional
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue