[Swift]UIAlertController

UIAlertController

let alertController:UIAlertController = UIAlertController(title: title, message: message, preferredStyle: UIAlertControllerStyle.Alert)
alertController.addAction( UIAlertAction(title: cancelTitle, style: UIAlertActionStyle.Cancel, handler: { (action:UIAlertAction) in

} )
)
alertController.addAction( UIAlertAction(title: okTitle, style: UIAlertActionStyle.Default, handler: { (action:UIAlertAction) in

} )
)
self.presentViewController(alertController, animated: true, completion: { 
    
})

[Swift]自作UIRefreshControl

自作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]文字列分割

文字列分割

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]dispatch_after

dispatch_after

let delayTime = dispatch_time(DISPATCH_TIME_NOW, Int64(0.3 * Double(NSEC_PER_SEC)))
dispatch_after(delayTime, dispatch_get_main_queue()) {
    //処理
}

[Swift]UIView Extension #6

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

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のページ番号

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)")
}

[Xcode]アップデートしたらコミット出来ない

XCodeをアップデートしたらコミット出来ず、下記メッセージが出る。

Couldn’t communicate with a helper application.

下記をターミナルで実行したら直った。

xcrun git config --global user.email sample@mail.com
xcrun git config --global user.name samplename

[Swift]singleton@Swift3

singleton@Swift3

class クラス名 {
    class var sharedInstance :クラス名 {
        struct Static {
            static let instance = クラス名()
        }
        return Static.instance
    }
    var 変数名:Int=0
}

使用時

let v:クラス名 = クラス名.sharedInstance