Skip to content

Instantly share code, notes, and snippets.

View daoseng33's full-sized avatar
💭
una yaha

Rayray daoseng33

💭
una yaha
View GitHub Profile
class LeftAlignedCollectionViewFlowLayout: UICollectionViewFlowLayout {
override func layoutAttributesForElements(in rect: CGRect) -> [UICollectionViewLayoutAttributes]? {
let attributes = super.layoutAttributesForElements(in: rect)
var leftMargin = sectionInset.left
var maxY: CGFloat = -1.0
attributes?.forEach { layoutAttribute in
if layoutAttribute.frame.origin.y >= maxY {
leftMargin = sectionInset.left
self.navigationController?.interactivePopGestureRecognizer?.isEnabled = false
@daoseng33
daoseng33 / textFieldMaxLength.swift
Created September 19, 2018 09:25
Let you set UITextField max length in storyboard or xib
// Set textfield max length
private var __maxLengths = [UITextField: Int]()
extension UITextField {
@IBInspectable var maxLength: Int {
get {
guard let l = __maxLengths[self] else {
return Int.max // (global default-limit. or just, Int.max)
}
return l
}
@daoseng33
daoseng33 / naviEdgeGesture.swift
Created September 18, 2018 09:53
Get navigation bar edge swipe gesture back
// MARK: - View Lifecycle
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
self.navigationController?.interactivePopGestureRecognizer?.delegate = self
}
// MARK: - UIGestureRecognizerDelegate
extension MyViewController: UIGestureRecognizerDelegate {
func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldBeRequiredToFailBy otherGestureRecognizer: UIGestureRecognizer) -> Bool {
return true
UIView.setAnimationsEnabled(false) // 我夾
self.tableView.insertRows(at: indexPaths, with: .none)
UIView.setAnimationsEnabled(true) // 我夾
DispatchQueue.global(qos: .background).async { // 背景線程
let initialIndex = self.datas.count // 原本已有的 20 筆資料
var indexPaths: [IndexPath] = [] // 將要加入新項目的懵懂 array
for index in 0..<newPackOfDatas.count { // 將 indexPaths 加入新的 20 筆資料
let indexPath = IndexPath(item: initialIndex + index, section: 0) // 注意起始位置是 20(initialIndex 為還沒更新前的資料 count)
indexPaths.append(indexPath)
}
self.datas += newPackOfDatas // 將新的資料加入 datas
let maskPath = UIBezierPath(roundedRect: view.bounds, byRoundingCorners: [.topLeft, .topRight], cornerRadii: CGSize(width: 5, height: 5))
let maskLayer = CAShapeLayer()
maskLayer.frame = view.bounds
maskLayer.path = maskPath.cgPath
view.layer.mask = maskLayer
inputTextField.addTarget(self, action: #selector(textFieldDidChange(_:)) , for: UIControlEvents.editingChanged)
@objc func textFieldDidChange(_ textField: UITextField) {
// text field did change
}
// 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)
@IBAction func textFieldDidChange(_ sender: UITextField) {
if let text = sender.text {
let uppercase = text.uppercased()
sender.text = uppercase
}
}