This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let json = ["type": 1] | |
if let typeNumber = json["type"] { | |
imageView.image = Type(rawValue: typeNumber)?.image | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
enum Type: String { | |
// 直接指定 API 回傳的 String | |
case regular = "regular" | |
case system = "system" | |
case advertise = "advertise" | |
var image: UIImage? { | |
// 一行搞定 | |
return UIImage(named: self.rawValue) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation | |
protocol Convertable: Codable { | |
} | |
// Convert struct to dictionary | |
extension Convertable { | |
func convertToDict() -> Dictionary<String, Any>? { | |
var dict: Dictionary<String, Any>? = nil |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
struct User: Convertable { | |
let name: String | |
let phone: String | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
if let userDict = User.convertToDict() { | |
// Do something you want with dict | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@IBAction func textFieldDidChange(_ sender: UITextField) { | |
if let text = sender.text { | |
let uppercase = text.uppercased() | |
sender.text = uppercase | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
inputTextField.addTarget(self, action: #selector(textFieldDidChange(_:)) , for: UIControlEvents.editingChanged) | |
@objc func textFieldDidChange(_ textField: UITextField) { | |
// text field did change | |
} |