您好,登錄后才能下訂單哦!
當(dāng)大家看到這兩個(gè)圖的時(shí)候,如果你沒有看本文的標(biāo)題,我想你的第一認(rèn)識(shí)絕對(duì)是View里面包了一個(gè)ImageView和一個(gè)Label,沒錯(cuò),其實(shí)UIButton的內(nèi)部就是這個(gè)構(gòu)造,圖1是由咱們?cè)?,而圖2則是咱們衍生的,在項(xiàng)目開發(fā)過程中,會(huì)有很多情況利用到圖2或更多相關(guān)衍生的現(xiàn)象,本期為大家做一個(gè)簡(jiǎn)單的例子,在項(xiàng)目中如何利用UIButton的內(nèi)部構(gòu)造
我們?nèi)绾螌?shí)現(xiàn)圖1到圖2的轉(zhuǎn)化,先為大家介紹圖中的各個(gè)顏色區(qū)域所表示的內(nèi)容
橙色為當(dāng)前View的背景色,紅色為Button的背景色,藍(lán)色為Button中的TitleLabel,圖片為Button中的ImageView
在閱讀下面代碼前,請(qǐng)大家一定要了解bounds的具體意義
//根據(jù)效果圖1我們先來獲得p_w_picpathView的center CGPoint p_w_picpathViewCenterNow = button.p_w_picpathView.center; //根據(jù)效果圖1我們先來獲得titleLabel的center CGPoint titleLabelCenterNow = button.titleLabel.center; //獲得Button的Center CGPoint buttonBoundsCenter = CGPointMake(CGRectGetMidX(button.bounds), CGRectGetMidY(button.bounds)); //根據(jù)效果圖2我們先來獲得p_w_picpathView最終想要得到的center CGPoint p_w_picpathViewCenter = CGPointMake(buttonBoundsCenter.x, CGRectGetMidY(button.p_w_picpathView.bounds)); //根據(jù)效果圖2我們?cè)賮慝@得titleLabel最終想要得到的center CGPoint titleLabelCenter = CGPointMake(buttonBoundsCenter.x, CGRectGetHeight(button.bounds) - CGRectGetMidY(button.titleLabel.bounds)); //根據(jù)兩個(gè)圖的差異我們來設(shè)置p_w_picpathEdgeInsets //因?yàn)槲覀円獙D上移,所以top一定是負(fù)值,bottom一定是正值,因?yàn)槲覀円獙D右移,所以left一定是正值,right一定是負(fù)值 CGFloat p_w_picpathEdgeInsetsTop = p_w_picpathViewCenter.y - p_w_picpathViewCenterNow.y; CGFloat p_w_picpathEdgeInsetsLeft = p_w_picpathViewCenter.x - p_w_picpathViewCenterNow.x; CGFloat p_w_picpathEdgeInsetsBottom = -p_w_picpathEdgeInsetsTop; CGFloat p_w_picpathEdgeInsetsRight = - p_w_picpathEdgeInsetsLeft; UIEdgeInsets p_w_picpathInsets = UIEdgeInsetsMake(p_w_picpathEdgeInsetsTop, p_w_picpathEdgeInsetsLeft, p_w_picpathEdgeInsetsBottom, p_w_picpathEdgeInsetsRight); button.p_w_picpathEdgeInsets = p_w_picpathInsets; //根據(jù)兩個(gè)圖的差異我們來設(shè)置titleEdgeInsets,跟p_w_picpath同理 CGFloat titleEdgeInsetsTop = titleLabelCenter.y - titleLabelCenterNow.y; CGFloat titleEdgeInsetsLeft = titleLabelCenter.x - titleLabelCenterNow.x; CGFloat titleEdgeInsetsBottom = -titleEdgeInsetsTop; CGFloat titleEdgeInsetsRight = -titleEdgeInsetsLeft; UIEdgeInsets titleInsets = UIEdgeInsetsMake(titleEdgeInsetsTop, titleEdgeInsetsLeft, titleEdgeInsetsBottom, titleEdgeInsetsRight); button.titleEdgeInsets = titleInsets;
沒錯(cuò),這就是UIButton的內(nèi)部再利用,小伙伴們有沒有想過,Button其實(shí)可以這么玩,由于時(shí)間有限,在這里就介紹這么多,歡迎校友們繼續(xù)探索并補(bǔ)充,如有更好的想法,請(qǐng)分享到技術(shù)群里,我們一起來討論
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。