Skip to content

Instantly share code, notes, and snippets.

View lanserxt's full-sized avatar
🖥️
Developing a dream

Anton Gubarenko lanserxt

🖥️
Developing a dream
View GitHub Profile
@lanserxt
lanserxt / QuadricEquationRootsPrint.swift
Last active October 10, 2019 12:02
Quadric equation roots printing method. There were to restrictions on type and ranges so it's a basic example.
func printRoots(a: Double, b: Double, c: Double, showImaginaryRoots: Bool = false) {
let b2 = b * b
let disc = b2 - (4 * a * c)
let discSqrt = sqrt(fabs(disc))
let isImage = disc < 0
let result = b * b - 4.0 * a * c
@lanserxt
lanserxt / Podfile
Created April 20, 2019 05:32
Cocoapods Swift version setup for specific Pod
#insert after all pods declaring (just put at the bottom of a file)
post_install do |installer|
installer.pods_project.targets.each do |target|
if target.name == 'POD_NAME_HERE'
target.build_configurations.each do |config|
config.build_settings['SWIFT_VERSION'] = '4.0'
end
end
end
end
@lanserxt
lanserxt / pi_monte_carlo.swift
Last active March 27, 2019 07:42
Pi approximate calculation by Monte Carlo method. Inspired by https://www.weheartswift.com/random-pi/.
func piCalc(_ totalPoints: Int) -> Double {
var nPointsInside = 0
for _ in 1...totalPoints {
let (x, y) = (drand48() * 2 - 1, drand48() * 2 - 1)
if x * x + y * y <= 1 {
nPointsInside += 1
}
}
return 4.0 * Double(nPointsInside) / Double(totalPoints)
}
@lanserxt
lanserxt / UINavigationController+Class.swift
Created January 15, 2019 11:39
UINavigationController extension to pop to specific class
extension UINavigationController {
func popToLastViewControllerType<T>(_ typeOfVC: T.Type) {
if let vc = viewControllers.last(where: {$0 is T}) {
popToViewController(vc, animated: true)
}
}
func popToFirstViewControllerType<T>(_ typeOfVC: T.Type) {
if let vc = viewControllers.first(where: {$0 is T}) {
popToViewController(vc, animated: true)
@lanserxt
lanserxt / CellBlurAnimator.swift
Last active December 28, 2018 12:54
Blur animator
fileprivate var propertyAnimator: UIViewPropertyAnimator!
fileprivate var blurEffectView: UIVisualEffectView!
fileprivate var backGradientView: UIView!
func addWorkoutBlur() {
if !UIAccessibilityIsReduceTransparencyEnabled() {
if blurEffectView != nil {
blurEffectView.removeFromSuperview()
}
if backGradientView != nil {
class Fraction: ExpressibleByStringLiteral {
private(set) var n: Int
private(set) var d: Int
init(numerator: Int, denominator:Int) {
self.n = numerator
self.d = denominator
}
@lanserxt
lanserxt / gist:691dcb8d08f50dd2de6cc564dd8f75ec
Created November 30, 2018 09:39
Mix audio for multiple videos
do {
try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback, mode: AVAudioSessionModeDefault, options: .mixWithOthers)
try AVAudioSession.sharedInstance().setActive(true)
} catch {
print(error)
}
func stripUrlToValid(_ url: String) -> String {
var stripped = url.trimmingCharacters(in: .whitespacesAndNewlines)
if stripped.hasPrefix("https") {
stripped = stripped.replacingOccurrences(of: "https", with: "")
}
if stripped.hasPrefix("http") {
stripped = stripped.replacingOccurrences(of: "http", with: "")
}
while stripped.hasPrefix(":") || stripped.hasPrefix("/") {
@lanserxt
lanserxt / LoopedPlayerView.swift
Last active October 25, 2023 16:18
Loop playback with AVQueuePlayer and AVPlayerLooper
final class LoopedVideoPlayerView: UIView {
fileprivate var videoURL: URL?
fileprivate var queuePlayer: AVQueuePlayer?
fileprivate var playerLayer: AVPlayerLayer?
fileprivate var playbackLooper: AVPlayerLooper?
func prepareVideo(_ videoURL: URL) {
let playerItem = AVPlayerItem(url: videoURL)
@lanserxt
lanserxt / LoopedPlayer.swift
Last active December 28, 2018 08:54
Loop playback with AVQueuePlayer
//: [Previous](@previous)
import UIKit
import PlaygroundSupport
import AVFoundation
import AVKit
let frame = CGRect(x: 0, y: 0, width: 400, height: 400)
final class LoopedVideoPlayerView: UIView {