[Swift]singleton@Swift3
singleton@Swift3
class クラス名 { class var sharedInstance :クラス名 { struct Static { static let instance = クラス名() } return Static.instance } var 変数名:Int=0 }
使用時
let v:クラス名 = クラス名.sharedInstance
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) }
UIView Extension #5
func fade(fadein:Bool, duration:CGFloat=0.3, operateHidden:Bool=true){ if operateHidden { self.hidden = false } UIView.setAnimationBeginsFromCurrentState(true) UIView.animateWithDuration(NSTimeInterval(duration), delay: 0.0, usingSpringWithDamping: 1.0, initialSpringVelocity: 0.0, options: UIViewAnimationOptions.CurveLinear, animations: { () -> Void in self.alpha = (fadein) ? 1.0 : 0.0 }, completion: { (finish:Bool) -> Void in if operateHidden { self.hidden = !fadein } } ) }
UIView Extension #4
var subviewsSortByTop:[AnyObject]{ get{ var w:[AnyObject] = self.getSubviews() w.sort { (v1:AnyObject, v2:AnyObject) -> Bool in return v1.origin.y < v2.origin.y } return w } }
UIView Extension #3
func goto(left:CGFloat,top:CGFloat, duration:CGFloat=0.4){ UIView.setAnimationBeginsFromCurrentState(true) UIView.animateWithDuration(NSTimeInterval(duration), delay: 0.0, usingSpringWithDamping: 1.0, initialSpringVelocity: 0.0, options: UIViewAnimationOptions.CurveEaseInOut, animations: { () -> Void in self.left = left self.top = top }, completion: { (finish:Bool) -> Void in } ) }
UIImageView Extension #1
extension UIImageView { var paintColor:String{ get{ return "" } set{ if self.image != nil { if newValue == "" { self.paintByTintColor = false }else{ self.tintColor = Drawer.getUIColorByColorHex(newValue, alpha: 1.0, luminanceScale: 1.0) self.paintByTintColor = true } } } } var paintByTintColor:Bool{ get{ return true } set{ if newValue { self.image = self.image!.imageWithRenderingMode(UIImageRenderingMode.AlwaysTemplate) }else{ self.image = self.image!.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal) } } } }
UIView Extension #2
var className:String{ get{ return NSStringFromClass(self.dynamicType).componentsSeparatedByString(".").last! } } var fileName:String{ get{ return __FILE__.componentsSeparatedByString("/").last! } }
UIView Extension #1
var left:CGFloat{ get{ return self.frame.origin.x } set{ self.frame = CGRectMake(newValue, self.frame.origin.y, self.frame.size.width, self.frame.size.height) } } var right:CGFloat{ get{ return self.frame.origin.x + self.frame.size.width } set{ self.frame = CGRectMake(newValue - self.frame.size.width, self.frame.origin.y, self.frame.size.width, self.frame.size.height) } } var top:CGFloat{ get{ return self.frame.origin.y } set{ self.frame = CGRectMake(self.frame.origin.x, newValue, self.frame.size.width, self.frame.size.height) } } var bottom:CGFloat{ get{ return self.frame.origin.y + self.frame.size.height } set{ self.frame = CGRectMake(self.frame.origin.x, newValue - self.frame.size.height, self.frame.size.width, self.frame.size.height) } }