LoginSignup
6
4

More than 5 years have passed since last update.

width,height以外のmin​Xなどの整理をしたくて

Last updated at Posted at 2017-04-08

width,height以外のmin​Xなどちゃんと理解できていなかったので理解のために書きます:hatched_chick:

参考URL:https://developer.apple.com/reference/coregraphics/cgrect

ViewController.swift
import UIKit

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view, typically from a nib.
        var x:CGFloat = self.view.bounds.origin.x
        var y:CGFloat = self.view.bounds.origin.x
        var width:CGFloat = self.view.bounds.size.width
        var height:CGFloat = self.view.bounds.size.height
        var frame:CGRect = CGRect(x:x, y:y, width:width ,height:height  )
        var view = UIView(frame:frame)
        view.backgroundColor = UIColor.green
        self.view.addSubview(view)
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
}

viewの座標の値をとっていっぱいに
Simulator Screen Shot 2017.04.08 17.15.30.png

ここからgoogle翻訳さんに頼って翻訳します

<img width="414" alt="Simulator Screen Shot 2017.04.08 17.15.30.png" src="https://qiita-image-store.s3.amazonaws.com/0/133940/36bc36bb-cdb5-032f-9404-16f59cc3b3b9.png">

var height:​ CGFloat
Returns the height of a rectangle.
長方形の高さを返します。

var width:​ CGFloat
Returns the width of a rectangle.
長方形の幅を返します。

var min​X:​ CGFloat
Returns the smallest value for the x-coordinate of the rectangle.
長方形のx座標の最小値を返します。

var mid​X:​ CGFloat
Returns the x- coordinate that establishes the center of a rectangle.
長方形の中心を設定するx座標を返します。

var max​X:​ CGFloat
Returns the largest value of the x-coordinate for the rectangle.
長方形のx座標の最大値を返します。

var min​Y:​ CGFloat
Returns the smallest value for the y-coordinate of the rectangle.
長方形のy座標の最小値を返します。

var mid​Y:​ CGFloat
Returns the y-coordinate that establishes the center of the rectangle.
長方形の中心を設定するy座標を返します。

var max​Y:​ CGFloat
Returns the largest value for the y-coordinate of the rectangle.
長方形のy座標の最大値を返します。
ViewController.swift

import UIKit

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view, typically from a nib.
        var x:CGFloat = self.view.bounds.origin.x
        var y:CGFloat = self.view.bounds.origin.x
        var width:CGFloat = self.view.bounds.size.width
        var height:CGFloat = self.view.bounds.size.height

        x = self.view.bounds.midX
        var frame:CGRect = CGRect(x:x, y:y, width:width ,height:height  )
        var view = UIView(frame:frame)
        view.backgroundColor = UIColor.green
        self.view.addSubview(view)

    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


}

x座標を長方形の中心にしてしたため

Simulator Screen Shot 2017.04.08 17.45.35.png

こんな感じで行って行くと

最初値は0なので
x = self.view.bounds.minX

Simulator Screen Shot 2017.04.08 17.15.30.png

xを最大にすると見えなくなります。
x = self.view.bounds.max​X

Simulator Screen Shot 2017.04.08 17.56.38.png

y座標の最小値は最初の0と変わらないので同じ見え方
y = self.view.bounds.minY

Simulator Screen Shot 2017.04.08 17.15.30.png

y座標が中心になるので下半分が塗りつぶされます。
y = self.view.bounds.midY

Simulator Screen Shot 2017.04.08 17.58.32.png

yがマックスになってしまうので見えません
y = self.view.bounds.maxY

Simulator Screen Shot 2017.04.08 17.56.38.png

色々な使い方があるかもしれませんがとりあえず。
一回ちゃんと見ると頭で考えるより想像しやすくなったような。
そしてself.viewのバックカラーを何かにしとけば見えやすかったのかなーと少し悩んでます:relaxed:

6
4
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
6
4