/*
view的layer的實(shí)現(xiàn)
通過(guò)view的layer直接設(shè)置的方式
*/
UIImageView *redImgView = [[UIImageView alloc]initWithFrame:CGRectMake(50, 50, 100, 100)];
redImgView.contentMode = UIViewContentModeScaleToFill;
redImgView.image = [UIImage imageNamed:@"11"];
redImgView.layer.cornerRadius = redImgView.frame.size.width / 2;
redImgView.layer.masksToBounds = YES;
[self.view addSubview:redImgView];
/*
使用貝塞爾曲線UIBezierPath和Core Graphics實(shí)現(xiàn)
BezierPath的實(shí)現(xiàn)方式繼承UIView,自己實(shí)現(xiàn)一個(gè)customview
*/
UIImageView *blueImgView = [[UIImageView alloc]initWithFrame:CGRectMake(50, 200, 100, 100)];
blueImgView.contentMode = UIViewContentModeScaleAspectFit;
blueImgView.image = [UIImage imageNamed:@"22"];
//開(kāi)始對(duì)imageView進(jìn)行畫(huà)圖
UIGraphicsBeginImageContextWithOptions(blueImgView.bounds.size, NO, 1.0);
//使用貝塞爾曲線畫(huà)出一個(gè)圓形圖
[[UIBezierPath bezierPathWithRoundedRect:blueImgView.bounds cornerRadius:blueImgView.frame.size.width] addClip];
[blueImgView drawRect:blueImgView.bounds];
blueImgView.image = UIGraphicsGetImageFromCurrentImageContext();
//結(jié)束畫(huà)圖
UIGraphicsEndImageContext();
[self.view addSubview:blueImgView];
/*
使用CAShapeLayer和UIBezierPath實(shí)現(xiàn)
通過(guò)bezizerpath設(shè)置一個(gè)路徑俏拱,加到目標(biāo)視圖的layer上顿涣。
*/
UIImageView *grayImgView = [[UIImageView alloc]initWithFrame:CGRectMake(200, 50, 100, 100)];
grayImgView.contentMode = UIViewContentModeScaleAspectFill;
grayImgView.image = [UIImage imageNamed:@"33"];
UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:grayImgView.bounds byRoundingCorners:UIRectCornerAllCorners cornerRadii:grayImgView.bounds.size];
CAShapeLayer *maskLayer = [[CAShapeLayer alloc]init];
//設(shè)置大小
maskLayer.frame = grayImgView.bounds;
//設(shè)置圖形樣子
maskLayer.path = maskPath.CGPath;
grayImgView.layer.mask = maskLayer;
[self.view addSubview:grayImgView];
image.png
//設(shè)置view左側(cè)上UIRectCornerTopLeft 和 左側(cè)下UIRectCornerBottomLeft為5的圓角
UIBezierPath * path = [UIBezierPath bezierPathWithRoundedRect:view.bounds byRoundingCorners:UIRectCornerTopLeft|UIRectCornerBottomLeft cornerRadii:CGSizeMake(5, 5)];
CAShapeLayer * layer = [[CAShapeLayer alloc]init];
layer.frame = view.bounds;
layer.path = path.CGPath;
view.layer.mask = layer;
//設(shè)置view 全部角為圓角
viewT.layer.cornerRadius = 10;//設(shè)置那個(gè)圓角大小
viewT.layer.masksToBounds = YES;//設(shè)置YES是保證添加的圖片覆蓋視圖的效果
iOS11 layer新增了maskedCorners屬性
typedef NS_OPTIONS (NSUInteger, CACornerMask)
{
kCALayerMinXMinYCorner = 1U << 0,
kCALayerMaxXMinYCorner = 1U << 1,
kCALayerMinXMaxYCorner = 1U << 2,
kCALayerMaxXMaxYCorner = 1U << 3,
};
//對(duì)左下角和右下角進(jìn)行圓角處理
if (@available(iOS 11.0, *)) {
view2.layer.maskedCorners = kCALayerMaxXMaxYCorner | kCALayerMaxXMinYCorner;
} else {
// Fallback on earlier versions
}
UIViewContentMode
視圖內(nèi)容的填充方式
@property(nonatomic) UIViewContentMode contentMode;
typedef NS_ENUM(NSInteger, UIViewContentMode) {
UIViewContentModeScaleToFill, //填充到整個(gè)視圖區(qū)域斑举,不等比例拉伸。
UIViewContentModeScaleAspectFit, //長(zhǎng)寬等比填充視圖區(qū)域眼滤,當(dāng)某一個(gè)邊到達(dá)視圖邊界的時(shí)候就不再拉伸盖矫,保證內(nèi)容的長(zhǎng)寬比是不變的同時(shí)盡可能的填充視圖區(qū)域厌丑。
UIViewContentModeScaleAspectFill, //長(zhǎng)寬等比填充視圖區(qū)域,當(dāng)某一個(gè)邊到達(dá)視圖邊界的時(shí)候還繼續(xù)拉伸匹耕,直到另一個(gè)方向達(dá)到視圖邊界聚请。內(nèi)容的長(zhǎng)寬比不變的同時(shí)填滿整個(gè)視圖區(qū)域,不顯示超過(guò)的部分。
UIViewContentModeRedraw, //重繪視圖邊界
UIViewContentModeCenter, //視圖居中
UIViewContentModeTop, //視圖頂部對(duì)齊
UIViewContentModeBottom, //視圖底部對(duì)齊
UIViewContentModeLeft, //視圖左側(cè)對(duì)齊
UIViewContentModeRight, //視圖右側(cè)對(duì)齊
UIViewContentModeTopLeft, //視圖左上角對(duì)齊
UIViewContentModeTopRight, //視圖右上角對(duì)齊
UIViewContentModeBottomLeft, //視圖左下角對(duì)齊
UIViewContentModeBottomRight, //視圖右下角對(duì)齊
};