UIView和UI Button的簡(jiǎn)單使用

UIView

UIView表示屏幕上的一塊矩形區(qū)域,它在App中占有絕對(duì)重要的地位,因?yàn)镮OS中幾乎所有可視化控件都是UIView的子類(lèi)。負(fù)責(zé)渲染區(qū)域的內(nèi)容,并且響應(yīng)該區(qū)域內(nèi)發(fā)生的觸摸事件

初始化方式:

UIView *view=[[UIView alloc]initWithFrame:CGRectMake(0, 0, 50, 50)];

即初始化一個(gè)大小為50*50 xy軸坐標(biāo)為(0,0)的view


常用方法:

添加子控件: ??

?[UIView addSubview:Subview]

移除子控件:

[Subview removeFromSuperview]

獲取當(dāng)前所有的子控件(返回值為nsarray類(lèi)型):

[self.view subviews];

UIView的常用動(dòng)畫(huà)方法:

1.UIView animateWithDuration:<#(NSTimeInterval)#> animations:<#^(void)animations#>

NSTimeInterval:動(dòng)畫(huà)時(shí)間

^(void)animations:需要執(zhí)行動(dòng)畫(huà)的block代碼塊

2.UIView animateWithDuration:<#(NSTimeInterval)#> animations:<#^(void)animations#> completion:<#^(BOOL finished)completion#>

NSTimeInterval:動(dòng)畫(huà)時(shí)間

^(void)animations:需要執(zhí)行動(dòng)畫(huà)的block代碼塊

^(BOOL finished)completion:動(dòng)畫(huà)播放完后需要執(zhí)行的block代碼塊

注意:只有顏色,frame,alpha的改變才會(huì)有動(dòng)畫(huà)效果



UIButton


初始化方式:

UIButton *button=[UIButton buttonWithType:<#(UIButtonType)#>]

UIButtonType:為枚舉類(lèi)型,包括:UIButtonTypeCustom(自定義類(lèi)型),UIButtonTypeSystem(系統(tǒng)類(lèi)型)

常用方法

設(shè)置背景顏色

[button setBackgroundColor:]

設(shè)置背景圖片(根據(jù)不同的狀態(tài))

? [button setImage:<#(nullable UIImage *)#> forState:<#(UIControlState)#>]

?[ button setBackgroundImage:<#(nullable UIImage *)#> forState:<#(UIControlState)#>]

以上兩個(gè)方法都可以為button設(shè)置背景圖片,不同的是,用backgroundImage,會(huì)將圖片按照一倍圖的方式進(jìn)行填充,且此時(shí)設(shè)置button的title可見(jiàn),setImage就直接當(dāng)做button的content,會(huì)按照?qǐng)D片的實(shí)際倍數(shù)進(jìn)行填充,此時(shí)設(shè)置title不可見(jiàn)。

button添加點(diǎn)擊事件

[button addTarget:<#(nullable id)#> action:<#(nonnull SEL)#> forControlEvents:<#(UIControlEvents)#>]

target:是指需要哪個(gè)實(shí)例來(lái)執(zhí)行觸發(fā)方法

action:是指需要執(zhí)行的方法

ControlEvents(枚舉):是指響應(yīng)哪種手勢(shì)

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

推薦閱讀更多精彩內(nèi)容