Skip to content

Instantly share code, notes, and snippets.

View barisuyar's full-sized avatar
🎯
Focusing

Barış Uyar barisuyar

🎯
Focusing
View GitHub Profile
@barisuyar
barisuyar / MockURLProtocol.swift
Created September 7, 2022 13:27
canInit override
final class MockURLProtocol: URLProtocol {
override class func canInit(with request: URLRequest) -> Bool { true }
}
@barisuyar
barisuyar / CameraPermission.swift
Created December 6, 2021 13:14
CameraPermission
struct CameraPermission: Permission {
var status: AVAuthorizationStatus {
AVCaptureDevice.authorizationStatus(for: .video)
}
func request(completion: @escaping (AVAuthorizationStatus) -> ()) {
AVCaptureDevice.requestAccess(for: .video) { (granted) in
completion(self.status)
}
@barisuyar
barisuyar / MicrophonePermission.swift
Created December 6, 2021 13:10
MicrophonePermission
struct MicrophonePermission: Permission {
var status: AVAudioSession.RecordPermission {
AVAudioSession.sharedInstance().recordPermission
}
func request(completion: @escaping (AVAudioSession.RecordPermission) -> ()) {
AVAudioSession.sharedInstance().requestRecordPermission { _ in
completion(self.status)
}
@barisuyar
barisuyar / Permission.swift
Created December 6, 2021 12:44
Permission Creation
protocol Permission {
associatedtype PermissionType
var status: PermissionType { get }
func request(completion: @escaping (PermissionType) -> ())
}
@barisuyar
barisuyar / Slider.swift
Created December 1, 2021 19:56
Complete Slider
import UIKit
final class Slider: UISlider {
private let baseLayer = CALayer() // Step 3
private let trackLayer = CAGradientLayer() // Step 7
override func draw(_ rect: CGRect) {
super.draw(rect)
setup()
@barisuyar
barisuyar / Slider.swift
Last active December 1, 2021 19:53
Slider + disabling actions
@objc private func valueChanged(_ sender: Slider) {
// Step 9
CATransaction.begin()
CATransaction.setDisableActions(true)
let thumbRectA = thumbRect(forBounds: bounds,
trackRect: trackRect(forBounds: bounds),
value: value)
trackLayer.frame = .init(x: 0,
y: frame.height / 4,
width: thumbRectA.midX,
@barisuyar
barisuyar / Slider.swift
Created December 1, 2021 19:27
Slider + Track positioning
private func setup() {
clear()
createBaseLayer() // Step 3
createThumbImageView() // Step 5
configureTrackLayer() // Step 7
addTarget(self, action: #selector(valueChanged(_:)), for: .valueChanged) // Step 8
}
// Step 8
@objc private func valueChanged(_ sender: Slider) {
@barisuyar
barisuyar / Slider.swift
Created December 1, 2021 19:24
Slider + TrackLayer
private let trackLayer = CAGradientLayer() // Step 7
// Step 7
private func configureTrackLayer() {
let firstColor = UIColor(red: 210/255, green: 152/255, blue: 238/255, alpha: 1).cgColor
let secondColor = UIColor(red: 166/255, green: 20/255, blue: 217/255, alpha: 1).cgColor
trackLayer.colors = [firstColor, secondColor]
trackLayer.startPoint = .init(x: 0, y: 0.5)
trackLayer.endPoint = .init(x: 1, y: 0.5)
trackLayer.frame = .init(x: 0,
@barisuyar
barisuyar / Slider.swift
Created December 1, 2021 19:20
Slider+all thumb states
// Step 5
private func createThumbImageView() {
let thumbSize = (3 * frame.height) / 4
let thumbView = ThumbView(frame: .init(x: 0,
y: 0,
width: thumbSize,
height: thumbSize))
thumbView.layer.cornerRadius = thumbSize / 2
let thumbSnapshot = thumbView.snapshot
setThumbImage(thumbSnapshot, for: .normal)
@barisuyar
barisuyar / Slider.swift
Created December 1, 2021 19:13
Slider + create thumb
// Step 5
private func createThumbImageView() {
let thumbSize = (3 * frame.height) / 4
let thumbView = ThumbView(frame: .init(x: 0,
y: 0,
width: thumbSize,
height: thumbSize))
thumbView.layer.cornerRadius = thumbSize / 2
let thumbSnapshot = thumbView.snapshot
setThumbImage(thumbSnapshot, for: .normal)