使用相關的知識點是calayer, 對這些內容一知半解的。
開發中用到了,搜尋了很多網頁,也算是做個小總結。
一直接設定view的四個角為圓角。
#import view.layer.cornerradius = cornerradiusinpixels;
view.layer.maskstobounds = yes;
直接在使用的組建view上設定。
二 根據需要設定view的角,比如左上,左下,右上,右下
此方法來自:
static inline uiimage* mtdcontextcreateroundedmask( cgrect rect, cgfloat radius_tl, cgfloat radius_tr, cgfloat radius_bl, cgfloat radius_br )
// cerate mask
cgfloat minx = cgrectgetminx( rect ), midx = cgrectgetmidx( rect ), maxx = cgrectgetmaxx( rect );
cgfloat miny = cgrectgetminy( rect ), midy = cgrectgetmidy( rect ), maxy = cgrectgetmaxy( rect );
cgcontextbeginpath( context );
cgcontextsetgrayfillcolor( context, 1.0, 0.0 );
cgcontextaddrect( context, rect );
cgcontextclosepath( context );
cgcontextdrawpath( context, kcgpathfill );
cgcontextsetgrayfillcolor( context, 1.0, 1.0 );
cgcontextbeginpath( context );
cgcontextmovetopoint( context, minx, midy );
cgcontextaddarctopoint( context, minx, miny, midx, miny, radius_bl );
cgcontextaddarctopoint( context, maxx, miny, maxx, midy, radius_br );
cgcontextaddarctopoint( context, maxx, maxy, midx, maxy, radius_tr );
cgcontextaddarctopoint( context, minx, maxy, minx, midy, radius_tl );
cgcontextclosepath( context );
cgcontextdrawpath( context, kcgpathfill );
// create cgimageref of the main view bitmap content, and then
// release that bitmap context
cgimageref bitmapcontext = cgbitmapcontextcreateimage( context );
cgcontextrelease( context );
// convert the finished resized image to a uiimage
uiimage *theimage = [uiimage imagewithcgimage:bitmapcontext];
// image is retained by the property setting above, so we can
// release the original
cgimagerelease(bitmapcontext);
// return the image
return theimage;
}
然後使用上面的函式,在需要的view上進行設定
uiimage *mask = mtdcontextcreateroundedmask( self.view.bounds, 50.0, 50.0, 0.0, 0.0 );
// create a new layer that will work as a mask
calayer *layermask = [calayer layer];
layermask.frame = self.view.bounds;
// put the mask image as content of the layer
layermask.contents = (id)mask.cgimage;
// set the mask layer as mask of the view layer
self.view.layer.mask = layermask;
// add a backaground color just to check if it works
self.view.backgroundcolor = [uicolor redcolor];
// add a test view to verify the correct mask clipping
uiview *testview = [[uiview alloc] initwithframe:cgrectmake( 0.0, 0.0, 50.0, 50.0 )];
testview.backgroundcolor = [uicolor bluecolor];
[self.view addsubview:testview];
uitableview設定為圓角的時候,用1的方法可以。
但是如果uitableview設定兩個角為圓角,兩個角為方角的時候,2的方法就有錯誤出現。
現在只能是給uitableview在加乙個背景view,使用2進行設定,然後組合顯示。
其他參考
實現圓角的三種方法
例子
UIButton 如何設定為圓角矩形
先上 登入按鈕 loginbtn uibutton buttonwithtype uibuttontyperoundedrect loginbtn.frame cgrectmake 1,199,200,36 self.view addsubview loginbtn loginbtn addtarg...
UIButton 如何設定為圓角矩形
先上 objc view plain copy 登入按鈕 loginbtn uibuttonbuttonwithtype uibuttontyperoundedrect loginbtn.frame cgrectmake 1,199,200,36 self view addsubview login...
設定任意某個角為圓角
原文 設定控制項圓角,只設定上面兩個角的圓角,下面兩個角依然是直角,如圖效果 通過貝塞爾曲線重繪layer層 uiimageview picimageview uiimageview alloc initwithframe cgrectmake 100,100,100,100 picimagevie...