1、區(qū)別
frame: 該view在父view坐標(biāo)系統(tǒng)中的位置和大小。(參照點(diǎn)是,父親的坐標(biāo)系統(tǒng))
bounds:該view在本地坐標(biāo)系統(tǒng)中的位置和大小。(參照點(diǎn)是,本地坐標(biāo)系統(tǒng))。
其實(shí)本地坐標(biāo)系統(tǒng)的關(guān)鍵就是要知道的它的原點(diǎn)(0,0)在父坐標(biāo)系統(tǒng)中的什么位置(這個位置是相對于父view的本地坐標(biāo)系統(tǒng)而言的,最終的父view就是UIWindow,它的本地坐標(biāo)系統(tǒng)原點(diǎn)就是屏幕的左上角了)。
通過修改view的bounds屬性可以修改本地坐標(biāo)系統(tǒng)的原點(diǎn)位置。
2、bounds使用場景
滾動 scrollview 時,imageview 的 frame 和 bounds 還有 scrollview 的 frame 是沒有改變的。唯一在不斷改變的是 scrollview 的 contentoffset 和 bounds,而且兩者完全相同。
向上滾動scrollview,我們就不斷增加 scrollview 的 bounds 的 y 值,也就是不斷把 scrollview 的本地坐標(biāo)系原點(diǎn)向下偏移(相對于 scrollview 的父 view 的坐標(biāo)系,y 值越大,越向下偏移)。那么此時 scrollview 的子控件的 frame 設(shè)置的 (0,0) 就是不斷向上偏移
假設(shè)某一時刻 scrollview 的坐標(biāo)系原點(diǎn)為 (0,100) ,那么scrollview的 (0,0) 位置就是相對于坐標(biāo)系原點(diǎn)向上偏移 100 的距離,設(shè)置 scrollview 的子控件的 frame 為(0,0),就是設(shè)置子控件左上角在 scrollview 中的(0,0)位置,那么子控件就會向上偏移 100,你也就看到 scrollview 的內(nèi)容(子控件)向上滾動的效果。
3、bound 大于 frame
假設(shè)設(shè)置了控件的 bounds 大于 frame,那么此時會導(dǎo)致 frame 被撐大,frame 的 x,y,width,height 都會改變。
新的frame的size等于bound的size。
新的frame.x = 舊frame.x - (bounds.size.witdh - 舊frame.size.width)/2
新的frame.y = 舊frame.y - (bounds.size.height - 舊frame.size.height)/2
4、bound 具有疊加效應(yīng)
假設(shè) view1上面添加了 view2,view2 上面添加了 view3。三個 view 的 size 都是(100,100)。
我們設(shè)置如下:
view1.bound = (0,100,100,100)
view2.bound = (0,100,100,100)
那么此時 view3.frame = (0,0,100,100),view3 會相對于原來沒有設(shè)置 view1、view2 的 bound 時的位置向上偏移 200。
參考文檔:http://m.itdecent.cn/p/964313cfbdaa