info

Home
[Swift]自作UIRefreshControl  @ App Develop , Swift 2016年7月1日
自作UIRefreshControl
let REFRESH_HEIGHT:CGFloat = 66.0
var readyRefresh:Bool = false
var defaultInsetTop:CGFloat = 0.0

func scrollViewDidScroll(scrollView: UIScrollView) {
    if self.readyRefresh {
        if -self.REFRESH_HEIGHT < scrollView.contentInset.top + scrollView.contentOffset.y {
            self.readyRefresh = false
        }
    }else {
        if -self.REFRESH_HEIGHT >= scrollView.contentInset.top + scrollView.contentOffset.y {
            self.readyRefresh = true
        }
    }
}
func scrollViewDidEndDragging(scrollView: UIScrollView, willDecelerate decelerate: Bool) {
    if !self.readyRefresh {
        return
    }
    self.readyRefresh = false
    self.defaultInsetTop = self.tableView.contentInsetTop
    UIView.setAnimationBeginsFromCurrentState(true)
    UIView.animateWithDuration(NSTimeInterval(0.3), delay: 0.0, usingSpringWithDamping: 1.0, initialSpringVelocity: 0.0, options: UIViewAnimationOptions.CurveLinear,
        animations: { () -> Void in
            self.tableView.contentInsetTop = self.REFRESH_HEIGHT
        },
        completion: { (finish:Bool) -> Void in
        }
    )
    
    self.executeRefresh()
}
private func endRefresh(){
    UIView.setAnimationBeginsFromCurrentState(true)
    UIView.animateWithDuration(NSTimeInterval(0.3), delay: 0.0, usingSpringWithDamping: 1.0, initialSpringVelocity: 0.0, options: UIViewAnimationOptions.CurveLinear,
        animations: { () -> Void in
            self.tableView.contentInsetTop = self.defaultInsetTop
        },
        completion: { (finish:Bool) -> Void in
        }
    )
}
private func executeRefresh(){
    //処理
    
    self.endRefresh()
}
[Swift]文字列分割  @ App Develop , Swift 2016年6月7日
文字列分割
let datetimeString:String = "2016-02-17 14:51:21"
let tmp:[String] = datetimeString.componentsSeparatedByCharactersInSet(NSCharacterSet(charactersInString:"- :"))
print(tmp)

出力結果
["2016", "02", "17", "14", "51", "21"]
[Swift]UIView Extension #6  @ App Develop , Swift 2016年6月5日
UIView Extension #6
func animateBG2Black(duration:CGFloat=0.3){
    UIView.setAnimationBeginsFromCurrentState(true)
    UIView.animateWithDuration(NSTimeInterval(duration), delay: 0.0, usingSpringWithDamping: 1.0, initialSpringVelocity: 0.0, options: UIViewAnimationOptions.CurveLinear,
        animations: { () -> Void in
            self.backgroundColor = UIColor.blackColor()
        },
        completion: { (finish:Bool) -> Void in
            
        }
    )
}
[Swift]Array Extension #1  @ App Develop , Swift 2016年6月4日
Array Extension #1
extension Array {
    mutating func firstToLast(){
        let elm = self.removeFirst()
        self.append(elm)
    }
    mutating func lastToFirst(){
        let elm = self.removeLast()
        self.insert(elm, atIndex: 0)
    }
}

使用
var tmp1:[Int] = [0,1,2,3]
tmp1.firstToLast()
print(tmp1)
tmp1.lastToFirst()
print(tmp1)

出力結果
[1, 2, 3, 0]
[0, 1, 2, 3]
[Swift]UIScrollViewのページ番号  @ App Develop , Swift 2016年6月3日
UIScrollViewのページ番号
・paging Enabledをチェックしておく。
・delegateを紐付けておく。
func scrollViewWillBeginDragging(scrollView: UIScrollView) {
    let page:Int = Int( round(scrollView.contentOffset.x / scrollView.frame.size.width) )
    print("------begin \(page)")//スクロール開始時ページ番号
}
func scrollViewDidEndDecelerating(scrollView: UIScrollView) {
    let page:Int = Int( scrollView.contentOffset.x / scrollView.frame.size.width )
    print("------end \(page)")//スクロール完了時ページ番号
}
func scrollViewWillEndDragging(scrollView: UIScrollView, withVelocity velocity: CGPoint, targetContentOffset: UnsafeMutablePointer<CGPoint>) {
    let to:Int = Int( targetContentOffset.memory.x / scrollView.frame.size.width )//スクロール完了時ページ番号
    print("\n------endDrag \(to)")
}
[Swift]singleton@Swift3  @ App Develop , Swift 2016年5月30日
singleton@Swift3
class クラス名 {
    class var sharedInstance :クラス名 {
        struct Static {
            static let instance = クラス名()
        }
        return Static.instance
    }
    var 変数名:Int=0
}
使用時
let v:クラス名 = クラス名.sharedInstance
[Swift]UITextField Extension #1  @ App Develop , Swift 2016年5月1日
UITextField Extension #1
func setTintColorToClearButton(){
    let anyobj:AnyObject? = self.valueForKey("_clearButton")
    if let anyobj:AnyObject = anyobj {
        if anyobj.isMemberOfClass(UIButton) {
            let button:UIButton = anyobj as UIButton
            if let image:UIImage = button.imageForState(.Highlighted) {
                let paintedImage:UIImage = image.imageWithRenderingMode(UIImageRenderingMode.AlwaysTemplate)
                button.setImage(paintedImage, forState: .Normal)
                button.setImage(paintedImage, forState: .Highlighted)
            }
        }
    }
}