Swift

[Swift]addObserverでresize event

addObserverでresize event

self.contentView.addObserver(self, forKeyPath: "frame", options: NSKeyValueObservingOptions(rawValue: 0), context: nil)

override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
    //処理
}

[Swift]addGestureRecognizer

addGestureRecognizer

let gest = UILongPressGestureRecognizer(target: self, action: #selector(self.onPushTargetView))
self.targetView.addGestureRecognizer(gest)

[Swift]UITableView deleteRows

deleteRows

self.mainTableView.beginUpdates()

self.data.remove(at: 0)
self.mainTableView.deleteRows(at: [IndexPath(row:0, section: 0)], with: UITableViewRowAnimation.top)

self.mainTableView.endUpdates()

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
   return self.data.count
}

[Swift]UITableView insertRows

insertRows

self.mainTableView.beginUpdates()
self.data.insert("jack", at: 0)
self.mainTableView.insertRows(at: [IndexPath(row:0, section:0)], with:UITableViewRowAnimation.top)
self.mainTableView.endUpdates()

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
   return self.data.count
}

[Swift]Array Extension #2

Array Extension #2

mutating func switchValue(_ index1:Int,index2:Int){
    let _index1:Int = (index1 < 0) ? self.count + index1 : index1
    let _index2:Int = (index2 < 0) ? self.count + index2 : index2
    let elm = self[_index1]
    self[_index1] = self[_index2]
    self[_index2] = elm
}

[Swift]getUIColorByColorTuple

typealias rgbTuple = (r:CGFloat,g:CGFloat,b:CGFloat)

func getUIColorByColorTuple(color:rgbTuple, alpha:CGFloat=1.0, luminanceScale:CGFloat = 1.0) -> UIColor{
    var colort:rgbTuple = (r:color.r,g:color.g,b:color.b)
    let rl:CGFloat = 0.298912 * colort.r
    colort.r = rl * luminanceScale / 0.298912
    let gl:CGFloat = 0.586611 * colort.g
    colort.g = gl * luminanceScale / 0.586611
    let bl:CGFloat = 0.114478 * colort.b
    colort.b = bl * luminanceScale / 0.114478
    
    return UIColor(red: colort.r, green: colort.g, blue: colort.b, alpha: alpha)
}