1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

SwiftでUIImageで画像を並べる

Last updated at Posted at 2015-06-13

ズバわかり! プログラミング Objective-C iPhoneアプリ開発 スタートブック Xcode5.1+iOS7.1対応

の Chap. 5-2 みたいに画像を並べるものをSwiftで書いてみた

Swiftのベストプラクティスがわかってないのでとりあえず動くレベル

Cannot invoke + with an argument of type CGRect error in Swift

ここに書いてあるように最初CGRectMakeを利用したら引数の型がおかしいとか言われたのでCGRectを利用した。

        let fooImage = UIImage(named: "foo.png")
        let barImage = UIImage(named: "bar.png")
        var imageView = UIImageView()
        
        let w = 50 // 幅
        let h = 50 // 高さ
        var x: Int
        var y: Int
        
        for var i = 0;  i < 4; i++ {
            for var j = 0; j < 5; j++ {
                switch  (j % 2) {
                case 0:
                    imageView = UIImageView(image: fooImage)
                    break
                case 1:
                    imageView = UIImageView(image: barImage)
                    break
                default:
                    break
                }

                x = 10+(w+5)*i   // x座標
                y = 50+(h+10)*j // y座標
                
                imageView.frame = CGRect(x: x, y: y, width: w, height: h)
                self.view.addSubview(imageView)

            }
        }
1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?