Skip to content

Instantly share code, notes, and snippets.

@alexnikol
alexnikol / .swift
Last active April 26, 2020 17:05
Combine animations
CATransaction.begin() //Start of animation transaction
CATransaction.setCompletionBlock { //Transaction is Ready
print("READY")
}
heightConstraint.constant = 80.0
UIView.animate(withDuration: 2.0) { // UIView type of animation
view.layoutIfNeeded()
@alexnikol
alexnikol / .swift
Created April 26, 2020 17:21
Example of multiple layer animations
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Creation of View
let square = UIView(frame: CGRect(x: 20, y: 70, width: 370, height: 200))
square.layer.borderColor = UIColor.green.cgColor
square.layer.borderWidth = 2.0
square.layer.cornerRadius = 5
square.backgroundColor = UIColor.systemPink
@alexnikol
alexnikol / .swift
Created April 27, 2020 17:08
How to Create UIView with shadow, image, border and cornerRadius
let rect = CGRect(x: 40, y: 40, width: 300, height: 250)
let view = UIView(frame: rect)
view.backgroundColor = UIColor.systemPink
//Add Shadow
view.layer.shadowColor = UIColor.red.cgColor
view.layer.shadowOffset = CGSize(width: 20, height: 20)
view.layer.shadowRadius = 10.0
view.layer.shadowOpacity = 0.7
@alexnikol
alexnikol / .swift
Created April 26, 2020 15:57
Add sublayers to layer of UIView
// MARK: Creation of sublayer1
let sublayer1 = CALayer()
sublayer1.frame = CGRect(x: 20, y: 20, width: 30, height: 30)
sublayer1.backgroundColor = UIColor.red.cgColor
// MARK: Creation of sublayer2
let sublayer2 = CALayer()
sublayer2.frame = CGRect(x: 80, y: 80, width: 40, height: 40)
sublayer2.backgroundColor = UIColor.green.cgColor
@alexnikol
alexnikol / .swift
Last active April 28, 2020 14:32
UIAlertController example
//We can change preferredStyle: .actionSheet
let alert = UIAlertController(title: "Title", message: "Message", preferredStyle: .alert)
//Add Cancel Action
alert.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: nil)
//Add Default Action
alert.addAction(UIAlertAction(title: "Default", style: .default, handler: { (action) in
print("Default action")
}))
@alexnikol
alexnikol / .swift
Created April 28, 2020 14:39
How to add and customize UITextField to UIAlertController
func openAlert() {
//UITextField completionHandler. Here we can customize and add event handlers to field
alert.addTextField { (field) in
field.placeholder = "Pet Name" //add placeholder
field.addTarget(self, action: #selector(self.editingChanged), for: .editingChanged) //add editingChanged handler
}
self.present(alert, animated: true, completion: nil)
@alexnikol
alexnikol / .swift
Created April 28, 2020 14:53
Complex UIAlertController with field and actions
//We can change preferredStyle: .actionSheet
let alert = UIAlertController(title: "Title", message: "Message", preferredStyle: .alert)
//Add Cancel Action
alert.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: nil))
//Add Default Action
alert.addAction(UIAlertAction(title: "Default", style: .default, handler: { action in
print("Default button did tap")
}))
@alexnikol
alexnikol / .swift
Created April 30, 2020 13:19
Nested Swift Function Example
func runReportFlow() {
func getWeekReportBody() {} //First Nested Function
func getMonthReportBody() {} //Second Nested Function
if isSomeConditionTrue {
getWeekReportBody()
@alexnikol
alexnikol / .swift
Created May 2, 2020 06:18
Nested Functions Strong Reference Explanation
class Factory {
var car: Car
init(car: Car) {
self.car = car
}
deinit {
print("Factory model deallocated")
@alexnikol
alexnikol / .swift
Last active May 9, 2020 18:54
HalfCircleIndicator
class HalfCircleIndicator: UIView {
private var progressLayer: CAShapeLayer!
private var fullSize: CGSize!
private var grayCircleSize: CGSize!
private var innerGrayCircleSize: CGSize!
private var greenCircleSize: CGSize!
var value: CGFloat = 0.0
var isInnerCircleExist: Bool