-
-
Save anupamchugh/7092de8a70d5c52bbae52866cb1ba494 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class ViewController: UIViewController, HandSwiperDelegate{ | |
//MARK: - Properties | |
var modelData = [DataModel(bgColor: .systemYellow), | |
DataModel(bgColor: .systemBlue), | |
DataModel(bgColor: .systemRed), | |
DataModel(bgColor: .systemTeal), | |
DataModel(bgColor: .systemOrange), | |
DataModel(bgColor: .brown)] | |
var stackContainer : StackContainerView! | |
var buttonStackView: UIStackView! | |
var leftButton : UIButton!, rightButton : UIButton! | |
var cameraView : CameraView! | |
//MARK: - Init | |
override func loadView() { | |
view = UIView() | |
stackContainer = StackContainerView() | |
view.addSubview(stackContainer) | |
configureStackContainer() | |
stackContainer.translatesAutoresizingMaskIntoConstraints = false | |
addButtons() | |
configureNavigationBarButtonItem() | |
addCameraView() | |
} | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
title = "HandPoseSwipe" | |
stackContainer.dataSource = self | |
} | |
private let videoDataOutputQueue = DispatchQueue(label: "CameraFeedDataOutput", qos: .userInteractive) | |
private var cameraFeedSession: AVCaptureSession? | |
private var handPoseRequest = VNDetectHumanHandPoseRequest() | |
let message = UILabel() | |
var handDelegate : HandSwiperDelegate? | |
func addCameraView() | |
{ | |
cameraView = CameraView() | |
self.handDelegate = self | |
view.addSubview(cameraView) | |
cameraView.translatesAutoresizingMaskIntoConstraints = false | |
cameraView.bottomAnchor.constraint(equalTo: view.bottomAnchor).isActive = true | |
cameraView.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true | |
cameraView.widthAnchor.constraint(equalToConstant: 150).isActive = true | |
cameraView.heightAnchor.constraint(equalToConstant: 150).isActive = true | |
} | |
//MARK: - Configurations | |
func configureStackContainer() { | |
stackContainer.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true | |
stackContainer.centerYAnchor.constraint(equalTo: view.centerYAnchor, constant: -60).isActive = true | |
stackContainer.widthAnchor.constraint(equalToConstant: 300).isActive = true | |
stackContainer.heightAnchor.constraint(equalToConstant: 400).isActive = true | |
} | |
func addButtons() | |
{ | |
//full source of UI setup at the end of this article | |
} | |
@objc func onButtonPress(sender: UIButton){ | |
UIView.animate(withDuration: 2.0, | |
delay: 0, | |
usingSpringWithDamping: CGFloat(0.20), | |
initialSpringVelocity: CGFloat(6.0), | |
options: UIView.AnimationOptions.allowUserInteraction, | |
animations: { | |
sender.transform = CGAffineTransform.identity | |
}, | |
completion: { Void in() }) | |
if let firstView = stackContainer.subviews.last as? TinderCardView{ | |
if sender.tag == 0{ | |
firstView.leftSwipeClicked(stackContainerView: stackContainer) | |
} | |
else{ | |
firstView.rightSwipeClicked(stackContainerView: stackContainer) | |
} | |
} | |
} | |
func configureNavigationBarButtonItem() { | |
navigationItem.rightBarButtonItem = UIBarButtonItem(title: "Reset", style: .plain, target: self, action: #selector(resetTapped)) | |
} | |
@objc func resetTapped() { | |
stackContainer.reloadData() | |
} | |
override func viewDidAppear(_ animated: Bool) { | |
super.viewDidAppear(animated) | |
do { | |
if cameraFeedSession == nil { | |
cameraView.previewLayer.videoGravity = .resizeAspectFill | |
try setupAVSession() | |
cameraView.previewLayer.session = cameraFeedSession | |
} | |
cameraFeedSession?.startRunning() | |
} catch { | |
AppError.display(error, inViewController: self) | |
} | |
} | |
override func viewWillDisappear(_ animated: Bool) { | |
cameraFeedSession?.stopRunning() | |
super.viewWillDisappear(animated) | |
} | |
func setupAVSession() throws { | |
// Select a front facing camera, make an input. | |
guard let videoDevice = AVCaptureDevice.default(.builtInWideAngleCamera, for: .video, position: .front) else { | |
throw AppError.captureSessionSetup(reason: "Could not find a front facing camera.") | |
} | |
guard let deviceInput = try? AVCaptureDeviceInput(device: videoDevice) else { | |
throw AppError.captureSessionSetup(reason: "Could not create video device input.") | |
} | |
let session = AVCaptureSession() | |
session.beginConfiguration() | |
session.sessionPreset = AVCaptureSession.Preset.high | |
// Add a video input. | |
guard session.canAddInput(deviceInput) else { | |
throw AppError.captureSessionSetup(reason: "Could not add video device input to the session") | |
} | |
session.addInput(deviceInput) | |
let dataOutput = AVCaptureVideoDataOutput() | |
if session.canAddOutput(dataOutput) { | |
session.addOutput(dataOutput) | |
// Add a video data output. | |
dataOutput.alwaysDiscardsLateVideoFrames = true | |
dataOutput.videoSettings = [kCVPixelBufferPixelFormatTypeKey as String: Int(kCVPixelFormatType_420YpCbCr8BiPlanarFullRange)] | |
dataOutput.setSampleBufferDelegate(self, queue: videoDataOutputQueue) | |
} else { | |
throw AppError.captureSessionSetup(reason: "Could not add video data output to the session") | |
} | |
session.commitConfiguration() | |
cameraFeedSession = session | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment