[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()) { //処理 }
dispatch_after
let delayTime = dispatch_time(DISPATCH_TIME_NOW, Int64(0.3 * Double(NSEC_PER_SEC))) dispatch_after(delayTime, dispatch_get_main_queue()) { //処理 }
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 } ) }
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]
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)") }
addTarget@Swift3
button.addTarget(self, action:#selector(UIMainScrollView.onTapButton(_:)), forControlEvents: UIControlEvents.TouchUpInside)
XCodeをアップデートしたらコミット出来ず、下記メッセージが出る。
Couldn’t communicate with a helper application.
下記をターミナルで実行したら直った。
xcrun git config --global user.email sample@mail.com xcrun git config --global user.name samplename
singleton@Swift3
class クラス名 { class var sharedInstance :クラス名 { struct Static { static let instance = クラス名() } return Static.instance } var 変数名:Int=0 }
使用時
let v:クラス名 = クラス名.sharedInstance
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) } } } }
UIButton Extension #1
var titleText:String{ get{ if let titleLabel:UILabel = self.titleLabel { if let titleLabelText:String = titleLabel.text { return titleLabelText } } return "" } set{ self.setTitle(newValue, forState: .Normal) self.setTitle(newValue, forState: .Highlighted) self.setTitle(newValue, forState: .Selected) self.setTitle(newValue, forState: .Reserved) self.setTitle(newValue, forState: .Disabled) } }
UIScrollView Extension #1
func contentSizeFit2Frame(){ self.contentSize = CGSizeMake(self.frame.size.width, self.frame.size.height) }