★ 初期化
let view = UIView()
// サイズを指定する
let view = UIView(frame: CGRectMake(0, 0, 100, 100))
★ 背景色のalphaを指定する
view.backgroundColor = UIColor(red:0.0,green:0.5,blue:1.0,alpha:0.5)
★ タグを取得、設定する
let tag = view.tag
view.tag = 5
★ 設定しているタグからオブジェクトを取得する
// オブジェクトが乗っているViewに対してタグ番号を指定する
let label = view.viewWithTag(1) as! UILabel
★ Viewのframeを取得、設定する
let frame = view.frame
//=> (0.0, 0.0, 320.0, 568.0)
view.frame = CGRectMake(0, 0, 100, 100)
★ Viewのboundsを取得、設定する
let bounds = view.bounds
//=> (0.0, 0.0, 320.0, 568.0)
view.bounds = CGRectMake(0, 0, 200, 200)
★ Viewをインサートする
// view2をview1の上にインサートする
self.view.insertSubview(view2, aboveSubview: view1)
// view2をview1の下にインサートする
self.view.insertSubview(view2, belowSubview: view1)
★ 指定のViewの最前面に配置し直す
// self.viewの最前面にview2を配置する
self.view.bringSubviewToFront(view2)
★ 指定のViewを最背面に配置する
// self.viewのSubviewsの中で最背面にview2を配置する
self.view.sendSubviewToBack(view2)
★ 指定のViewへアニメーション、アニメーション時間を設定して遷移する
let next = storyboard?.instantiateViewControllerWithIdentifier("next")
UIView.transitionFromView( self.view,
toView : next!.view,
duration : 0.5,
options : .TransitionFlipFromLeft,
completion: nil
)
★ UIView Life Cycle
import UIKit
class CustomView: UIView {
override func setNeedsDisplay() {
super.setNeedsDisplay()
print("setNeedsDisplay()")
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
print("init(coder: aDecoder)")
}
override init(frame: CGRect) {
super.init(frame: frame)
print("init(frame: CGRect)")
}
override func willMoveToSuperview(newSuperview: UIView?) {
super.willMoveToSuperview(newSuperview)
print("willMoveToSuperview()")
}
override func didMoveToSuperview() {
super.didMoveToSuperview()
print("didMoveToSuperview()")
}
override func awakeFromNib() {
super.awakeFromNib()
print("awakeFromNib()")
}
override func willMoveToWindow(newWindow: UIWindow?) {
super.willMoveToWindow(newWindow)
print("willMoveToWindow(newWindow: UIWindow?)")
}
override func needsUpdateConstraints() -> Bool {
super.needsUpdateConstraints()
print("needsUpdateConstraints()")
return true
}
override func didMoveToWindow() {
super.didMoveToWindow()
print("didMoveToWindow()")
}
override func updateConstraints() {
super.updateConstraints()
print("updateConstraints()")
}
override func layoutSubviews() {
super.layoutSubviews()
print("layoutSubviews()")
}
override func drawRect(rect: CGRect) {
print("drawRect(rect: CGRect)")
}
override func didAddSubview(subview: UIView) {
super.didAddSubview(subview)
print("didAddSubview(subview: UIView)")
}
override func willRemoveSubview(subview: UIView) {
super.willRemoveSubview(subview)
print("willRemoveSubview(subview: UIView)")
}
deinit {
print("deinit")
}
/*
// 自分自身が描画される(Storyboardに配置した場合)
setNeedsDisplay()
↓
setNeedsDisplay()
↓
setNeedsDisplay()
↓
init(coder: aDecoder)
↓
willMoveToSuperview()
↓
didMoveToSuperview()
↓
awakeFromNib()
↓
willMoveToWindow(newWindow: UIWindow?)
↓
needsUpdateConstraints()
↓
didMoveToWindow()
↓
updateConstraints()
↓
layoutSubviews()
↓
drawRect(rect: CGRect)
↓
画面表示完了
// 自分自身が描画される(コードで生成した場合)
let customV = CustomView(frame: CGRectMake(0,0,100,100))
view.addSubview(customV)
setNeedsDisplay()
↓
setNeedsDisplay()
↓
init(frame: CGRect)
↓
setNeedsDisplay()
↓
willMoveToSuperview()
↓
didMoveToSuperview()
↓
willMoveToWindow(newWindow: UIWindow?)
↓
needsUpdateConstraints()
↓
didMoveToWindow()
↓
updateConstraints()
↓
layoutSubviews()
↓
drawRect(rect: CGRect)
↓
画面表示完了
// 自分自身に他のViewがaddされる時
didAddSubview(subview: UIView)
↓
layoutSubviews()
// 自分自身から他のViewがemoveFromSuperView()される時
willRemoveSubview(subview: UIView)
↓
layoutSubviews()
// 自分自身のViewがsuperViewからremoveFromSuperView()される時
willMoveToSuperview()
↓
willMoveToWindow(newWindow: UIWindow?)
↓
didMoveToWindow()
↓
didMoveToSuperview()
↓
deinit
*/
}
★ タップした位置(CGPoint)が指定のviewの範囲内にあるか判定する
※ UIViewの座標をsuperViewの座標に置き換えている
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 = view.convertRect(view2.bounds, fromView: view2) // 以下と同じ意味
let convertRect = view2.superview!.convertRect(view2.frame, toView: nil)
if CGRectContainsPoint(convertRect, point) {
print("Tap view2 Area")
}
}
}
★ TapしたUITableViewCellの位置をviewの位置に座標を変換する
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
tableView.deselectRowAtIndexPath(indexPath, animated: true)
let cell = tableView.cellForRowAtIndexPath(indexPath)!
let convertRect = view.convertRect(cell.bounds, fromView: cell)
//let convertRect = cell.superview!.convertRect(cell.frame, toView: nil)
let v = UIView(frame: convertRect)
v.backgroundColor = UIColor.redColor()
view.addSubview(v)
}