Skip to content

Instantly share code, notes, and snippets.

import UIKit
struct Person: ExpressibleByStringLiteral {
typealias StringLiteralType = String
init(stringLiteral value: StringLiteralType) {
let components = value.components(separatedBy: .whitespaces)
firstName = components[0]
lastName = components[1]
}
let firstName: String
@khurram18
khurram18 / SwiftAtomics.swift
Last active May 31, 2020 17:54
Different approaches for implementing atomic types in swift. Please see the complete article here https://swiftx.tech/atomics-types-in-swift/
// A protocol to be used in our atomic type
protocol Operatable {
func add(other: Self) -> Self
}
// We will only limit our atomic type to Int, but it can be extended to use any other type
extension Int: Operatable {
func add(other: Int) -> Int {
return self + other
}
private func updateLabelPosition(_ cell: TableViewCell) {
let point = view.convert(cell.contributerNameLabel.frame.origin, from: cell.contentView) // 1
let ratio = point.y / viewHeight // 2
let updatedConstraint = (ratio * constraintRange) + minimumConstraint // 3
cell.nameLabelTopConstraint.constant = updatedConstraint // 4
}
extension ViewController: UITableViewDelegate {
// other implementaion
func scrollViewDidScroll(_ scrollView: UIScrollView) {
updateVisibleLabelPositions()
}
private func updateVisibleLabelPositions() {
guard let cells = tableView.visibleCells as? [TableViewCell] else { return }
for cell in cells {
updateLabelPosition(cell)
}
@khurram18
khurram18 / GridView.swift
Last active May 18, 2023 13:46
A grid view in swift, can be shown in a iOS camera app. It draws three rows and three columns which helps user to take a picture precisely.
class GridView: UIView {
// Only override draw() if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
override func draw(_ rect: CGRect) {
// Drawing code
let borderLayer = gridLayer()
borderLayer.path = UIBezierPath(rect: bounds).cgPath
@khurram18
khurram18 / UIImageView+DownloadURL.swift
Created May 10, 2018 18:28
A category for downloading image from URL and display it in UIImageView in iOS
extension UIImageView {
static var dataTask: URLSessionDataTask?
func download(urlString: String) {
guard let url = URL(string: urlString) else {
print("\(urlString) is not a valid url")
return
}
if let existingTask = UIImageView.dataTask {
if existingTask.state == .running {
existingTask.cancel()
@khurram18
khurram18 / CountingHelper.java
Created January 31, 2018 09:13
Show counting in TextView Android with animation
import android.view.animation.AccelerateInterpolator;
import android.view.animation.Interpolator;
import android.widget.TextView;
public class CountingHelper implements Runnable {
private final long mDuration;
private final int mStartingValue;
private final int mFinalValue;