Swift

[Swift]参照渡し

参照渡し

class func addMonthWithYearMonth(inout ym:(year:Int,month:Int),addMonth:Int){
    var y:Int = ym.year
    var m:Int = ym.month + addMonth
    while 12 < m {
        m -= 12
        y++
    }
    while m < 1 {
        m += 12
        y--
    }
    ym.year=y
    ym.month=m
}

[Swift]sort

sort

var w:[AnyObject] = self.subviews
w.sort { (v1:AnyObject, v2:AnyObject) -> Bool in
    return v1.tag < v2.tag
}

[Swift]クラス拡張

クラス拡張 objective-cにおけるカテゴリ

extension UIView {
    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)
        }
    }
    func toUIImage() -> UIImage{
        UIGraphicsBeginImageContextWithOptions(self.bounds.size, false, 0.0)
        self.layer.renderInContext(UIGraphicsGetCurrentContext())
        let image:UIImage = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()
        return image
    }
}

[Swift]Swift #5 animateWithDuration

Swift #5 animateWithDuration

@IBOutlet weak var mainLabel: UILabel!

func initUI(){
    var message:String = getMessage(message:"いあうえ",message2:"390u8")
    println(message)
        
    message = getMessage(message2:"むぎちゃ")
    println(message)

    UIView.animateWithDuration(
        2.0,
        delay: 1.0,
        usingSpringWithDamping: 1.0,
        initialSpringVelocity: 0.0,
        options: UIViewAnimationOptions.CurveEaseInOut,
        animations:{()in
            self.mainLabel.frame = CGRectMake(100.0, 300.0, self.mainLabel.frame.size.width, self.mainLabel.frame.size.height)
        },
        completion:{(finished)in
            println("finished:\(finished)")
            
        })
}
func getMessage(var message:String="ABC",var message2:String="MBNRKF")->String{
    return "\(message) \(message2)"
}

override func viewDidLoad() {
    super.viewDidLoad()
    initUI()
}

出力結果

いあうえ 390u8
ABC むぎちゃ
finished:true

[Swift]Swift #4 カメラを扱う

Swift #4 カメラを扱う

Main.storyboard
Main.storyboard

ViewController

class ViewController: UIViewController, UINavigationControllerDelegate, UIImagePickerControllerDelegate {
    
    @IBOutlet var mainImageView: UIImageView
    var picker:UIImagePickerController?
    
    override func viewDidLoad() {
        super.viewDidLoad()
        picker = UIImagePickerController()
        picker!.delegate = self
        picker!.allowsEditing = false
    }

    @IBAction func onTapButton1(button:UIButton){
        println( "onTapButton - open album" )
        
        if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.PhotoLibrary) {
            picker!.sourceType = UIImagePickerControllerSourceType.PhotoLibrary
            self.presentViewController(picker, animated: true, completion: nil)
        }
    }
    
    @IBAction func onTapButton2(button:UIButton){
        println( "onTapButton - take photo" )
        
        if picker==nil {
            return
        }
        
        if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.Camera) {
            picker!.sourceType = UIImagePickerControllerSourceType.Camera
            self.presentViewController(picker, animated: true, completion: nil)
        }
    }
    
    func imagePickerController(picker: UIImagePickerController!, didFinishPickingImage image: UIImage!, editingInfo: NSDictionary!){
        println( "imagePickerController" )
        self.dismissViewControllerAnimated(true, completion: nil)
        mainImageView.image = image
        if picker!.sourceType == UIImagePickerControllerSourceType.Camera {
            println( "save photo" )
            UIImageWriteToSavedPhotosAlbum(image, self, "onSaveImageWithUIImage:error:contextInfo:", nil)
        }
    }
    
    func onSaveImageWithUIImage(image: UIImage!, error: NSErrorPointer, contextInfo: UnsafePointer<()>){
        if error {
            println( "error" )
            return
        }
        println( "success" )
    }
    
    func imagePickerControllerDidCancel(picker: UIImagePickerController!){
        println( "imagePickerControllerDidCancel" )
        self.dismissViewControllerAnimated(true, completion: nil)
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

}