[Swift]UITextField Extension #1

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

[Swift]UIButton Extension #1

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

[Swfit]UIView Extension #5

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

[javascript]isArray

isArray

var isArray = function(target){
	return getTypeString(target) == 'array';
};
var getTypeString = function(target){
	if(target == null){
		return "null";
	}
	var toString = {}.toString;
	return (typeof target === "object" || typeof target === "function") ? toString.call(target).split(' ').pop().toLowerCase().replace(/\]/g,'') : typeof obj;
};

[javascript]orientationchange

iOSのorientationchange時の処理順
画面サイズ変更 -> resize -> orientationchange

Androidのorientationchange時の処理順
orientationchange -> 画面サイズ変更 -> resize
又は 画面サイズ変更 -> resize -> orientationchange
※端末(Androidのバージョン?)によってはこの順番の時もある

よってjsで端末回転時の処理をする際は下記のようにする。

デモ

var oEventCount = 0;
$(window).off('.ori').on('orientationchange.ori resize.ori',function(e){
	oEventCount++;
	if(oEventCount < 2){
		return;
	}
	oEventCount = 0;

	//do something
});

・resizeイベントだけで済む場合もある。
・iOSの場合、スクロールでステータスバーが出たり引っ込んだりした時にresizeイベントが走る

こっちのほうが良い場合も

var oEventId;
$(window).off('.ori').on('orientationchange.ori resize.ori',function(e){
	clearTimeout(oEventId);
	oEventId = setTimeout(function(){
		//do something
	},300);
});

[Swift]UIView Extension #4

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

[Swfit]UIView Extension #3

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