info

category : Swift
[Swift]GCD #1  @ App Develop , Swift 2015年11月26日
GCD #1
func dispatch_async_main(block: () -> (), delay: Double = 0) {
    if delay <= 0 {
        dispatch_async(dispatch_get_main_queue(), block)
        return
    }
    
    let d = dispatch_time(DISPATCH_TIME_NOW, Int64(delay * Double(NSEC_PER_SEC)))
    dispatch_after(d, dispatch_get_main_queue()) {
        block()
    }
}
[Swift]UICollectionViewCellを上揃え  @ App Develop , Swift 2015年10月30日
UICollectionViewCellを上揃え
import UIKit

class TopAlignedCollectionViewFlowLayout: UICollectionViewFlowLayout {
    
    override func layoutAttributesForElementsInRect(rect: CGRect) -> [AnyObject]? {
        var sameYElements:[UICollectionViewLayoutAttributes] = []
        var baseline:CGFloat = -2.0
        var height:CGFloat = 0.0
        var basey:CGFloat = 0.0
        let attributesToReturn:[UICollectionViewLayoutAttributes] = super.layoutAttributesForElementsInRect(rect) as [UICollectionViewLayoutAttributes]
        for attributes in attributesToReturn {
            if attributes.representedElementCategory == UICollectionElementCategory.Cell {
                let centerY:CGFloat = CGRectGetMidY(attributes.frame)
                if 1.0 < fabs(centerY - baseline) {
                    baseline = centerY
                    self.__alignTopWithElements(sameYElements, basey:basey)
                    sameYElements.removeAll()
                    height = 0.0
                    basey = 0.0
                }
                sameYElements.append(attributes)
                if height < attributes.frame.size.height {
                    height = attributes.frame.size.height
                    basey = attributes.frame.origin.y
                }
            }
        }
        self.__alignTopWithElements(sameYElements, basey:basey)
        return attributesToReturn
    }
    private func __alignTopWithElements(elements:[UICollectionViewLayoutAttributes], basey:CGFloat) {
        if elements.count < 1 {
            return
        }
        for attributes in elements {
            var rect:CGRect = attributes.frame
            rect.origin.y = basey
            attributes.frame = rect
        }
    }
}
[Swift]UITextFieldのクリアボタンの色を変える  @ App Develop , Swift 2015年8月3日
UITextFieldのクリアボタンの色を変える
extension UITextField {
    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)
                }
            }
        }
    }
}

self.textField.tintColor = UIColor.whiteColor()
self.textField.setTintColorToClearButton()

before
textfield1

after
textfield2
[Swift]メモリアドレス取得  @ App Develop , Swift 2015年8月2日
メモリアドレス取得
func getMemoryAddress(target:AnyObject) -> String {
    return "\(unsafeAddressOf(target))"
}
let m1:String = self.getMemoryAddress(self.view1)
let m2:String = self.getMemoryAddress(self.view2)
println("mem:\(m1)")
println("mem:\(m2)")
println("m1 < m2 = \(m1 < m2)")
println("m2 < m1 = \(m2 < m1)")

結果
mem:0x00007fe718c4da20
mem:0x00007fe718c42f20
m1 < m2 = false
m2 < m1 = true
[Swift]クラス判別  @ App Develop , Swift 2015年8月1日
クラス判別
let button:UIButton = UIButton()
let anyobj:AnyObject = button

if anyobj.isKindOfClass(UIView) {//対象クラスのサブクラスでもtrue
    println("anyobj is kind of UIView")
}
if anyobj.isMemberOfClass(UIView) {
    println("anyobj is member of UIView")
}
if anyobj.isMemberOfClass(UIButton) {
    println("anyobj is member of UIButton")
}
if anyobj is UIView {//対象クラスのサブクラスでもtrue
    println("anyobj is UIView")
}
if anyobj is UIButton {
    println("anyobj is UIButton")
}

結果
anyobj is kind of UIView
anyobj is member of UIButton
anyobj is UIView
anyobj is UIButton
[Swift]UIImageを染める  @ App Develop , Swift 2015年7月25日
UIImageを染める
let image:UIImage? = UIImage(named: "input_iconTest")
let iv:UIImageView = UIImageView(image:image!)
iv.paintColor = "ff0000"
self.view.addSubview(iv)

extension UIImageView {
    class func getUIColorByColorHex(colorHex:String) -> UIColor{
        let colorScanner:NSScanner = NSScanner(string: colorHex)
        var color:UInt32 = 0
        colorScanner.scanHexInt(&color)
        let r:CGFloat = CGFloat((color & 0xFF0000) >> 16) / CGFloat(255.0);
        let g:CGFloat = CGFloat((color & 0x00FF00) >> 8)  / CGFloat(255.0);
        let b:CGFloat = CGFloat(color & 0x0000FF) / CGFloat(255.0);
        return UIColor(red: r, green: g, blue: b, alpha: 1.0)
    }
    var paintColor:String{
        get{
            return ""
        }
        set{
            if self.image != nil {
                if newValue == "" {
                    self.image = self.image!.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal)
                }else{
                    self.tintColor = UIImageView.getUIColorByColorHex(newValue)
                    self.image = self.image!.imageWithRenderingMode(UIImageRenderingMode.AlwaysTemplate)
                }
            }
        }
    }
}
[Swift]線を描く  @ App Develop , Swift 2015年7月24日
線を描く
let image:UIImage = self.getUIImageOfVerticalGrid(self.view.frame.size, start: 0.0, lineInterval: 10.0, lineThickness: 1.0, lineColorHex: "ff0000")
let iv:UIImageView = UIImageView(image:image)
iv.contentMode = UIViewContentMode.ScaleAspectFit
iv.frame = CGRectMake(0,0,self.view.frame.size.width,self.view.frame.size.height)
self.view.addSubview(iv)

func getUIImageOfVerticalGrid(size:CGSize, start:CGFloat=0.0, lineInterval:CGFloat=44.0, lineThickness:CGFloat=1.0, lineColorHex:String="D6D6D6") -> UIImage {
    return self.getUIImageOfGrid(false, size: size, start: start, lineInterval: lineInterval, lineThickness: lineThickness, lineColorHex: lineColorHex)
}
func getUIImageOfGrid(horizontal:Bool, size:CGSize, start:CGFloat=0.0, lineInterval:CGFloat=44.0, lineThickness:CGFloat=1.0, lineColorHex:String="D6D6D6") -> UIImage {
    let scale:CGFloat = UIScreen.mainScreen().scale
    UIGraphicsBeginImageContextWithOptions(size, false, 0.0)
    let context:CGContextRef = UIGraphicsGetCurrentContext()
    CGContextSetLineWidth(context, 0.0)
    let color:rgbTuple = self.getColorTupleByColorHex(lineColorHex)
    CGContextSetRGBStrokeColor(context, color.r,color.g,color.b, 1.0)
    CGContextSetRGBFillColor(context, color.r,color.g,color.b, 1.0)
    let w:CGFloat = (horizontal) ? size.width : lineThickness / scale
    let h:CGFloat = (horizontal) ? lineThickness / scale : size.height
    var currentPosition:CGFloat = start
    let maxPosition:CGFloat = (horizontal) ? size.height : size.width
    while currentPosition < maxPosition {
        let x:CGFloat = (horizontal) ? 0.0 : currentPosition
        let y:CGFloat = (horizontal) ? currentPosition : 0.0
        CGContextFillRect(context, CGRectMake( x, y, w, h))
        currentPosition += lineInterval
    }
    let image:UIImage = UIGraphicsGetImageFromCurrentImageContext()
    UIGraphicsEndImageContext()
    return image
}
func getColorTupleByColorHex(colorHex:String, alpha:CGFloat=1.0) -> (r:CGFloat,g:CGFloat,b:CGFloat){
    let colorScanner:NSScanner = NSScanner(string: colorHex)
    var color:UInt32 = 0
    colorScanner.scanHexInt(&color)
    let r:CGFloat = CGFloat((color & 0xFF0000) >> 16) / CGFloat(255.0);
    let g:CGFloat = CGFloat((color & 0x00FF00) >> 8)  / CGFloat(255.0);
    let b:CGFloat = CGFloat(color & 0x0000FF) / CGFloat(255.0);
    return (r: r, g: g, b: b)
}


結果
iOS Simulator Screen Shot 2015.07.22 16.09.11
[Swift]split  @ App Develop , Swift 2015年7月1日
split
let t:String = "yyyy-MM-dd HH:mm:ss"
//let tmp:[String] = split(t, {$0 == "-" || $0 == " " || $0 == ":"}) //この方法でもよい
let tmp:[String] = split(t, {contains(["-"," ",":"],$0)})
println(tmp)

出力結果
[yyyy, MM, dd, HH, mm, ss]