Skip to content

Instantly share code, notes, and snippets.

View 2018062821110.swift
let json = ["type": 1]
if let typeNumber = json["type"] {
imageView.image = Type(rawValue: typeNumber)?.image
}
View 201806282115.swift
enum Type: String {
// 直接指定 API 回傳的 String
case regular = "regular"
case system = "system"
case advertise = "advertise"
var image: UIImage? {
// 一行搞定
return UIImage(named: self.rawValue)
}
@daoseng33
daoseng33 / StopJumpingTableViewOnInsertRows.swift
Created Jul 11, 2018 — forked from joshdholtz/StopJumpingTableViewOnInsertRows.swift
Used when loading more data into UITableView for a smooth "infinite scroll" feel
View StopJumpingTableViewOnInsertRows.swift
// I dont want my table jumping/animation when appending new rows
// for an infinite scroll feel
//
// Some of this might not be needed but it works
//
// TODO: Possibly garbage
extension UITableView {
func reloadDataSmoothly() {
UIView.setAnimationsEnabled(false)
View Convertable.swift
import Foundation
protocol Convertable: Codable {
}
// Convert struct to dictionary
extension Convertable {
func convertToDict() -> Dictionary<String, Any>? {
var dict: Dictionary<String, Any>? = nil
View convertableSampleStruct.swift
struct User: Convertable {
let name: String
let phone: String
}
View convertToDict.swift
if let userDict = User.convertToDict() {
// Do something you want with dict
}
View loadingHud.swift
// Set up the shape of the circle
CAShapeLayer *border = [CAShapeLayer layer];
border.path = [UIBezierPath bezierPathWithRoundedRect:self.myImageView.frame cornerRadius:self.myImageView.frame.size.height/2].CGPath;
border.anchorPoint = CGPointMake(0.5, 0.5);
border.position= CGPointMake(self.myImageView.bounds.origin.x, self.myImageView.bounds.origin.y);
border.fillColor = [UIColor clearColor].CGColor;
border.strokeColor = [UIColor blueColor].CGColor;
border.lineWidth = 5; [self.view.layer addSublayer:border];
// Configure animation
// draw Border
View uppercase.swift
@IBAction func textFieldDidChange(_ sender: UITextField) {
if let text = sender.text {
let uppercase = text.uppercased()
sender.text = uppercase
}
}
View dottedLine.swift
// Draw dotted line
let viewBorder = CAShapeLayer()
viewBorder.lineDashPattern = [2, 2]
viewBorder.frame = myView.bounds
viewBorder.fillColor = nil
viewBorder.path = UIBezierPath(rect: myView.bounds).cgPath
myView.layer.addSublayer(viewBorder)
View addObserve.swift
inputTextField.addTarget(self, action: #selector(textFieldDidChange(_:)) , for: UIControlEvents.editingChanged)
@objc func textFieldDidChange(_ textField: UITextField) {
// text field did change
}