LoginSignup
30
31

More than 5 years have passed since last update.

基本を理解するために(CGPoint,CGSize,CGRect,frame)

Last updated at Posted at 2017-04-08

まずはじめにどうやって宣言されているかを確認しました。以下一覧
CGPoint

CGGeometry.h

public struct CGPoint {

    public var x: CGFloat

    public var y: CGFloat

    public init()

    public init(x: CGFloat, y: CGFloat)
}

CGSize

CGGeometry.h
public struct CGSize {

    public var width: CGFloat

    public var height: CGFloat

    public init()

    public init(width: CGFloat, height: CGFloat)
}

CGRect

CGGeometry.h
public struct CGRect {

    public var origin: CGPoint

    public var size: CGSize

    public init()

    public init(origin: CGPoint, size: CGSize)
}

frame

UIView.h
extension UIView {


    // animatable. do not use frame if view is transformed since it will not correctly reflect the actual location of the view. use bounds + center instead.
    open var frame: CGRect


    // use bounds/center and not frame if non-identity transform. if bounds dimension is odd, center may be have fractional part
    open var bounds: CGRect // default bounds is zero origin, frame size. animatable

    open var center: CGPoint // center is center of frame. animatable

    open var transform: CGAffineTransform // default is CGAffineTransformIdentity. animatable

    @available(iOS 4.0, *)
    open var contentScaleFactor: CGFloat


    open var isMultipleTouchEnabled: Bool // default is NO

    open var isExclusiveTouch: Bool // default is NO


    open func hitTest(_ point: CGPoint, with event: UIEvent?) -> UIView? // recursively calls -pointInside:withEvent:. point is in the receiver's coordinate system

    open func point(inside point: CGPoint, with event: UIEvent?) -> Bool // default returns YES if point is in bounds


    open func convert(_ point: CGPoint, to view: UIView?) -> CGPoint

    open func convert(_ point: CGPoint, from view: UIView?) -> CGPoint

    open func convert(_ rect: CGRect, to view: UIView?) -> CGRect

    open func convert(_ rect: CGRect, from view: UIView?) -> CGRect


    open var autoresizesSubviews: Bool // default is YES. if set, subviews are adjusted according to their autoresizingMask if self.bounds changes

    open var autoresizingMask: UIViewAutoresizing // simple resize. default is UIViewAutoresizingNone


    open func sizeThatFits(_ size: CGSize) -> CGSize // return 'best' size to fit given size. does not actually resize view. Default is return existing view size

    open func sizeToFit() // calls sizeThatFits: with current view bounds and changes bounds size.
}

ここからわかったことframeもsizeのようにあるものだと思っていましたがそうではなかった。結果。
(CGPoint型, CGSize型, CGRect型)は宣言できるがframe型は存在しないframeはUIViewの作成を行う時にCGRectを入れておく変数名!

let point:CGPoint = CGPoint(x:0, y:0)

let size:CGSize = CGSize(width:100, height:100)

let cgrect:CGRect = CGRect(x:0, y:0, width:100, height:100 )
let cgrect1:CGRect = CGRect(origin: CGPoint(x:0, y:0), 

let frame:CGRect = CGRect(x: 0, y: 0, width: 100, height: 100)
var myVeiw:UIView = UIView(frame: frame)

よって下のこの書き方は存在しない。なぜならframeは型としてというよりUIView作成時にCGRectの値を渡すための変数だから。

let frame:frame = frame(x: 0, y: 0, width: 100, height: 100)

そしてself.viewのxやwidthの取得の前にoriginやsizeをつかないとアクセスできないかがわかるという:turtle:

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

ここら辺をあやふやにやっていたので色々つまずいていたのかなと思いました。
もっと勉強しなくては!!:hatching_chick:

30
31
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
30
31