[Swift]UIImageView Extension #1

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

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

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

[Swfit]GCD #2

GCD #2

func dispatch_async_global(block: () -> ()) {
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), block)
}

[Swift]GCD #1

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を上揃え

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

[objective-c]NSURLDownload

NSURLDownload

- (void)startDownloadWithURL:(NSString*)urlstr
{
    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:urlstr]
                                                cachePolicy:NSURLRequestUseProtocolCachePolicy
                                            timeoutInterval:5.0];
    NSURLDownload  *download = [[NSURLDownload alloc] initWithRequest:request delegate:self];
    if (!download) {

    }
}
- (void)download:(NSURLDownload *)download decideDestinationWithSuggestedFilename:(NSString *)filename
{
    NSString *destinationFilename = @"ローカルファイルパス";
    [download setDestination:destinationFilename allowOverwrite:YES];
}
- (void)download:(NSURLDownload *)download didReceiveResponse:(NSURLResponse *)response
{

}
- (void)download:(NSURLDownload *)download didReceiveDataOfLength:(NSUInteger)length
{

}
- (void)download:(NSURLDownload *)download didFailWithError:(NSError *)error
{

}
- (void)downloadDidFinish:(NSURLDownload *)download
{

}

[objective-c]NSViewのフェード

NSViewのフェード

view.wantsLayer = YES;
view.hidden = NO;
view.layer.opacity = 1.0;
CABasicAnimation * alphaAnimation  = [CABasicAnimation animationWithKeyPath:@"opacity"];
alphaAnimation.removedOnCompletion =  YES;
alphaAnimation.autoreverses = NO;
alphaAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];
alphaAnimation.duration = fadeDuration_;
alphaAnimation.fromValue = [NSNumber numberWithFloat:0.0];
alphaAnimation.toValue = [NSNumber numberWithFloat:1.0];
alphaAnimation.byValue = [NSNumber numberWithFloat:1.0];
[view.layer addAnimation:alphaAnimation forKey:@"opacityAnimation"];

[MacApp]アイコンの設定

アイコンの設定
・「sample.iconset」というフォルダを作成する※sampleは任意
・「sample.iconset」に下記ファイルを追加する※カッコ内は画像サイズ

・icon_16x16.png (16x16)
・icon_16x16@2x.png (32x32)
・icon_32x32.png (32x32)
・icon_32x32@2x.png (64x64)
・icon_128x128.png (128x128)
・icon_128x128@2x.png (256x256)
・icon_256x256.png (256x256)
・icon_256x256@2x.png (512x512)
・icon_512x512.png (512x512)
・icon_512x512@2x.png (1024x1024)

・「Info.plist」に「Icon files」を追加
・「Icon files」の「Item 0」に「sample」と入力