info

category : Swift
[Swfit]UIView Extension #5  @ App Develop , Swift 2016年3月30日
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
            }
        }
    )
}
[Swfit]UIView Extension #3  @ App Develop , Swift 2015年12月30日
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
            
        }
    )
}
[Swift]UIImageView Extension #1  @ App Develop , Swift 2015年12月29日
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)
            }
        }
    }
}
[Swift]UIView Extension #2  @ App Develop , Swift 2015年11月29日
UIView Extension #2
var className:String{
    get{
        return NSStringFromClass(self.dynamicType).componentsSeparatedByString(".").last!
    }
}
var fileName:String{
    get{
        return __FILE__.componentsSeparatedByString("/").last!
    }
}
[Swift]UIView Extension #1  @ App Develop , Swift 2015年11月28日
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)
    }
}
[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
        }
    }
}