Skip to content

Instantly share code, notes, and snippets.

@eugeniobaglieri
Created July 18, 2018 22:52
Show Gist options
  • Save eugeniobaglieri/1cf1b16527705da2e2ec25550e53eb03 to your computer and use it in GitHub Desktop.
Save eugeniobaglieri/1cf1b16527705da2e2ec25550e53eb03 to your computer and use it in GitHub Desktop.
//
// VideoPlayerViewController.swift
// Tattoo Store
//
// Created by Eugenio Baglieri on 13/07/18.
// Copyright © 2018 Eugenio Baglieri. All rights reserved.
//
import UIKit
import YoutubeKit
class VideoPlayerViewController: UIViewController {
private enum Constants {
static let playerSizeRatio: CGFloat = 4/3
static let animationDuration: TimeInterval = 0.3
}
let video: Video
private var trimView: UIView!
private var player: YTSwiftyPlayer!
private var isShowingTrimView: Bool = false
// MARK: - Inits
init(video: Video) {
self.video = video
super.init(nibName: nil, bundle: nil)
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
// MARK: - Lifecycle
override func loadView() {
super.loadView()
view.backgroundColor = .black
player = createAndSetupPlayer()
trimView = createAndSetupTrimView()
view.addSubview(player)
view.addSubview(trimView)
view.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(VideoPlayerViewController.showTrimView)))
}
override func viewDidLoad() {
super.viewDidLoad()
player.loadPlayer()
}
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
let viewWidth = view.bounds.size.width
let viewHeight = view.bounds.size.height
let viewBounds = view.bounds
let viewSafeInsets = view.safeAreaInsets
let safeBounds = CGRect(x: viewBounds.origin.x + viewSafeInsets.left,
y: viewBounds.origin.y + viewSafeInsets.top,
width: viewBounds.width - viewSafeInsets.left - viewSafeInsets.right,
height: viewBounds.height - viewSafeInsets.top - viewSafeInsets.bottom)
if viewWidth > viewHeight {
// Landscape mode
player.frame = safeBounds
} else {
// Portrait mode
let playerWidth = safeBounds.width
let playerHeight = ceil(playerWidth / Constants.playerSizeRatio)
let playerFrame = CGRect(x: safeBounds.origin.x,
y: (viewHeight - playerHeight) / 2,
width: playerWidth,
height: playerHeight)
player.frame = playerFrame
}
trimView.frame = safeBounds
}
// MARK: - Private methods
private func createAndSetupPlayer() -> YTSwiftyPlayer {
let setup: [VideoEmbedParameter] = [.videoID(video.id), .playsInline(true), .loopVideo(false), .showModestbranding(true), .showRelatedVideo(false), .alwaysShowCaption(false), .loopVideo(true), .autoplay(true)]
return YTSwiftyPlayer(frame: CGRect(x: 0, y: 0, width: 640, height: 480), playerVars: setup)
}
private func createAndSetupTrimView() -> UIView {
let trim = UIView()
trim.isOpaque = false
trim.backgroundColor = UIColor(white: 0, alpha: 0.5)
trim.alpha = 0.0
let closeButton = UIButton(type: .system)
trim.addSubview(closeButton)
let closeImage = UIImage(named: "close-icon")
closeButton.setImage(closeImage, for: .normal)
closeButton.tintColor = .white
closeButton.translatesAutoresizingMaskIntoConstraints = false
closeButton.widthAnchor.constraint(equalToConstant: 26).isActive = true
closeButton.heightAnchor.constraint(equalToConstant: 26).isActive = true
closeButton.topAnchor.constraint(equalTo: trim.safeAreaLayoutGuide.topAnchor, constant: 0).isActive = true
closeButton.leftAnchor.constraint(equalTo: trim.safeAreaLayoutGuide.leftAnchor, constant: 16).isActive = true
closeButton.addTarget(self, action: #selector(VideoPlayerViewController.didTapCloseButton(_:)), for: .touchUpInside)
trim.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(VideoPlayerViewController.hideTrimView)))
return trim
}
@objc
private func hideTrimView() {
isShowingTrimView = false
UIView.animate(withDuration: Constants.animationDuration) { [unowned self] in
self.trimView.alpha = 0.0
}
}
@objc
private func showTrimView() {
isShowingTrimView = true
UIView.animate(withDuration: Constants.animationDuration) { [unowned self] in
self.trimView.alpha = 1
}
}
@objc
private func didTapCloseButton(_ sender: UIButton) {
self.presentingViewController?.dismiss(animated: true, completion: nil)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment