Skip to content

Instantly share code, notes, and snippets.

@KentarouKanno
Last active September 10, 2016 09:23
Show Gist options
  • Save KentarouKanno/fd51c426c4549550b484 to your computer and use it in GitHub Desktop.
Save KentarouKanno/fd51c426c4549550b484 to your computer and use it in GitHub Desktop.
CGGeometry

CGGeometry

★ CGPoint

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

var x = point.x
var y = point.y

★ CGSize

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

var width = size.width
var height = size.height

★ CGRect

let rect: CGRect = CGRectMake(0, 0, 100, 100)
let rect: CGRect = CGRect(origin: CGPointMake(0, 0), size: CGSizeMake(100, 100))
let rect2: CGRect = CGRect(origin: CGPoint(x: 0, y: 0), size: CGSize(width: 100, height: 100))

var x = rect.origin.x
var y = rect.origin.y
var width = rect.size.width
var heith = rect.size.height


// 画面サイズで処理を分岐する
switch self.view.frame.size {
    case CGSize(width: 320.0, height: 480.0):print("3.5inch") //iPhone4S
    case CGSize(width: 320.0, height: 568.0):print("4inch")   //iPhone5,iPhone5S,iPodTouch5
    case CGSize(width: 375.0, height: 667.0):print("4.7inch") //iPhone6
    case CGSize(width: 414.0, height: 736.0):print("5.5inch") //iPhone6Plus
    default : print("取得エラー")
}

★ Zero

var rectZero = CGRectZero
var rectZero = CGRect.zero
//=> {x 0 y 0 w 0 h 0}

var pointZero = CGPointZero
var pointZero = CGPoint.zero
//=> {x 0 y 0}

var sizeZero = CGSizeZero
var sizeZero = CGSize.zero
//=> {w 0 h 0}

★ Rect,Size,Origin

let frame = CGRectMake(30, 60, 100, 150)
//=> {x 30 y 60 w 100 h 150}

let width = frame.size.width
//=> 100

let height = frame.size.height
//=> 150

let x = frame.origin.x
//=> 30

let y = frame.origin.y
//=> 60

// -------------------------

let width = CGRectGetWidth(frame)
//=> 100

let height = CGRectGetHeight(frame)
//=> 150

let maxX = CGRectGetMaxX(frame)
//=> 130

let midX = CGRectGetMidX(frame)
//=> 80

let minX = CGRectGetMinX(frame)
//=> 30

let maxY = CGRectGetMaxY(frame)
//=> 210

let midY = CGRectGetMidY(frame)
//=> 135

let minY = CGRectGetMinY(frame)
//=> 60

let inset = CGRectInset(frame, 10, 0)
//=> (40.0, 60.0, 80.0, 150.0)

★ add Value

frame.size.width += 10
frame.size.height += 10

frame.origin.x += 30
frame.origin.y += 30

CGRectContainsPoint

★ タップした位置(CGPoint)が指定のviewの範囲内にあるか判定する

import UIKit

class ViewController: UIViewController {
    
    @IBOutlet weak var view1: UIView!
    @IBOutlet weak var view2: UIView!
    

    override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
        let touchEvent = touches.first!
        let point = touchEvent.locationInView(self.view)
        
        let convertRect = view2.superview!.convertRect(view2.frame, toView: nil)
        if CGRectContainsPoint(convertRect, point) {
            print("Tap view2 Area")
        }
    }
}

s

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment