Skip to content

Instantly share code, notes, and snippets.

let json = ["type": 1]
if let typeNumber = json["type"] {
imageView.image = Type(rawValue: typeNumber)?.image
}
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 July 11, 2018 12:31 — forked from joshdholtz/StopJumpingTableViewOnInsertRows.swift
Used when loading more data into UITableView for a smooth "infinite scroll" feel
// 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)
import Foundation
protocol Convertable: Codable {
}
// Convert struct to dictionary
extension Convertable {
func convertToDict() -> Dictionary<String, Any>? {
var dict: Dictionary<String, Any>? = nil
struct User: Convertable {
let name: String
let phone: String
}
if let userDict = User.convertToDict() {
// Do something you want with dict
}
// 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
@IBAction func textFieldDidChange(_ sender: UITextField) {
if let text = sender.text {
let uppercase = text.uppercased()
sender.text = uppercase
}
}
// 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)
inputTextField.addTarget(self, action: #selector(textFieldDidChange(_:)) , for: UIControlEvents.editingChanged)
@objc func textFieldDidChange(_ textField: UITextField) {
// text field did change
}